2

I have a user in production who is getting a ReflectionTypeLoadException while trying get the assembly types from an Outlook interop assembly. I need to code the application to better debug the problem but I cannot reproduce his issue so I have no way of testing the code to make sure it is giving me what I need.

I found this post: Error message 'Unable to load one or more of the requested types. Retrieve the LoaderExceptions property for more information.' which has sample code but I would like to debug through it to make sure it works for me and to see how it works.

Here is my code that throws. I have the actual assembly loaded. It is when I enumerate the types contained within that I get the exception:

Type t = assembly.GetTypes().Where(x => x.IsClass && x.Name.Equals("ApplicationClass")).FirstOrDefault();

Can anyone provide a sample or some insight into how I may simulate this problem so I can validate the code that I need to write?

Thanks.

4

4 回答 4

2

我今天遇到了同样的问题。这是我想出的,基于我过去遇到的一个案例:

var assemblyName = new AssemblyName("TestAssembly");
var assemblyBuilder = AppDomain.CurrentDomain.DefineDynamicAssembly(assemblyName, AssemblyBuilderAccess.Run);
var module = assemblyBuilder.DefineDynamicModule("MainModule");
var typeBuilder = module.DefineType(name: "TestType", attr: TypeAttributes.Public | TypeAttributes.Class);
// uncommenting this stops the error. Basically, GetTypes() seems to fail if the ModuleBuilder
// has an "unfinished" type
//typeBuilder.CreateType();
module.Assembly.GetTypes();
于 2014-02-14T15:37:26.103 回答
1

似乎所有公共成员System.Reflection.Assembly都是虚拟的,所以这样的事情可能适合您的目的:

public class DodgyAssembly : Assembly
{
    public override Type[] GetTypes()
    {
        throw new ReflectionTypeLoadException(new [] { typeof(Foo) }, new [] { new Exception() });
    }
}

var assembly = new DodgyAssembly();
于 2016-11-29T10:31:48.857 回答
0

您应该能够只抛出一个 ReflectionTypeLoadException 并使用它进行测试。显然,您可能应该为您的特定情况抛出一个更现实的示例,但这将模拟该异常类型。

try
{
    var test = 10;
    throw new ReflectionTypeLoadException(new Type[] { test.GetType() }, new Exception[] { new FileNotFoundException() });
}
catch (ReflectionTypeLoadException ex)
{
   var whatIsTheException = ex;
}
于 2013-07-26T13:28:51.847 回答
0

在创建 Outlook 类的实例时,是否有一个 try 块围绕它来获取堆栈跟踪?此外,该应用程序是否作为客户端应用程序运行?如果是这样,用户可能没有安装 Outlook?

于 2013-07-26T13:22:20.937 回答