Search code examples
c#reflectionobfuscation

Is there a way to do a conditional check against whether your code is obfuscated in C#?


Is there a way to check whether your code base is obfuscated programmatically?

In my project, I'm using reflection to load classes from an outside DLL (which I control). The data for loading the information (assembly and class name) are stored in a resource file.

Once I obfuscate the project, I am unable to link the outside DLL into the output EXE (i.e. roll up all assemblies into a single output executable), because the assembly name in the resource file is no longer correct (the DLL has become part of the EXE). But I have to preserve the un-obfuscated functionality as well for debugging purposes.

What I'd like to do, is have the code check to see if it has been obfuscated at runtime. If it hasn't, use the assembly name in the resource file; if it has, use the name of the application. Is something like this possible?


Solution

  • Most Obfuscators, such as Dotfuscate, will put an assembly level Custom Attribute on the assembly. If that is the case of your obfuscator, check to see if the attribute is present, such as:

    bool foundObfuscatationAttribute = false;
    foreach (object attribute in attributes)
    {
        if (attribute.GetType().FullName == "DotfuscatorAttribute")
        {
            foundObfuscatationAttribute = true;
            break;
        }
    }
    //If found...
    

    Since we don't want to reference the assembly that contains the attribute itself, we check by name.