C# Type.GetType("namespace.abClassName") 返回 null
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1825147/
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("namespace.a.b.ClassName") returns null
提问by Omu
This code:
这段代码:
Type.GetType("namespace.a.b.ClassName")
returns null
.
返回null
。
and I have in the usings:
我有以下用途:
using namespace.a.b;
Update:
更新:
The type exists, it's in a different class library, and i need to get it by string name.
该类型存在,它在不同的类库中,我需要通过字符串名称获取它。
采纳答案by DrPizza
Type.GetType("namespace.qualified.TypeName")
only works when the type is found in either mscorlib.dll or the currently executing assembly.
Type.GetType("namespace.qualified.TypeName")
仅当在 mscorlib.dll 或当前执行的程序集中找到该类型时才有效。
If neither of those things are true, you'll need an assembly-qualified name:
如果这些都不是真的,您将需要一个程序集限定名称:
Type.GetType("namespace.qualified.TypeName, Assembly.Name")
回答by Guillaume
If the assembly is referenced and the Class visible :
如果程序集被引用并且类可见:
typeof(namespace.a.b.ClassName)
GetType returns null because the type is not found, with typeof, the compiler may help you to find out the error.
因为找不到类型,所以GetType返回null,用typeof,编译器可以帮你找出错误。
回答by Ruben Bartelink
If it's a nested Type, you might be forgetting to transform a . to a +
如果它是嵌套类型,则您可能忘记将 . 到 +
Regardless, typeof( T).FullName
will tell you what you should be saying
无论如何,typeof( T).FullName
都会告诉你你应该说什么
EDIT: BTW the usings (as I'm sure you know) are only directives to the compiler at compile time and cannot thus have any impact on the API call's success. (If you had project or assembly references, that could potentially have had influence - hence the information isnt useless, it just takes some filtering...)
编辑:顺便说一句,使用(我相信你知道)只是在编译时对编译器的指令,因此不会对 API 调用的成功产生任何影响。(如果您有项目或程序集引用,那可能会产生影响 - 因此信息并非无用,只需要进行一些过滤......)
回答by erikkallen
Dictionary<string, Type> typeCache;
...
public static bool TryFindType(string typeName, out Type t) {
lock (typeCache) {
if (!typeCache.TryGetValue(typeName, out t)) {
foreach (Assembly a in AppDomain.CurrentDomain.GetAssemblies()) {
t = a.GetType(typeName);
if (t != null)
break;
}
typeCache[typeName] = t; // perhaps null
}
}
return t != null;
}
回答by Ismail Hawayel
Try using the full type name that includes the assembly info, for example:
尝试使用包含程序集信息的完整类型名称,例如:
string typeName = @"MyCompany.MyApp.MyDomain.MyClass, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null";
Type myClassType = Type.GetType(typeName);
I had the same situation when I was using only the the namesspace.classname to get the type of a class in a different assembly and it would not work. Only worked when I included the assembly info in my type string as shown above.
当我只使用 namesspace.classname 来获取不同程序集中的类的类型时,我遇到了同样的情况,但它不起作用。仅当我在我的类型字符串中包含程序集信息时才有效,如上所示。
回答by Asaf Pala
You can also get the type without assembly qualified name but with the dll name also, for example:
您还可以获取没有程序集限定名称但也带有 dll 名称的类型,例如:
Type myClassType = Type.GetType("TypeName,DllName");
I had the same situation and it worked for me. I needed an object of type "DataModel.QueueObject" and had a reference to "DataModel" so I got the type as follows:
我有同样的情况,它对我有用。我需要一个“DataModel.QueueObject”类型的对象并引用“DataModel”,所以我得到的类型如下:
Type type = Type.GetType("DataModel.QueueObject,DataModel");
The second string after the comma is the reference name (dll name).
逗号后的第二个字符串是引用名称(dll 名称)。
回答by peyman
try using this method
尝试使用这种方法
public static Type GetType(string typeName)
{
var type = Type.GetType(typeName);
if (type != null) return type;
foreach (var a in AppDomain.CurrentDomain.GetAssemblies())
{
type = a.GetType(typeName);
if (type != null)
return type;
}
return null ;
}
回答by LarryBud
If the assembly is part of the build of an ASP.NET application, you can use the BuildManager class:
如果程序集是 ASP.NET 应用程序构建的一部分,则可以使用 BuildManager 类:
using System.Web.Compilation
...
BuildManager.GetType(typeName, false);
回答by Stephan
I am opening user controls depending on what user controls the user have access to specified in a database. So I used this method to get the TypeName...
我打开用户控件取决于用户有权访问数据库中指定的用户控件。所以我用这个方法来获取TypeName...
Dim strType As String = GetType(Namespace.ClassName).AssemblyQualifiedName.ToString
Dim obj As UserControl = Activator.CreateInstance(Type.GetType(strType))
So now one can use the value returned in strType to create an instance of that object.
所以现在可以使用 strType 中返回的值来创建该对象的实例。
回答by Ozair Kafray
Thissolution above seems to be the best to me, but it didn't work for me, so I did it as follows:
上面的这个解决方案对我来说似乎是最好的,但它对我不起作用,所以我是这样做的:
AssemblyName assemblyName = AssemblyName.GetAssemblyName(HttpContext.Current.Server.MapPath("~\Bin\AnotherAssembly.dll"));
string typeAssemblyQualifiedName = string.Join(", ", "MyNamespace.MyType", assemblyName.FullName);
Type myType = Type.GetType(typeAssemblyQualifiedName);
The precondition is that you know the path of the assembly. In my case I know it because this is an assembly built from another internal project and its included in our project's bin folder.
前提是您知道程序集的路径。就我而言,我知道它是因为这是从另一个内部项目构建的程序集,并且它包含在我们项目的 bin 文件夹中。
In case it matters I am using Visual Studio 2013, my target .NET is 4.0. This is an ASP.NET project, so I am getting absolute path via HttpContext
. However, absolute path is not a requirement as it seems from MSDN on AssemblyQualifiedNames
万一我使用的是 Visual Studio 2013,我的目标 .NET 是 4.0。这是一个 ASP.NET 项目,所以我通过HttpContext
. 但是,绝对路径不是必需的,因为从MSDN 上的 AssemblyQualifiedNames看来