C# Type.GetType 不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12898282/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Type.GetType not working
提问by Daffi
I just noticed kind of a bug in the function:
我刚刚注意到函数中的一个错误:
Type.GetType("System.Uri");
The return value is null whereas the following functions are working quite well...
返回值为空,而以下函数运行良好...
Type.GetType("System.string");
Type.GetType("System.bool");
Type.GetType("System.DateTime");
...
...
Anyone knows, why the returned Type is null?
任何人都知道,为什么返回的类型为空?
EDIT: removed Uri double entry...
编辑:删除了 Uri 双项...
采纳答案by theMayer
The reason that Type.GetType("System.Uri")returns nullis that the type is located in system.dllinstead of mscorlib.dll. You must use the assembly-qualified name as noted above.
Type.GetType("System.Uri")返回的原因null是该类型位于system.dll而不是mscorlib.dll。您必须使用如上所述的程序集限定名称。
From MSDN:
来自 MSDN:
typeNameType: System.String
The assembly-qualified name of the type to get. See AssemblyQualifiedName. If the type is in the currently executing assembly or in Mscorlib.dll, it is sufficient to supply the type name qualified by its namespace.
typeName类型:System.String
要获取的类型的程序集限定名称。请参阅AssemblyQualifiedName。如果类型在当前执行的程序集中或 Mscorlib.dll 中,则提供由其命名空间限定的类型名称就足够了。
回答by CodeCaster
Gets the Type with the specified name, performing a case-sensitive search.
Return Value
Type: System.Type
The type with the specified name, if found; otherwise, null.
获取具有指定名称的 Type,执行区分大小写的搜索。
返回值
类型:System.Type
具有指定名称的类型(如果找到);否则,为空。
So, if you make a typo, your type will not be found and nullwill be returned. This is not a bug.
因此,如果您打错了字,您的类型将不会被找到并且null会被返回。这不是一个错误。
回答by Igal Tabachnik
Without additional information, I would guess you're not using the fully-qualified type name. Type.GetType()not only expects a fully qualified type name (i.e. System.String), but also the assembly-qualified name, in case you're trying to load anything other than a currently executing assembly type.
如果没有其他信息,我猜您没有使用完全限定的类型名称。Type.GetType()不仅需要完全限定的类型名称(即System.String),还需要程序集限定的 name,以防您尝试加载当前正在执行的程序集类型以外的任何内容。
回答by x4rf41
try this code:
试试这个代码:
Uri uri = new Uri("http://test");
Type t = Type.GetType(uri.GetType().AssemblyQualifiedName);
and then u can copy/paste the AssemblyQualifiedName from the type
然后你可以从类型中复制/粘贴 AssemblyQualifiedName
another method would be:
另一种方法是:
Type t = typeof(Uri);
回答by Alistair Gillanders
I also hit this problem and realized that, especially in ASP.Net with JIT compilation, I do not always know the Assembly information. I added the following to my ReflectionUtilities class. It is a "sledgehammer to crack a nut" to some degree but it works with both the AssemblyQualifiedName and the basic class FullName. The first basically short-circuits the search of the CurrentDomainAssemblies that must otherwise occur.
我也遇到了这个问题并意识到,尤其是在带有 JIT 编译的 ASP.Net 中,我并不总是知道程序集信息。我在我的 ReflectionUtilities 类中添加了以下内容。它在某种程度上是“敲碎坚果的大锤”,但它适用于 AssemblyQualifiedName 和基本类 FullName。第一个基本上短路了 CurrentDomainAssemblies 的搜索,否则必须发生。
public static Type FindType(string qualifiedTypeName)
{
Type t = Type.GetType(qualifiedTypeName);
if (t != null)
{
return t;
}
else
{
foreach (Assembly asm in AppDomain.CurrentDomain.GetAssemblies())
{
t = asm.GetType(qualifiedTypeName);
if (t != null)
return t;
}
return null;
}
}
Note: Given Reflection performance issues this should not be called inside loops without the assembly qualification if at all possible. Better to access the first item you need, extract the assembly information from that, and procede from there. Not always appropriate but much more efficient (if anything in Reflection can be called efficient:-)).
注意:考虑到反射性能问题,如果可能的话,不应该在没有装配资格的情况下在循环内调用。最好访问您需要的第一个项目,从中提取装配信息,然后从那里继续。并不总是合适但效率更高(如果反射中的任何东西都可以称为高效:-))。
Alistair
阿利斯泰尔

