如何从此字符串中删除程序集信息?

时间:2017-06-20 22:53:44

标签: c#

我有以下代码。它返回汇编信息,因为它是closed generic type

List<String> list = new List<String>();
t = list.GetType();
Console.WriteLine(t.FullName);

打印以下内容:

//System.Collections.Generic.List`1[[System.String, mscorlib,
//Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]

我知道我可以写出在&#34; [[&#34;和&#34;]]&#34;删除版本,文化和publickeytoken等属性。我想知道是否有更清洁的方法来删除这些属性?

编辑: 我想要的输出是

//System.Collections.Generic.List`1[[System.String, mscorlib]], mscorlib

1 个答案:

答案 0 :(得分:0)

像这样:

internal class Program {

    public static void Main(string[] args) {

        List<String> list = new List<String>();
        Type t = list.GetType();

        string yourString = string.Format(
                                          "{0}.{1}.[[{2}, {3}]], {3}",
                                          t.Namespace,
                                          t.Name,
                                          t.GetGenericArguments()[0],
                                          t.Assembly.GetName().Name);

        Console.WriteLine(yourString);

        // This prints :
        //System.Collections.Generic.List`1[[System.String, mscorlib]], mscorlib
    }

}