防止 Assembly.GetTypes() 加载依赖项

发布于 2025-01-03 06:16:53 字数 1194 浏览 2 评论 0原文

我有程序集 A 引用程序集 B。它们位于同一目录中。

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);
}

如何防止B被加载?我希望 GetTypes() 运行就像 B 不可用一样,并仅返回可用类型和 null 表示不可用,以便我可以执行 ex.Types.where(x=>x!=null);

我想使用 如何在调用 Assembly.GetTypes() 时防止 ReflectionTypeLoadException

这样我只能获取不依赖于 B 并可使用的类型 他们。

更新:

我在反射和正常上下文中加载了 A。我使用反射上下文 A 来调用 GetTypes()。从反射上下文程序集中获取类型后,我遇到了另一个问题。当我调用 Type.GetCustomAttributes() 时,收到以下异常

反映通过加载的类型的自定义属性是非法的 ReflectionOnlyGetType(参见 Assembly.ReflectionOnly)——使用 相反,使用 CustomAttributeData。

我通过从正常的上下文程序集中获取相同的类型来解决这个问题。

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

这样我就阻止了 B 的加载,并使用了 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);
}

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);

I want to use the trick from How to prevent ReflectionTypeLoadException when calling Assembly.GetTypes()

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

Update:

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

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);

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

花落人断肠 2025-01-10 06:16:53

看看使用 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.

毁梦 2025-01-10 06:16:53

现在是 2022 年,.NET 6 已经到来,因此上述解决方案已正式过时。

这是一个新的解决方案:
https://github.com/dotnet/corefxlab/blob/ master/docs/specs/typeloader.md

显然,这是一个存档的实验存储库,因此您必须嵌入 代码手动添加到您的项目中。请记下许可证。

It's 2022, and .NET 6 is here, so the solution mentioned above is officially obsolete.

Here is a new solution:
https://github.com/dotnet/corefxlab/blob/master/docs/specs/typeloader.md

Apparently, this is an archived experimental repository, so you have to embed the code into your project manually. Take note of the license.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文