Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/apache/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Reflection 以编程方式获取类型';NET中的别名_Reflection - Fatal编程技术网

Reflection 以编程方式获取类型';NET中的别名

Reflection 以编程方式获取类型';NET中的别名,reflection,Reflection,是否可以通过反射确定对象类型的别名?给出以下示例,其中myObject的类型为System.Int32——例如 t.Name将是Int32。但是,我想确定相关对象的可能别名(如果有的话),以便除了Int32之外,我还可以解析int的类型名 我不想在这上面花太多时间。如果没有一个非常简单的内置解决方案,我将自己映射这些值 没有直接反射,没有。“type name”(正如您所说的)“int”根本不是一个类型名,而是一个语言关键字 当然,您可以使用字典来存储从类型名称到较短方便表单的查找。但是反射AP

是否可以通过反射确定对象类型的别名?给出以下示例,其中myObject的类型为System.Int32——例如

t.Name将是Int32。但是,我想确定相关对象的可能别名(如果有的话),以便除了Int32之外,我还可以解析int的类型名


我不想在这上面花太多时间。如果没有一个非常简单的内置解决方案,我将自己映射这些值

没有直接反射,没有。“type name”(正如您所说的)“int”根本不是一个类型名,而是一个语言关键字


当然,您可以使用字典来存储从类型名称到较短方便表单的查找。但是反射API中没有任何东西可以为您做到这一点。

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

您可以使用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

int在运行时不作为.net类型存在
    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