如何reproducably事业/挑起ReflectionTypeLoadException? [英] How to reproducably cause / provoke a ReflectionTypeLoadException?

查看:163
本文介绍了如何reproducably事业/挑起ReflectionTypeLoadException?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可悲的是,引用此异常通常是异国情调的性质,当你如可能发生枚举通过 Assembly.GetTypes类型() - 典型的例子,它发生在一个我们的部署,但同一组组件的做工精细的集成服务器上。

Sadly, references to this exception are usually of an exotic nature and can happen when you e.g. enumerate Types via Assembly.GetTypes() - case in point, it is happening on one of our deployments but the same set of assemblies work fine on the Integration Server.

为了强化针对这种类型的错误,我想能够激起这种例外,看看我的异常处理code正常工作。

In order to harden against this type of error I would like to be able to provoke such an exception to see if my exception handling code works as expected.

因此​​任何指针将是有用的,例如只知道什么样的事情会导致此异常。

Hence any pointers would be useful, e.g. just knowing what kind of things cause this exception.

<打击> 修改 我试图至今:

  • 定义一个使用程序集B的属性类型中集A
  • 拥有对所有类型中说,大会的一些code迭代 被指定的属性。
  • 在我建立只有当DEBUG标志是真正的属性
  • 在我然后复制相关的发布版本到相关的文件夹
  • Define a type in assembly A which uses an attribute of assembly B
  • Have some code iterate over all types in the assembly where said attribute is specified.
  • I build the attribute only when DEBUG flag is true
  • I then copy the release build of that dependency into the relevant folder

不过,我只设法得到一个 TypeLoadException 以pretty的明确的错误信息

But I only manage to get a TypeLoadException with a pretty clear error message

推荐答案

以下原因此异常:

您必须装配一个定义了以下类:

You have assembly A which defines the following class:

public class AC
{
    public BC GetBC() { /* ... */ }
}

您有集B定义的类 BC 。 现在,当你加载程序集A和获得 AC的类成员,例如使用 assembly.GetTypes()的SelectMany(T =&GT;:T 。.GetMembers())了ToList(); ,该框架尝试解析 BC 。它甚至知道它是在组装 B 。但是,如果框架解决装配 B 不包含 BC ,一个 TypeLoadException 将被抛出。这可能发生,如果装配 B 是达不到最新的,因为你在部署忘了。

You have assembly B which defines the class BC. Now, when you load Assembly A and get the members of class AC, for example using assembly.GetTypes().SelectMany(t => t.GetMembers()).ToList();, the framework tries to resolve BC. It even knows that it is in assembly B. However, if the framework resolves an assembly B that doesn't contain BC, a TypeLoadException will be thrown. This can happen, if assembly B is not up-to-date, because you forgot it in your deployment.

更新:
要真正获得 ReflectionTypeLoadException ,情景非常相似。但是,你并不需要在 C 的回报 BC 的方法,但你需要获得 C BC

UPDATE:
To actually get a ReflectionTypeLoadException, the scenario is very similar. However, you don't need to have a method in AC that returns BC but you need to derive AC from BC:

public class AC : BC
{
}

使用 LoaderExceptions 属性,可以检索导致这种 ReflectionTypeLoadException 例外。就我而言,这是一个 TypeLoadException 说明到底是什么类型它无法加载。

Using the LoaderExceptions property, you can retrieve the exceptions that lead to this ReflectionTypeLoadException. In my case, this is a TypeLoadException stating exactly what type it couldn't load.

这篇关于如何reproducably事业/挑起ReflectionTypeLoadException?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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