加载依赖prevent Assembly.GetTypes() [英] Prevent Assembly.GetTypes() from loading dependencies

查看:189
本文介绍了加载依赖prevent Assembly.GetTypes()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有件A引用组件B.它们都位于同一目录下。

I have assembly A referencing assembly B. They are located in the same directory.

Type[] types = null;
try
{
  Assembly a = Assembly.Load("A.dll");
  types = a.GetTypes(); // loads B implicitly.
}
catch(ReflectionTypeLoadException ex)
{
  types = ex.Types.Where(x=>x!=null);
}

我如何从prevent B移动装?我想GetTypes()来运行,如果B为不可用,并仅返回可用的类型和无效的为不可用,所以我可以执行 ex.Types.where(X =>!x = NULL);

How do I prevent B from being loaded? I want GetTypes() to run as if B wasn't available and to return only available types and null's for not available so I could execute ex.Types.where(x=>x!=null);

我想要使用的伎俩<一href="http://stackoverflow.com/questions/7889228/how-to-$p$pvent-reflectiontypeloadexception-when-calling-assembly-gettypes">How以prevent ReflectionTypeLoadException调用Assembly.GetTypes时()

这样我可以只得到了不依赖于B和与他们的工作类型。

This way I can get only types that don't depend on B and work with them.

更新:

我在反射以及在正常的上下文加载的。我用反射上下文中的调用GetTypes()。从反射背景下组装得到类型后,我有另外一个问题。当我打电话Type.GetCustomAttributes(),我收到了以下异常

I loaded A in the reflection as well as in the normal context. I used the reflection context A for calling GetTypes(). After getting types from the reflection context assembly, I had another problem. When I called Type.GetCustomAttributes(), I received the following exception

这是非法的,以反映在Type通过加载自定义属性   ReflectionOnlyGetType(见Assembly.ReflectionOnly) - 使用   一个CustomAttributeData代替。

It is illegal to reflect on the custom attributes of a Type loaded via ReflectionOnlyGetType (see Assembly.ReflectionOnly) -- use CustomAttributeData instead.

我通过正常的情况下组装得到相同类型的解决了这个问题。

I solved it by getting the same type from the normal context assembly.

//... code omited for brevity
Type reflectionType = reflectionAssembly.GetTypes()[0];
//... code omited for brevity
Type goodType = normalAssembly.GetType(reflectionType.FullName);

这样我prevented装B的和从B。使用的类型从一个独立的

This way I prevented loading of B and used types from A independent from B.

推荐答案

看看使用 Assembly.ReflectionOnlyLoad()看起来不加载的依赖。不知道这是否会返回引用的依赖或没有,因为我还没有尝试过这样做的类型。

look at using Assembly.ReflectionOnlyLoad() which looks to not load dependencies. not sure if it will return types that reference that dependency or not as i've not tried to do it before.

这篇关于加载依赖prevent Assembly.GetTypes()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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