I am trying to use several possible methods to create a dynamic C # interface proxy at runtime. Until now, I have found that Roslyn took my detachment without much friction, but I was a bit stuck in communicating with the generic types. In particular, getting type names for parsing.
My main workflow:
- Create scaffolding to use, namespace and class like
CompilationUnitSyntax - Verify that the interface is proxied.
- For each method on the interface, use
MethodInfo to build MethodDeclarationSyntax using SyntaxFactory.MethodDeclaration to create a new dynamic class
Here is an example of a problem that I am puzzling. At the moment it seems that I need to parse the string to get TypeSyntax (in this case for the return type), and the only place I can take is from methodInfo.ReturnType.Name :
var methodDecl = SyntaxFactory.MethodDeclaration(SyntaxFactory.ParseTypeName(methodInfo.ReturnType.Name), methodInfo.Name);
Problem SyntaxFactory.ParseTypeName expects "valid" declarations of C # syntax type, such as List<string> , but access to the Name or FullName properties takes the form:
{Name = "List`1" FullName = "System.Collections.Generic.List`1[[UnitTests.SamplePoco, UnitTests, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]]"} System.Type {System.RuntimeType}
which, obviously, will not be parsed, with reverse windows, no angle brackets, etc.
Is there a better bridge between Reflection style classes (MethodInfo, Types) and Roslyn syntactic units? I am also trying to find a solution with a clean reflection, but would like to see if I can get Roslin coming here.
source share