private static ICSharpFile GetLinkedTypeFile(string linkedTypeName, string linkedTypeNamespace, ITypeElement templateLinkedType) { var elementFactory = CSharpElementFactory.GetInstance(templateLinkedType.GetFirstDeclaration <IDeclaration>().NotNull()); var templateLinkedTypeSourceFile = templateLinkedType.GetSingleOrDefaultSourceFile().NotNull("templateLinkedTypeSourceFile != null"); var templateFile = templateLinkedTypeSourceFile.GetPrimaryPsiFile().NotNull("templateFile != null"); var fileText = templateFile.GetText() .Replace(templateLinkedType.GetContainingNamespace().QualifiedName, linkedTypeNamespace) .Replace(templateLinkedType.ShortName, linkedTypeName); var linkedTypeFile = elementFactory.CreateFile(fileText); var typeDeclarations = GetTypeDeclarations(linkedTypeFile); var linkedType = (IClassDeclaration)typeDeclarations.Single(x => x.DeclaredName == linkedTypeName); // Remove base types foreach (var x in linkedType.SuperTypes) { linkedType.RemoveSuperInterface(x); } // Clear body linkedType.SetBody(((IClassLikeDeclaration)elementFactory.CreateTypeMemberDeclaration("class C{}")).Body); // Remove unrelated types foreach (var declaration in linkedTypeFile.TypeDeclarations.Where(x => x.DeclaredName != linkedTypeName)) { ModificationUtil.DeleteChild(declaration); } return(linkedTypeFile); }