C # sizeof (enum) option? (for a workaround for a false resharper error)?

In C #, I have a "secure" API code related to raising UAC. It includes getting the size of the listing (as follows)

int myEnumSize = sizeof (MyEnum); 

The code itself is valid, compiles, works correctly, etc. But Resharper mistakenly designates it as an error ("It is impossible to use an unsafe construct in a safe context") inside the solution. ( Starting with version C # 2.0, applying sizeof to built-in types no longer requires unsafe mode. ) I like Resharper and I like analyzing solutions, but with this code in the solution I have a big red dot in the corner that always forces me to think that something is broken. If I tell resharper to ignore this error, it returns in a few minutes.

I would raise a problem with JetBrains, but I looked at their tracker and they already have one magazine that has been ignored since March. Looking further, they have at least two other cases when this journal was registered several years ago, both were dismissed with the status of โ€œno repetitionโ€. I donโ€™t want to subscribe to their tracker just to vote for this mistake. I can still hold my breath for many years. The fastest way forward is simply to solve this problem.

What is the best alternative that is still the right one and the least likely to cause problems on the part of the maintainer?

I could write it hard:

 int myEnumSize = 4; 

Is there a better solution? - which does not use sizeof (enum)?

Btw:

  Marshal.SizeOf() 

completely "safe", but returns the wrong size.

PS. The code in questions is heavily dependent on Microsoft's UACSelfElvation demo code. If you want more information. But I do not think they are relevant.

+6
c # sizeof interop resharper
Nov 18 '10 at 20:44
source share
5 answers

It looks ugly, but can work:

 int myEnumSize = Marshal.SizeOf(Enum.GetUnderlyingType(typeof(MyEnum))); 




Edited by John Gietzen:
Evidence:
 enum Enum1 : sbyte { A, B, C, D } enum Enum2 : short { A, B, C, D } enum Enum3 : int { A, B, C, D } enum Enum4 : long { A, B, C, D } enum Enum5 : byte { A, B, C, D } enum Enum6 : ushort { A, B, C, D } enum Enum7 : uint { A, B, C, D } enum Enum8 : ulong { A, B, C, D } 

sizeof (Enum1): 1
sizeof (Enum2): 2
sizeof (Enum3): 4
sizeof (Enum4): 8
sizeof (Enum5): 1
sizeof (Enum6): 2
sizeof (Enum7): 4
sizeof (Enum8): 8

Marshal.SizeOf (Enum.GetUnderlyingType (typeof (Enum1))): 1
Marshal.SizeOf (Enum.GetUnderlyingType (typeof (Enum2))): 2
Marshal.SizeOf (Enum.GetUnderlyingType (typeof (Enum3))): 4
Marshal.SizeOf (Enum.GetUnderlyingType (typeof (Enum4))): 8
Marshal.SizeOf (Enum.GetUnderlyingType (typeof (Enum5))): 1
Marshal.SizeOf (Enum.GetUnderlyingType (typeof (Enum6))): 2
Marshal.SizeOf (Enum.GetUnderlyingType (typeof (Enum7))): 4
Marshal.SizeOf (Enum.GetUnderlyingType (typeof (Enum8))): 8

+6
Nov 18 '10 at 20:55
source share

The correct solution would be to add a comment before this line, indicating that the warning generated by the tool is incorrect. This will prevent the entanglement of future attendants and the attempt to fix something that has not broken.

+1
Nov 18 '10 at 20:46
source share

I suppose (if you really really want this), you can use the switch / case in the listing. But I assume that sizeof exists for some reason.

0
Nov 18 '10 at 20:52
source share

If you are interested in getting the size of the underlying enumeration data object, perhaps the best way would be to get the System.Type object.

 Type type = typeof (MyEnum); int enumSize = sizeof (Enum.GetUnderlyingType (type)); 
0
Nov 18 '10 at 20:52
source share

You can ignore it in ReSharper, but it's a bit of a pain and trade-offs / compromises to your design. You can put the Enum definition and the method to get the size (using sizeof) in the class in its own file and click "ReSharper"> "Options ..."> "Code Verification"> "Settings"> "Edit Elements for Skips" and then select this file (I using R # 5.1).

Obviously, you will not get the analysis code, but you will still get code cleanup.

0
Nov 18 '10 at 21:34
source share



All Articles