C#的sizeof(枚举)选择? (要解决的ReSharper虚假的错误)? [英] C# sizeof(enum) alternative? (to workaround resharper false error)?

查看:304
本文介绍了C#的sizeof(枚举)选择? (要解决的ReSharper虚假的错误)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在C#我有相关的UAC提升一些安全的API代码。它涉及到得到一个枚举的大小(如下图)

  INT myEnumSize = sizeof的(MyEnum); 



代码本身有效,编译,运行正常等,但ReSharper的虚假标记它作为一个错误该解决方案中(不能在安全的前提下不安全结构)。 (用C#2.0版开始,申请到的sizeof内置类型不再要求使用不安全的方式。)我喜欢ReSharper的,和我爱的解决方案的分析,但与此代码的解决方案我在,这让我总觉得有什么坏的拐角处一个大的红点。如果我告诉ReSharper的忽略这个错误它回来分钟之内。



我会提高与JetBrains公司的问题,但我看他们的跟踪,他们已经得到了一个记录为自3被忽略。进一步看他们的这种至少其他两个实例未登录的状态回去几年,无论是用无摄制状态开除。我不想登录到自己只是跟踪最多票这个bug。我仍然可以最终达到我屏住呼吸多年。前进最快的方法是刚参加工作,解决此问题。



那是什么仍然是正确的,有后来造成维护者任何麻烦的机会最小的最佳替代品?



我可以有硬代码:

  INT myEnumSize = 4; 



时有更正确的解决方案? - 不使用的sizeof(枚举)?



BTW:

  Marshal.SizeOf()

完全是安全的,但返回错误的大小。



PS。在问题的代码在很大程度上由 UACSelfElvation 演示从微软的源代码的影响。如果您想了解更多的细节。但我不认为他们是相关的。


解决方案

相貌丑陋,但可能工作:

  INT myEnumSize = Marshal.SizeOf(Enum.GetUnderlyingType(typeof运算(MyEnum))); 





编辑约翰Gietzen:结果
证明

 枚举Enum1:为sbyte {A,b,C,D} 
枚举Enum2:短{A,b ,C,D}
枚举Enum3:INT {A,b,C,D}
枚举Enum4:长{A,b,C,D}

枚举Enum5:字节{A,b,C,D}
枚举Enum6:USHORT {A,b,C,D}
枚举Enum7:UINT {A,b,C,D}
枚举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



In C# I've got some "safe" API code related to UAC elevation. It involves getting the size of an enum (as follows)

int myEnumSize = sizeof (MyEnum);

The code itself is valid, compiles, works correctly etc. But Resharper falsely flags it as a an error ("Cannot use unsafe construct in safe context") within the solution. (Starting with version 2.0 of C#, applying sizeof to built-in types no longer requires that unsafe mode be used.) I love Resharper, and I love the solution analysis, but with this code in the solution I have a big red dot in the corner that makes me always think something is broken. If I tell resharper to ignore this error it comes back within minutes.

I would raise the issue with JetBrains, but I looked on their tracker and they've already got one logged that has been ignored since March. Looking further they have at least two other instances of this logged going back several years, both were dismissed with a "no-repro" status. I don't want to sign-up to their tracker just to up-vote this bug. I could still end-up holding my breath for years. The fastest way forward is just to work-around the issue.

What is the best alternative that is still correct and has the least chance of causing a maintainer any trouble later on?

I could hard-code it to:

int myEnumSize = 4;  

Is there are more correct solution? -- which doesn't use sizeof(enum)?

Btw:

 Marshal.SizeOf() 

is completely "safe" but returns the wrong size.

PS. The code in questions is heavily influenced by the UACSelfElvation demo code from Microsoft. If you want more details. But I don't think they are relevant.

解决方案

Looks ugly, but may work:

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


Edit by John Gietzen:
Proof:

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

这篇关于C#的sizeof(枚举)选择? (要解决的ReSharper虚假的错误)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆