문제

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);
도움이 되었습니까?

해결책

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top