Private field name is not mangled in merged assembly

iblazhko-twiblazhko-tw Posts: 6
edited February 16, 2015 2:35AM in SmartAssembly
I have following code in a dependency assembly:
using AppDomainTest.Contracts;
using AppDomainTest.Implementation2;
using System;

namespace AppDomainTest.Implementation3
{
    [Serializable]
    public class ServiceInAppDomain : ICalculatorService
    {
        private readonly ServiceEmbedded implementation = new ServiceEmbedded();
        
        public double Add(double a, double b)
        {
            return implementation.Add(a, b);
        }
    }
}

Note the private field "implementation ".

This dependency is merged into main application and obfuscated:
...
<Options>
  ...
  <Obfuscation FieldsNameMangling="2" NameMangling="1" />
  ...
</Options>
...
<Assembly AssemblyName="AppDomainTest.Implementation3, Culture=neutral, PublicKeyToken=null">
  <Merging Merge="1">
  <Obfuscation ExcludePublicMembers="0" Obfuscate="1" />
  <ControlFlow Obfuscate="1" />
  </Merging>
</Assembly>

After the obfuscation name of the private field "implementation" is not mangled:
https://www.dropbox.com/s/w64ibwonkf1nh0v/PrivateFieldNotMangled.png?dl=0
I would expect the name to be obfuscated, it is only used once as displayed in the snippet above, no access by reflection or anything like this.

Is there something I am missing from obfuscation configuration?

I was using SmartAssembly 6.9.0.114 for this test.

Comments

  • Hi Ivan,

    According to the documentation here on serialization, it indicates that
    Classes and properties in an executable assembly, however, will be made private and renamed, breaking serialization if the class is defined in the executable. SmartAssembly attempts to work around this by automatically excluding from obfuscation all classes with the Serializable attribute set.

    It has further information on a way to get the obfuscation working in this instance.

    I hope this helps!

    Kind regards,
    Alex
    Product Support Engineer | Redgate Software

    Have you visited our Help Center?
  • Hi Alex,

    Thanks for the reply.
    Yes, I see, that's make sense for private fields to not be obfuscated when the type is [Serializable].

    How about private constants?

    Consider this class:
    [Serializable]
    public class Class1
    {
        private const int State = 123;
        private readonly string timestamp = DateTime.Now.ToTimestamp();
    
        public string GetStatus()
        {
            return string.IsNullOrEmpty(timestamp) ? "Not initialized " + State.ToString() : "Initialized at " + timestamp + "  " + State.ToString();
        }
    }
    

    Obfuscated version has "private const int State" unobfuscated. Any reasons why? It does not seem to have any effect on serialization.

    If I apply [NonSerialized] attribute to the constant, its name gets mangled during obfuscation.

    Thanks.
  • Hi Ivan,

    As stated before, the entire Class1 class is excluded from obfuscation, which includes the private constant. In the link it mentions you can work around this by implementing ISerializable on the class. Have a look at the documentation on serialization.

    Kind regards,
    Alex
    Product Support Engineer | Redgate Software

    Have you visited our Help Center?
Sign In or Register to comment.