代码之家  ›  专栏  ›  技术社区  ›  George Johnston

在.NET中以编程方式获取类型的别名

  •  3
  • George Johnston  · 技术社区  · 14 年前

    别名 通过反思?给出以下示例,其中 我的对象

    Type t = myObject.GetType();
    

    t、 姓名 国际32 内景 除了 国际32

    3 回复  |  直到 6 年前
        1
  •  5
  •   quentin-starin    14 年前

        2
  •  5
  •   desco    14 年前

    您可以使用CodeDom类获得特定于语言的类型别名

        var cs = new CSharpCodeProvider();
        var vb = new VBCodeProvider();
    
        var type = typeof (int);
        Console.WriteLine("Type Name: {0}", type.Name); // Int32
        Console.WriteLine("C# Type Name: {0}", cs.GetTypeOutput(new CodeTypeReference(type))); // int
        Console.WriteLine("VB Type Name: {0}", vb.GetTypeOutput(new CodeTypeReference(type))); // Integer
    
        3
  •  0
  •   siride    14 年前

    别名似乎只是编译时的。注意它们如何不应用于给定的源文件之外。它们只是方便而已。