Red Gate forums :: View topic - FlagsAttribute is removed on Enums
Return to www.red-gate.com RSS Feed Available

Search  | Usergroups |  Profile |  Messages |  Log in  Register 
Go to product documentation
SmartAssembly 6
SmartAssembly 6 forum

FlagsAttribute is removed on Enums

Search in SmartAssembly 6 forum
Post new topic   Reply to topic
Jump to:  
Author Message
smuda



Joined: 08 Jan 2012
Posts: 18
Location: Sweden

PostPosted: Sun Jan 22, 2012 8:01 am    Post subject: FlagsAttribute is removed on Enums Reply with quote

Hi!

I use a lot of enums with FlagsAttribute which is displayed to the user.

It seems however that SA removes the FlagsAttribute when processing the assembly. I created a short console app:

Code:

static void Main()
{
   for (var i = 1; i <= 7; i++)
   {
      Console.WriteLine((FlagsTestEnum) i);
   }
}

[DoNotObfuscateType]
[Flags]
private enum FlagsTestEnum
{
   FirstValue = 1,
   SecondValue = 2,
   ThirdValue = 4
}


Output before SA processing looks like:
Code:

FirstValue
SecondValue
FirstValue, SecondValue
ThirdValue
FirstValue, ThirdValue
SecondValue, ThirdValue
FirstValue, SecondValue, ThirdValue



Output after SA processing looks like:
Code:

FirstValue
SecondValue
3
ThirdValue
5
6
7


Using Reflector before SA processing the FlagsTestEnum looks like:
Code:

[Flags, DoNotObfuscateType]
private enum FlagsTestEnum
{
    FirstValue = 1,
    SecondValue = 2,
    ThirdValue = 4
}



Using Reflector after SA processing the FlagsTestEnum looks like:
Code:

private enum FlagsTestEnum
{
    FirstValue = 1,
    SecondValue = 2,
    ThirdValue = 4
}

As you can see, the FlagsAttribute is missing. How can I put it back?

Best Regards,

John
Back to top
View user's profile Send private message
Brian Donahue



Joined: 23 Aug 2004
Posts: 6646

PostPosted: Mon Jan 23, 2012 4:00 pm    Post subject: Reply with quote

I am unable to reproduce this issue - are you using obfuscation (name changing) pruning, and what version of SmartAssembly are you using? I think we had a problems with enums in an older version.
Back to top
View user's profile Send private message
smuda



Joined: 08 Jan 2012
Posts: 18
Location: Sweden

PostPosted: Mon Jan 23, 2012 7:57 pm    Post subject: Reply with quote

I'm using version 6.6.0.144, which I don't think qualify for "older version". Smile

You did however put me on the right track, it's the pruning the creates the issue. Enabling pruning creates the issue, disabling it removes the issue.

In SA I can add pruning exception on the "Flags" attribute which makes me able to enable pruning without creating the issue. However, programmaticly adding DoNotPrune or DoNotPruneType doesn't remove the issue.

I think that removing the Flags attribute when both DoNotPruneAttribute and DoNotPruneTypeAttribute are set is an error. What do you think?

SA automaticly detect the ToString() being used on the enum and do not obfuscate the names but still prune the FlagsAttribute. I think that is an error. What do you think?

Best Regards,

John
Back to top
View user's profile Send private message
Brian Donahue



Joined: 23 Aug 2004
Posts: 6646

PostPosted: Tue Jan 24, 2012 12:57 pm    Post subject: Reply with quote

I'll upload a private build of SmartAssembly that should fix the problem -- I'll send you an email with the download link once I can make the patch available.
Back to top
View user's profile Send private message
Brian Donahue



Joined: 23 Aug 2004
Posts: 6646

PostPosted: Tue Jan 24, 2012 4:59 pm    Post subject: Reply with quote

Hi,

You can try out ftp://support.red-gate.com/patches/smart_assembly/SmartAssembly_6.6.1.31.zip
This should sort out the enum flags problem.
Back to top
View user's profile Send private message
smuda



Joined: 08 Jan 2012
Posts: 18
Location: Sweden

PostPosted: Tue Jan 24, 2012 10:13 pm    Post subject: Reply with quote

I have to admit that was really fast of you to deliver a fix within one working day, well done! Surprised

Patch works nicely, thanks for your help!Very Happy

Best Regards,

John
Back to top
View user's profile Send private message
jasonauk



Joined: 28 Feb 2012
Posts: 4

PostPosted: Tue Feb 28, 2012 9:47 pm    Post subject: Reply with quote

Hello,

I am still having issues with this bug.

Code:

    [Flags]
    public enum DaysOfWeekFlags
    {
        NotSet = 0,
        Monday = 1,
        Tuesday = 2,
        Wednesday = 4,
        Thursday = 8,
        Friday = 16,
        Saturday = 32,
        Sunday = 64,
        Every = 127
    }


still results in:

Code:

   public enum DaysOfWeekFlags
   {
      NotSet,
      Monday,
      Tuesday,
      Wednesday = 4,
      Thursday = 8,
      Friday = 16,
      Saturday = 32,
      Sunday = 64,
      Every = 127
   }


I have the latest version: 6.6.2.35.
Back to top
View user's profile Send private message
smuda



Joined: 08 Jan 2012
Posts: 18
Location: Sweden

PostPosted: Tue Feb 28, 2012 10:00 pm    Post subject: Reply with quote

Jasonauk,

I also noticed that the latest version (6.6.2.35) seems to have re-introduced it so my latest release of my software was, well, not as good as it was meant to be. I actually trusted SmartAssembly and didn't test my obfuscated release thoroughly. My bad. Embarassed

I found out that you now at least could get it to work by adding the DoNotPrune attribute, that didn't work before. I've added that to all my flags-enums (which I use ToString() on) and is as happy as a bird.

Best Regards,

John
Back to top
View user's profile Send private message
jasonauk



Joined: 28 Feb 2012
Posts: 4

PostPosted: Tue Feb 28, 2012 10:03 pm    Post subject: Reply with quote

Hi John,

Thanks for your prompt reply. I did just do that, and it worked Smile. At least I can configure SmartAssembly to prune that one class rather than everything.

When can we expect a proper fix? It does seem slightly worrying that we can end up with broken builds if we upgrade SmartAssembly and trust it to work!

Jason
Back to top
View user's profile Send private message
smuda



Joined: 08 Jan 2012
Posts: 18
Location: Sweden

PostPosted: Tue Feb 28, 2012 10:10 pm    Post subject: Reply with quote

Good thing it worked for you! It always feels good helping someone else in the community... Smile

My experience is that Red-Gate has been really responsive so far, so I'd expect they'll respond in the morning.

John
Just another user... Wink
Back to top
View user's profile Send private message
jasonauk



Joined: 28 Feb 2012
Posts: 4

PostPosted: Tue Feb 28, 2012 10:19 pm    Post subject: Re: Reply with quote

smuda wrote:
Just another user... Wink


Oops! A little confusion there! Sorry!
Back to top
View user's profile Send private message
Simon C



Joined: 26 Feb 2008
Posts: 140
Location: Red Gate Software

PostPosted: Wed Feb 29, 2012 1:30 pm    Post subject: Reply with quote

Hi smuda, jasonauk.

I've just had a look at this, and 6.6.1.31 has the same behaviour as 6.6.2.35:

- by default, FlagsAttribute is removed from enums by pruning if they aren't publically accessible on a dll. This is because it doesn't affect the operation of the attribute (apart from ToString) and gives information away about how the attribute is meant to be used
- DoNotPruneAttribute specified on a type stops all attributes from being removed from that type, including FlagsAttribute
- Obfuscation does not affect FlagsAttribute

The fix that went into 6.6.1.31 was that SA was removing attributes even when DoNotPrune was specified.

If this is different to what you are experiencing, do let us know. In particular, project settings or exclusions and whether it's a dll or an exe could be affecting the behaviour. If that is the case, please do email support and they'll have a closer look at what's going on.

With regards to the current behaviour, what do you think SA should do? Should it not remove FlagsAttribute at all (and thus leak information about how it is used), should it try and work out when ToString is used on an enum, similar to the heuristics we use to guess when an enum shouldn't be obfuscated, or some other behaviour?
Back to top
View user's profile Send private message Send e-mail
jasonauk



Joined: 28 Feb 2012
Posts: 4

PostPosted: Wed Feb 29, 2012 2:19 pm    Post subject: Reply with quote

Hi Simon,

My [Flags] attribute is used on an Enum which is in turn used by a class (marked as Serialisable) that is serialised to XML. When the object to written the XML the DaysOfWeek property should be written as "Monday Tuesday Wednesday Thursday Friday" for those chosen days. After it has been processed by SmartAssembly, as the [Flags] attribute is no longer there, it fails to serialise/deserialse and results in a crash.

Jason
Back to top
View user's profile Send private message
smuda



Joined: 08 Jan 2012
Posts: 18
Location: Sweden

PostPosted: Wed Feb 29, 2012 3:15 pm    Post subject: Reply with quote

Hi!

The vote from the John'ian jury is: Smile

When the heuristics says it's not to be obfuscated (i.e. ToString is used) the flags attribute is to be kept.

Haven't tried HasFlag on an enum that isn't flag, but that obviously has to work regardless.

Best Regards,

John
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic All times are GMT + 1 Hour
Page 1 of 1

 
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group