Question

I have an interface in my application, and several DLL's that extend it. The interface provides one simple method :

public interface ICustomConversion
{
    string Namespace { get; }
    string Code { get; }

    int TotalCount { get; set; }
    int ErrorCount { get; set; }

    string ConvertList(string inputFileLocation);
}

The goal is to allow multiple files to be converted to our custom format. I load up the DLL based on the user selection via reflection :

 var dll = Assembly.LoadFrom(path);
 var type = dll.GetTypes().First();
 var instance = (ICustomConversion)Activator.CreateInstance(type);
 return instance.ConvertList(filename);

This all works great in the dev environment. However, after obfuscation, it's unable to load the dll, and I'm not sure why. Using ILSpy on the obfuscated DLL shows the method signature is still the same:

ILSpy

I'm curious what the issue is here. Is reflection against the obfuscated assembly somehow breaking? Has anyone else encountered this error before? Thank you for any insight.

UPDATE

it looks like this line is throwing the exception:

var instance = (ICustomConversion)Activator.CreateInstance(type);
Was it helpful?

Solution

This line is just getting the first type declared by the DLL:

var type = dll.GetTypes().First();

This is not good practice. You should select a type based on some criteria.

Obfuscation may change the ordering of types in the assembly (perhaps due to name changes).

I recommend that you do this instead:

var type = dll.GetTypes().First(typeof(ICustomConversion).IsAssignableFrom);

That will select the first type that implements your interface.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top