我怎样才能得到使用反射泛型类型的正确的文本定义? [英] How can I get the correct text definition of a generic type using reflection?

查看:166
本文介绍了我怎样才能得到使用反射泛型类型的正确的文本定义?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的工作code代跑与泛型一个障碍。这是什么原因造成了我的问题的简化版本。

 词典<字符串,日期>词典=新词典<字符串,日期时间及GT;();
字符串文本= dictionary.GetType()全名。

通过上面的code段文本值如下:

System.Collections.Generic.Dictionary`2 [System.String,mscorlib程序,版本= 2.0.0.0,
 文化=中性公钥= b77a5c561934e089],[System.DateTime的,mscorlib中,
 版本= 2.0.0.0,文化=中性公钥= b77a5c561934e089]

(增加了更好的可读性换行。)

有没有办法让不同的格式类型名称(键入),而不分析上面的字符串?我渴望为正文如下结果

System.Collections.Generic.Dictionary< System.String,System.DateTime的>


解决方案

有没有内置的方式来获得在.Net框架这种重新presentation。也就是说,因为没有办法得到它正确的。有相当数量未在C#风格的语法重新presentable构造。例如其中p>富是IL有效的类型名称,但不能被重新在C#中psented $ P $。

不过,如果你正在寻找一个pretty很好的解决方案,可以实现手很快。下面的解决方案将在大多数情况下工作。它不会处理


  1. 嵌套类型

  2. 非法C#名称

  3. 的其他情形情侣

例如:

 公共静态字符串GetFriendlyTypeName(类型类型){
    如果(type.IsGenericParameter)
    {
        返回type.Name;
    }    如果(!type.IsGenericType)
    {
        返回type.FullName;
    }    VAR建设者=新System.Text.StringBuilder();
    变量名称= type.Name;
    VAR指数= name.IndexOf(`);
    builder.AppendFormat({0} {1},type.Namespace,name.Substring(0,指数));
    builder.Append('<');
    VAR第一= TRUE;
    的foreach(在type.GetGenericArguments VAR ARG())
    {
        如果(!第一)
        {
            builder.Append(,);
        }
        builder.Append(GetFriendlyTypeName(ARG));
        第一= FALSE;
    }
    builder.Append('>');
    返回builder.ToString();
}

I am working on code generation and ran into a snag with generics. Here is a "simplified" version of what is causing me issues.

Dictionary<string, DateTime> dictionary = new Dictionary<string, DateTime>();
string text = dictionary.GetType().FullName;

With the above code snippet the value of text is as follows:

 System.Collections.Generic.Dictionary`2[[System.String, mscorlib, Version=2.0.0.0, 
 Culture=neutral, PublicKeyToken=b77a5c561934e089],[System.DateTime, mscorlib, 
 Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]

(Line breaks added for better readability.)

Is there a way to get the type name (type) in a different format without parsing the above string? I desire the following result for text:

System.Collections.Generic.Dictionary<System.String, System.DateTime>

解决方案

There is no built-in way to get this representation in the .Net Framework. Namely because there is no way to get it correct. There are a good number of constructs that are not representable in C# style syntax. For instance "<>foo" is a valid type name in IL but cannot be represented in C#.

However, if you're looking for a pretty good solution it can be hand implemented fairly quickly. The below solution will work for most situations. It will not handle

  1. Nested Types
  2. Illegal C# Names
  3. Couple of other scenarios

Example:

public static string GetFriendlyTypeName(Type type) {
    if (type.IsGenericParameter)
    {
        return type.Name;
    }

    if (!type.IsGenericType)
    {
        return type.FullName;
    }

    var builder = new System.Text.StringBuilder();
    var name = type.Name;
    var index = name.IndexOf("`");
    builder.AppendFormat("{0}.{1}", type.Namespace, name.Substring(0, index));
    builder.Append('<');
    var first = true;
    foreach (var arg in type.GetGenericArguments())
    {
        if (!first)
        {
            builder.Append(',');
        }
        builder.Append(GetFriendlyTypeName(arg));
        first = false;
    }
    builder.Append('>');
    return builder.ToString();
}

这篇关于我怎样才能得到使用反射泛型类型的正确的文本定义?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆