简体   繁体   English

是否有一种简单的方法来从C#类型名称获取Type对象

[英]Is there an easy way to get Type object from C# type name

I want to create Type object from string value, eg, "int", "long". 我想从字符串值(例如“ int”,“ long”)创建Type对象。 I know I can create Type object from .net framework type name, like Type t = Type.GetType("System.Int32"). 我知道我可以从.net框架类型名称创建Type对象,例如Type t = Type.GetType(“ System.Int32”)。

Is there a built-in way to do same from C# type, "int", "long". 是否有内置的方法可以从C#类型“ int”,“ long”执行相同的操作。

The .Net framework itself (including the Reflection library) is completely unaware of C#'s type keywords. .Net框架本身(包括反射库)完全不了解C#的类型关键字。
Therefore, you'll need to do this youself. 因此,您需要自己执行此操作。

You can create a Dictionary<string, Type> . 您可以创建一个Dictionary<string, Type>

检查反射

鉴于我不明白您的问题,这里是以前询问此问题的链接

typeof(int), typeof(string);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM