为什么 Assembly.GetTypes() 需要引用?

发布于 2024-12-14 19:06:22 字数 243 浏览 2 评论 0原文

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

Assembly assembly = Assembly.LoadFrom(myAssemblyPath);
Type[] typeArray = 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.

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

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

发布评论

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

评论(4

悲欢浪云 2024-12-21 19:06:22

加载程序集还需要加载其所有依赖项,因为程序集中的代码可以在加载后执行(实际上不运行任何内容而仅反映它并不重要)。

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

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

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.

她如夕阳 2024-12-21 19:06:22

它似乎是仅获取程序集中定义的类型的重复项,其中解为:

public static Type[] GetTypesLoaded(Assembly assembly)
{
    Type[] types;
    try
    {
        types = assembly.GetTypes();
    }
    catch (ReflectionTypeLoadException e)
    {
        types = e.Types.Where(t => t != null).ToArray();
    }

    return types;    
}

It seems to be a duplicate of Get Types defined in an assembly only, where the solution is:

public static Type[] GetTypesLoaded(Assembly assembly)
{
    Type[] types;
    try
    {
        types = assembly.GetTypes();
    }
    catch (ReflectionTypeLoadException e)
    {
        types = e.Types.Where(t => t != null).ToArray();
    }

    return types;    
}
桜花祭 2024-12-21 19:06:22

为了加载程序集,有必要加载程序集的依赖项。例如,如果您的程序集包含返回 XmlNode 的类型,那么您将必须加载 System.Xml.dll

In order to load the assembly, it's necessary to load the assembly's dependencies. If, for example, your assembly contains a type that returns an XmlNode then you will have to load System.Xml.dll

真心难拥有 2024-12-21 19:06:22

使用仅反射上下文的替代方法可能是 Mono.Cecil< /a> 由 Jb Evain 提供,也可以通过 NuGet 获取。

ModuleDefinition module = ModuleDefinition.ReadModule(myAssemblyPath);
Collection<TypeDefinition> types = module.Types;

An alternative to using the reflection only context might be Mono.Cecil by Jb Evain which is also available via NuGet.

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