在Roslyn中使用泛型生成有效类型名称的技术

时间:2015-06-27 16:07:12

标签: c# reflection code-generation roslyn

我尝试了一些可能的技术,用于在运行时生成C#接口的动态代理。到目前为止,我发现罗斯林在没有太多摩擦的情况下带我走了一段路,但我在处理泛型类型方面有点困难。特别是,要解析类型名称。

我的基本工作流程是:

  • 构建使用脚手架,命名空间和类CompilationUnitSyntax
  • 检查界面是否已获得代理
  • 对于界面上的每个方法,使用MethodInfo使用MethodDeclarationSyntax构建SyntaxFactory.MethodDeclaration,目标是我的新动态类

以下是我一直困惑的问题的一个例子。在这一点上,似乎我需要解析一个字符串以获得TypeSyntax(在这种情况下为返回类型),我唯一可以接受它的地方是methodInfo.ReturnType.Name

var methodDecl = SyntaxFactory.MethodDeclaration(SyntaxFactory.ParseTypeName(methodInfo.ReturnType.Name), methodInfo.Name);

问题是SyntaxFactory.ParseTypeName期待&#39;有效&#39; C#语法类型声明,例如List<string>,但访问Name或FullName属性的形式如下:

{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}

显然不会用反引号解析,缺少尖括号等。

Reflection样式类(MethodInfo,Types)和Roslyn语法单元之间是否有更好的桥梁?我也尝试了一种纯粹的反射发射方式解决方案,但是想知道我是否可以在这里找到一个基于Roslyn的方法。

2 个答案:

答案 0 :(得分:3)

为了从泛型类型创建TypeSyntax,这个静态工厂类可能会有所帮助。您需要获取要生成的类型的泛型参数列表,但我在底部发布的工厂(也可在this gist中找到)帮助我相对轻松地获取TypeSyntax的实例。

如何使用它的示例:

// List<Dictionary<string, List<Type>>>
TypeSyntaxFactory.GetTypeSyntax(
    "List",
    TypeSyntaxFactory.GetTypeSyntax(
        "Dictionary",
        TypeSyntaxFactory.GetTypeSyntax(
            "string"
        ),
        TypeSyntaxFactory.GetTypeSyntax(
            "List",
            "Type"
        )
    )
)

我不确定处理反射输出的最佳方法是什么,但你可能只需要在&#34;`&#34;之前获取类型标识符的子字符串。符号。在我的IDE中,此符号不是类型名称的有效字符,因此可以安全地假设它是反射类型输出的一部分。

最后,这是此gist

的副本
public static class TypeSyntaxFactory
{
    /// <summary>
    /// Used to generate a type without generic arguments
    /// </summary>
    /// <param name="identifier">The name of the type to be generated</param>
    /// <returns>An instance of TypeSyntax from the Roslyn Model</returns>
    public static TypeSyntax GetTypeSyntax(string identifier)
    {
        return
            SyntaxFactory.IdentifierName(
                SyntaxFactory.Identifier(identifier)
            );
    }

    /// <summary>
    /// Used to generate a type with generic arguments
    /// </summary>
    /// <param name="identifier">Name of the Generic Type</param>
    /// <param name="arguments">
    /// Types of the Generic Arguments, which must be basic identifiers
    /// </param>
    /// <returns>An instance of TypeSyntax from the Roslyn Model</returns>
    public static TypeSyntax GetTypeSyntax(string identifier, params string[] arguments)
    {
        return GetTypeSyntax(identifier, arguments.Select(GetTypeSyntax).ToArray());
    }

    /// <summary>
    /// Used to generate a type with generic arguments
    /// </summary>
    /// <param name="identifier">Name of the Generic Type</param>
    /// <param name="arguments">
    /// Types of the Generic Arguments, which themselves may be generic types
    /// </param>
    /// <returns>An instance of TypeSyntax from the Roslyn Model</returns>
    public static TypeSyntax GetTypeSyntax(string identifier, params TypeSyntax[] arguments)
    {
        return
            SyntaxFactory.GenericName(
                SyntaxFactory.Identifier(identifier),
                SyntaxFactory.TypeArgumentList(
                    SyntaxFactory.SeparatedList(
                        arguments.Select(
                            x =>
                            {
                                if(x is GenericNameSyntax)
                                {
                                    var gen_x = x as GenericNameSyntax;
                                    return
                                        GetTypeSyntax(
                                            gen_x.Identifier.ToString(),
                                            gen_x.TypeArgumentList.Arguments.ToArray()
                                        );
                                }
                                else
                                {
                                    return x;
                                }
                            }
                        )
                    )
                )
            );
    }
}

答案 1 :(得分:2)

我创建了这种扩展方法来解决这个问题。

static class SyntaxExtensions
{
    /// <summary>
    /// Generates the type syntax.
    /// </summary>
    public static TypeSyntax AsTypeSyntax( this Type type )
    {
        string name = type.Name.Replace( '+', '.' );

        if ( type.IsGenericType ) {
            // Get the C# representation of the generic type minus its type arguments.
            name = name.Substring( 0, name.IndexOf( "`" ) );

            // Generate the name of the generic type.
            var genericArgs = type.GetGenericArguments();
            return SyntaxFactory.GenericName( SyntaxFactory.Identifier( name ),
                SyntaxFactory.TypeArgumentList( SyntaxFactory.SeparatedList( genericArgs.Select( AsTypeSyntax ) ) )
            );
        } else
            return SyntaxFactory.ParseTypeName( name );
    }
}