是有可能使用Type.GetType带有动态加载组件? [英] Is it possible to use Type.GetType with a dynamically loaded assembly?
问题描述
说我有这个代码一点:
公共静态无效LoadSomething(T型)
{
VAR T1 = Type.GetType(t.AssemblyQualifiedName);
变种T2 = T
.Assembly
.GetTypes()
。首先(TA => ta.AssemblyQualifiedName == t.AssemblyQualifiedName);
}
什么情况是,T1是的无效和t2是的不为空。我很困惑,因为如果我把它像这样...
LoadSomething(typeof运算(SomeObject));
和没有的空但我实际上做的是更多像这样的(不是真的,这是大规模简化,但它说明了我的观点):
LoadSomething(Assembly.LoadFile(@C:\\ \\ .... DLL)GetTypes()一());
所以我的问题的第一部分(我的信息)是...
在第二种情况下,因为大会必须加载了,我找到的类型了吧,为什么 Type.GetType
回归空?
和第二(实际上解决我的问题)...
有一些其他的方式,当我只有装配合格的名称作为一个字符串(我知道先前已使用的Assembly.Load方法加载)我可以加载一个类型?
有没有办法,我可以加载一个类型时,我只有
装配合格的名称作为字符串(据我所知以前$ b了一些其他的方式$ b使用的Assembly.Load方法加载)?
块引用>
是的。有一个
的GetType
重载允许。它需要一个集结号解析功能的参数:公共静态类型LoadSomething(字符串assemblyQualifiedName)
{
//这将返回null
//只是在这里测试该简单的GetType超载不能返回的实际类型
VAR T0 = Type.GetType(assemblyQualifiedName);
//抛出的异常的类型是没有被发现
返回Type.GetType(
assemblyQualifiedName,
(名称)= GT;
{
//通过在应用程序域
返回AppDomain.CurrentDomain.GetAssemblies(枚举加载的程序集
//)返回类型的装配在哪里(Z =方式> z.FullName == name.FullName ).FirstOrDefault();
},
空,
真);
}
私有静态无效的主要(字串[] args)
{
//动态加载程序集
VAR总成= Assembly.LoadFrom(@ C:\ ... \ClassLibrary1.dll);
//利用其装配合格的名称
VAR loadedType = LoadSomething加载类型(ClassLibrary1.Class1,ClassLibrary1的,版本= 1.0.0.0,文化=中立,公钥=空);
Console.ReadKey();
}
Say I have this little bit of code:
public static void LoadSomething(Type t) { var t1 = Type.GetType(t.AssemblyQualifiedName); var t2 = t .Assembly .GetTypes() .First(ta => ta.AssemblyQualifiedName == t.AssemblyQualifiedName); }
What happens is that t1 is null and t2 is not null. I was confused since if I call it like so...
LoadSomething(typeof(SomeObject));
then neither are null but what I am actually doing is more like this (not really, this is massively simplified but it illustrates my point):
LoadSomething(Assembly.LoadFile(@"C:\....dll").GetTypes().First());
So the first part of my question (for my information) is...
In the second case, since the assembly must be loaded up and I found the type out of it, why does
Type.GetType
return null?And secondly (to actually solve my problem)...
Is there some other way that I could load a type when I only have the assembly qualified name as a string (that I know has been previously loaded by using the Assembly.Load methods)?
解决方案Is there some other way that I could load a type when I only have the assembly qualified name as a string (that I know has been previously loaded by using the Assembly.Load methods)?
Yes. There is a
GetType
overload that allows that. It takes an "assembly resolver" function as parameter:public static Type LoadSomething(string assemblyQualifiedName) { // This will return null // Just here to test that the simple GetType overload can't return the actual type var t0 = Type.GetType(assemblyQualifiedName); // Throws exception is type was not found return Type.GetType( assemblyQualifiedName, (name) => { // Returns the assembly of the type by enumerating loaded assemblies // in the app domain return AppDomain.CurrentDomain.GetAssemblies().Where(z => z.FullName == name.FullName).FirstOrDefault(); }, null, true); } private static void Main(string[] args) { // Dynamically loads an assembly var assembly = Assembly.LoadFrom(@"C:\...\ClassLibrary1.dll"); // Load the types using its assembly qualified name var loadedType = LoadSomething("ClassLibrary1.Class1, ClassLibrary1, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"); Console.ReadKey(); }
这篇关于是有可能使用Type.GetType带有动态加载组件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!