Inheritance of the DoNotObfuscate Attribute

Andy_LAndy_L Posts: 4 New member
I have some code similar to this:
[SmartAssembly.Attributes.DoNotObfuscate]public class BaseClass {}
public class DerivedClass : BaseClass {}
When I look at the implementation of the DoNotObfuscateAttribute, I can see its AttributeUsage attribute doesn't specify a value for the Inherited parameter, so uses the default value of true, and the attribute should be inherited by derived classes.
At runtime, if I look at
then sure enough, DerivedClass has the SmartAssembly.Attributes.DoNotObfuscateAttribute attribute.

Yet when I run obfuscation, DerivedClass gets obfuscated while BaseClass doesn't.  Why does DerivedClass get obfuscated when it should inherit the DoNotObfuscateAttribute?

Is there some other way I can tell SmartAssembly not to obfuscate all classes derived from a particular base class?


Best Answer

  • Russell DRussell D Posts: 765 Rose Gold 5
    edited August 3, 2018 9:57AM Accepted Answer
    So this is certainly true. When applying [DoNotObfuscate] on a base class, it doesn't affect derived classes. 

    I'm not sure that it's a bug though. I can imagine people wanting to obfuscate all derived classes, but not the base class (as is right now). Or obfuscate the base class, but not derived classes. So its because of this that we've added an enhancement request to our backlog, one possible alternative would be to exclude classes one-by-one in GUI, but it needs some thought/testing.

    Another alternative would be to do some find-and-replace magic. For example, if I wanted to not obfuscate all classes deriving form BaseClass, I could replace \r?\n.*?class[^\r\n]+:[^\r\n]+\bBaseClass to [DoNotObfuscate]$0.

    It's a somewhat dirty regular expression. but it might work; especially with more thought.

    Sorry for the delay in response; it took a little thought.
    Have you visited our Help Centre?


  • Andy_LAndy_L Posts: 4 New member
    Thanks for the reply Russell.

    I can understand the reasoning behind the non-inheriting behaviour of the DoNotObfuscate attribute - as you say, there will be many cases where it's the desired outcome.  I think the intention would be clearer if the attribute were declared with [AttributeUsage(Inherited=false)].  Also, it would be great if there were another attribute we could use to specify inheriting don't-obfuscate behaviour.  Or even a way to exclude an entire inheritance hierarchy in the SmartAssembly UI.

    Actually I don't mind having to manually add the attribute to every derived class; what concerns me is the latent bug that's created if we add a new derived class and forget to put [DoNotObfuscate] on it.  My equally dirty workaround to guard against that has been to put some debug build-only code in the constructor of the base class, which uses reflection to check for the presence of the attribute every time a derived class is instantiated.  I think a better option would be to perform that validation as some sort of custom build step, but that has to wait for another day.
Sign In or Register to comment.