Dynamic Keyword and Dotfuscator = Very Unhappy

Posted on Oct 03, 2011

I’ve been using Dotfuscator to obfuscate an assembly. I thought all was good, but when I tried to use the obfuscate assembly, the assembly didn’t work like the unobfuscated one did and was throwing exceptions all over the place. After much debugging, I finally discovered that the use of the dynamic keyword was causing bigtime problems with Dotfucator.

Understanding what is going on with a simple sample is useful. Say I have the following method to be obfuscated:

staticvoid MyMethod()
{
int num =1;
int i = DoSomething(num);
}

When obfuscated and then inspected in Reflector, here’s what it looks like:

privatestaticvoid a()
{
int num =1;
int num2 = a(num);
}

That’s what you’d expect. But, now let’s say you change that method to use dynamic types instead of int. So it looks like this:

staticvoid MyMethod()
{
dynamic num =1;
int i = DoSomething(num);
}

When obfuscated and then disassembled in Reflector, here’s what you get:

Eek! Not only is it ugly as can be, look at how the method name DoSomething is passed as a string to the InvokeMember method. But, DoSomething has been ofbuscated as a, so the assembly no longer has any notion of the DoSomething name for that method.

I’m not the only one who’s hit this as you can see from this post in the Dotfuscator forums. Unfortunately, I couldn’t find a workaround other than removing the use of the dynamic keyword.