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.

Community
  • 1
  • 1
Ben
  • 597
  • 1
  • 6
  • 19

4 Answers4

2

I came across the same issue today. Here's what I came up with, based on one of the cases where I've hit this in the past:

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();
ChaseMedallion
  • 20,860
  • 17
  • 88
  • 152
1

It seems all public members of System.Reflection.Assembly are virtual, so something like this may suit your purposes:

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

var assembly = new DodgyAssembly();
Blisco
  • 601
  • 6
  • 14
0

At the point where you create an instance of the outlook class, do you have a try block around it to get a stacktrace? also, does the application run as a client application? if so, the user may not have outlook installed?

Christian Phillips
  • 18,399
  • 8
  • 53
  • 82
  • I don't have a try/catch right now. I need to add one but I want to specifically log/track the information that can be gathered from a ReflectionTypeLoadException. Since I can't reproduce the problem on my machine I cannot evaluate my code to make sure I am trapping what I need and I am also curious so see what this specific reflection type provides. The user does have outlook installed. – Ben Jul 26 '13 at 13:36
  • Take a look here to see the properties http://msdn.microsoft.com/en-us/library/system.reflection.reflectiontypeloadexception.aspx – Christian Phillips Jul 26 '13 at 18:26
  • I have reviewed all that. I can write plenty of code to pull out all the information I would need from the exception but what I can't do is actually trigger one of these exceptions so I can validate my code. I cannot reproduce the problem so I am looking for a way to do that. I also don't have access to create sample applications for testing which I could run on the users machine. How could I create an assembly with a bug in it such that calling GetTypes on it would throw this exception. – Ben Jul 26 '13 at 20:55
0

You should be able to just throw a ReflectionTypeLoadException and use that to test. Obviously you should probably throw a more realistic example for your particular case, but this will simulate that exception type.

try
{
    var test = 10;
    throw new ReflectionTypeLoadException(new Type[] { test.GetType() }, new Exception[] { new FileNotFoundException() });
}
catch (ReflectionTypeLoadException ex)
{
   var whatIsTheException = ex;
}
cgotberg
  • 2,045
  • 1
  • 18
  • 18
  • I can do this but I would like to simulate a real example where the ReflectionTypeLoadException is thrown as a true exception rather than as a code generated one. – Ben Jul 26 '13 at 13:37
  • Put a typo in the name of the type where you're trying to load the type via reflection. One guess is that it can't find the file your looking for in the type loader. – cgotberg Jul 26 '13 at 13:40