void OpenClassDeclaration(RoslynCodeBuilder roslynCodeBuilder) { roslynCodeBuilder.AddClass ( ClassName, null, SuperClassTypeSymbol, AllImplementedInterfaces.ToArray() ); }
static void Main(string[] args) { MSBuildWorkspace workspace = MSBuildWorkspace.Create(); Project proj = workspace.OpenProjectAsync("../../../TestProj/TestProj.csproj").Result; Compilation compilation = proj.GetCompilationAsync().Result; INamedTypeSymbol myClass = compilation.GetTypeByMetadataName("TestProj.MyClass"); INamedTypeSymbol myInterface = compilation.GetTypeByMetadataName("TestProj.IMyInterface"); string fullNamespace = myClass.GetFullNamespace(); IEnumerable <ISymbol> allMembers = myClass.GetMembers(); IMethodSymbol myMethod = myClass.GetMemberByName <IMethodSymbol>(nameof(MyClass.MyMethod)); IPropertySymbol myProp = myClass.GetMemberByName <IPropertySymbol>(nameof(MyClass.MyInt)); string methodSignature = myMethod.GetMethodSignature(); IPropertySymbol intProp = myInterface.GetMemberByName <IPropertySymbol>(nameof(IMyInterface.TheInt)); IPropertySymbol strProp = myInterface.GetMemberByName <IPropertySymbol>(nameof(IMyInterface.TheStr)); RoslynCodeBuilder roslynCodeBuilder = new RoslynCodeBuilder(); RoslynAnalysisAndGenerationUtils.TheNamespaces = roslynCodeBuilder.AllNamespaces; RoslynAnalysisAndGenerationUtils.TheAssemblies = roslynCodeBuilder.AllAssemblies; roslynCodeBuilder.AddNamespace("NP.Generated"); roslynCodeBuilder.AddClass("MyClass1", null, null, myInterface); roslynCodeBuilder.AddAutoProp(intProp.Name, intProp.Type as INamedTypeSymbol); roslynCodeBuilder.AddAutoProp(strProp.Name, strProp.Type as INamedTypeSymbol); //roslynCodeBuilder.AddField("_myField", myClass); //roslynCodeBuilder.AddPropWithBackingStore("MyProp", myClass); //roslynCodeBuilder.AddPropWrapper("_propField", myProp); //roslynCodeBuilder.AddPropOpening("MyField", myClass); //roslynCodeBuilder.Pop(); roslynCodeBuilder.AddMethodWrapper("_propField", myMethod); roslynCodeBuilder.PopAll(); Console.WriteLine(roslynCodeBuilder.ToString()); }
void OpenClassDeclaration(RoslynCodeBuilder roslynCodeBuilder) { INamedTypeSymbol[] allSymbols = { ImplementorTypeSymbol, TypeToImplementSymbol }; INamedTypeSymbol firstSymbol = allSymbols.FirstOrDefault(symb => symb.IsClass()) ?? ImplementorTypeSymbol; INamedTypeSymbol[] otherSymbols = allSymbols.Except(firstSymbol.ToCollection()).ToArray(); roslynCodeBuilder.AddClass ( ClassName, null, firstSymbol.GetNullForNoType(TheCompilation), true, otherSymbols ); }
static void Main(string[] args) { MSBuildWorkspace workspace = MSBuildWorkspace.Create(); Project proj = workspace.OpenProjectAsync("../../InterfaceImplTest.csproj").Result; //Project proj = workspace.OpenProjectAsync("../../../TestProj/TestProj.csproj").Result; //Assembly testProjAssembly = Assembly.ReflectionOnlyLoadFrom("TestProj.dll"); //AssemblyMetadata assemblyMetadata = // Microsoft.CodeAnalysis.AssemblyMetadata.CreateFromFile("TestProj.dll"); //AssemblyIdentity assemblyIdentity = AssemblyIdentity.FromAssemblyDefinition(testProjAssembly); Compilation compilation = proj.GetCompilationAsync().Result; INamedTypeSymbol myInterface = compilation.GetTypeByMetadataName($"InterfaceImplTest.{nameof(MyInterfaceToTestCircularReference)}"); //INamedTypeSymbol myInterface = // compilation.GetTypeByMetadataName($"TestProj.{nameof(IMyInterface)}"); IAssemblySymbol assembly = myInterface.ContainingAssembly; ISourceAssemblySymbol sourceAssemblySymbol = assembly as ISourceAssemblySymbol; IAssemblySymbol assemblySymbol = assembly as IAssemblySymbol; IEnumerable <MetadataReference> references = sourceAssemblySymbol .Compilation .References .Concat ( new[] { sourceAssemblySymbol .Compilation .ToMetadataReference() }).ToArray(); IPropertySymbol intProp = myInterface .GetMemberByName <IPropertySymbol>(nameof(IMyInterface.TheInt)); IPropertySymbol strProp = myInterface .GetMemberByName <IPropertySymbol>(nameof(IMyInterface.TheStr)); RoslynCodeBuilder roslynCodeBuilder = new RoslynCodeBuilder(); RoslynAnalysisAndGenerationUtils.TheNamespaces = roslynCodeBuilder.AllNamespaces; RoslynAnalysisAndGenerationUtils.TheAssemblies = roslynCodeBuilder.AllAssemblies; roslynCodeBuilder.AddNamespace("NP.Generated"); roslynCodeBuilder.AddClass("MyClass1", null, null, myInterface); roslynCodeBuilder.AddAutoProp(intProp.Name, intProp.Type as INamedTypeSymbol); roslynCodeBuilder.AddAutoProp(strProp.Name, strProp.Type as INamedTypeSymbol); roslynCodeBuilder.PopAll(); string classStr = roslynCodeBuilder.ToString(); }