C#的Assembly.Load VS Assembly.ReflectionOnlyLoad [英] C# Assembly.Load vs Assembly.ReflectionOnlyLoad

查看:618
本文介绍了C#的Assembly.Load VS Assembly.ReflectionOnlyLoad的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想了解的Assembly.Load和Assembly.ReflectionOnlyLoad之间的差异。

I'm trying to understand the differences between Assembly.Load and Assembly.ReflectionOnlyLoad.

在下面的代码中,我试图找到所有的对象鉴于大会,从给定界面继承:

In the code below I am attempting to find all of the objects in a given assembly that inherit from a given interface:

var myTypes = new List<Type>();

var assembly = Assembly.Load("MyProject.Components");

foreach (var type in assembly.GetTypes())
{
   if (type.GetInterfaces().Contains(typeof(ISuperInterface)))
   {
      myTypes.Add(type);
   }
}

此代码工作对我很好,但我做一些研究到其他可能更好的替代品,并碰到Assembly.ReflectionOnlyLoad()方法。

This code works fine for me, but I was doing some research into other possibly better alternatives and came across Assembly.ReflectionOnlyLoad() method.

我以为,因为我不加载或执行任何对象,本质上只是查询自己的定义,我可以用ReflectionOnlyLoad了轻微的性能提高...

I assumed that since I'm not loading or executing any of the objects, essentially just querying on their definitions that I could use ReflectionOnlyLoad for a slight performance increase...

但事实证明,当我改变的Assembly.Load到Assembly.ReflectionOnlyLoad我得到的以下错误,当它调用assembly.GetTypes():

But it turns out that when I change Assembly.Load to Assembly.ReflectionOnlyLoad I get the following error when it calls assembly.GetTypes():

System.Reflection.ReflectionTypeLoadException:
无法加载一个或多个在
要求的类型。检索更多
信息
LoaderExceptions属性。

System.Reflection.ReflectionTypeLoadException: Unable to load one or more of the requested types. Retrieve the LoaderExceptions property for more information.

我认为上面的代码只是在做反思和看图书馆......但是这是某种形式的海森堡测不准原理的实例,由此看图书馆和它的对象实际上是试图实例化它们以某种方式?

I assumed that the above code was JUST doing reflection and "looking at" the library... but is this some sort of instance of the Heisenberg Uncertainty Principle whereby looking at the library and the objects in it is actually attempting to instantiate them in some way?

谢谢,
。最大

Thanks, Max

推荐答案

根据乔恩的答复,这将有助于了解什么是在 LoaderExceptions 。代替这个信息,我想我可以大胆地猜测。从 MSDN

As per Jon's reply, it would be helpful to know what's in LoaderExceptions. In lieu of this information, I think I can hazard a guess. From MSDN:

如果装配有相依,
ReflectionOnlyLoad方法并不
加载它们。如果您需要检查
他们,你必须自己加载它们。

If the assembly has dependencies, the ReflectionOnlyLoad method does not load them. If you need to examine them, you must load them yourself.

您需要一个处理程序附加到 AppDomain.ReflectionOnlyAssemblyResolve 来帮助CLR加载你加载程序集的任何依赖。你有没有这样做呢?

You need to attach a handler to AppDomain.ReflectionOnlyAssemblyResolve to help the CLR load any dependencies of the assembly you're loading. Have you done this?

这篇关于C#的Assembly.Load VS Assembly.ReflectionOnlyLoad的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆