为什么 Assembly.GetTypes() 需要引用? [英] Why does Assembly.GetTypes() require references?

查看:29
本文介绍了为什么 Assembly.GetTypes() 需要引用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从我的程序集中获取所有类型,但我没有引用,我也不关心它们.查找接口类型与引用有什么关系?有没有办法让我解决这个问题?

I want to get all of the types from my assembly, but I don't have the references, nor do I care about them. What does finding the interface types have to do with the references? and is there a way for me to get around this?

Assembly assembly = Assembly.LoadFrom(myAssemblyPath);
Type[] typeArray = assembly.GetTypes();

抛出:文件未找到异常无法加载文件或程序集某些引用的程序集"或其依赖项之一.系统找不到指定的文件.

Throws: FileNotFoundException Could not load file or assembly 'Some referenced assembly' or one of its dependencies. The system cannot find the file specified.

推荐答案

加载程序集也需要加载它的所有依赖项,因为程序集的代码可以在加载后执行(这并不重要)不实际运行任何东西,而只是思考它).

Loading an assembly requires all of its dependencies to be loaded as well, since code from the assembly can be executed after it's loaded (it doesn't matter that you don't actually run anything but only reflect on it).

要加载程序集以明确反映它,您需要将其加载到仅反射上下文中,例如ReflectionOnlyLoadFrom.这也不需要加载任何引用的程序集,但是您将无法运行代码,并且反射有时会变得比您习惯的更尴尬.

To load an assembly for the express purpose of reflecting on it, you need to load it into the reflection-only context with e.g. ReflectionOnlyLoadFrom. This does not require loading any referenced assemblies as well, but then you can't run code and reflection becomes a bit more awkward than what you 're used to at times.

这篇关于为什么 Assembly.GetTypes() 需要引用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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