Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Exclude constructor from obfuscation in .NET

There is ObfuscationAttibute in .NET. But I don't understand, how to exclude code inside constructor from obfuscation.

// Obfuscated class
class MyClass {
    [Obfuscation(Exclude = true)] // "Attribute 'Obfuscation' is not valid on this declaration type"
    public MyClass() {
        //some code, I need to exclude this code from obfuscation
    }
    // Obfuscated method
    public void Method1() {
        //some code
    |
    // Obfuscated method
    public void Method2() {
        //some code
    |
}

UPD: The question is NOT about renaming constructor. It's name obviously became ".ctor". I need to prevent obfuscation of code itself. Yes, some obfuscators not only rename symbols, but change code, too. Yes, I know I can't do it with this attribute. Compiler says the same. I already know what I cannot do. I am asking what i can do, prefferably using only standard .net instruments.

like image 570
Alex Butenko Avatar asked Jun 21 '26 07:06

Alex Butenko


1 Answers

You can do what you want using only ObfuscationAttribute, but it's tedious: apply [Obfuscation(ApplyToMembers=false)] to the class, and [Obfuscation] to every individual member except the constructor.

Alternatively, use your obfuscator's configuration to exclude the constructor from consideration. Since ObfuscationAttribute offers only very limited control (just turning features on and off, basically) most have separate configuration for fine-grained control.

Finally, consider making your constructor so simple and uninteresting that it doesn't matter if the flow is obfuscated or not. This should ideally be the case anyway -- if your constructor only performs member initialization, there isn't much to obfuscate in the first place. You can call member functions for the more involved stuff, and you can control obfuscation of those using the attribute.

like image 55
Jeroen Mostert Avatar answered Jun 23 '26 22:06

Jeroen Mostert



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!