且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

字符串转换为键入C#

更新时间:2023-02-12 17:54:50

您只能使用的只是的(当然,它的命名空间,)类型的名称,如果该类型是的mscorlib 或调用程序集。否则,你必须包括程序集的名称,以及:

You can only use just the name of the type (with its namespace, of course) if the type is in mscorlib or the calling assembly. Otherwise, you've got to include the assembly name as well:

Type type = Type.GetType("Namespace.MyClass, MyAssembly");

如果该程序集强命名,你一定要包括所有的信息了。请参阅 Type.GetType(字符串) 了解更多信息。

If the assembly is strongly named, you've got to include all that information too. See the documentation for Type.GetType(string) for more information.

另外,如果你有一个参考的装配体已经(例如,通过一个著名的类型),可以使用的 Assembly.GetType

Alternatively, if you have a reference to the assembly already (e.g. through a well-known type) you can use Assembly.GetType:

Assembly asm = typeof(SomeKnownType).Assembly;
Type type = asm.GetType(namespaceQualifiedTypeName);