如何获取已定义文本的类型

Isl*_*him 0 .net c# visual-studio-2010

我可以在C#中这样做:

string str="string";
Type typ=typeof(str);
Run Code Online (Sandbox Code Playgroud)

任何人都可以帮助我创建通用类型转换器而无需多个条件

Luk*_*oid 5

你能得到的最接近的是使用Type.GetType(System.String).

这需要使用程序集限定名称,除非在mscorlib中定义了类型,您将只能使用名称空间限定名称.

例如,对于来自mscorlib的类:

Type stringType = Type.GetType("System.String");
Run Code Online (Sandbox Code Playgroud)

或者不是来自mscorlib的类:

Type dependencyPropertyType = Type.GetType("System.Windows.DependencyProperty, WindowsBase, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35");
Run Code Online (Sandbox Code Playgroud)