C# sizeof(enum) 替代方案? (解决 resharper 错误错误)?

发布于 2024-10-03 05:27:09 字数 1006 浏览 11 评论 0原文

在 C# 中,我有一些与 UAC 提升相关的“安全”API 代码。它涉及获取枚举的大小(如下)。

int myEnumSize = sizeof (MyEnum);

代码本身是有效的,可以编译,可以正确工作等。但是 Resharper 在解决方案中错误地将其标记为错误(“不能在安全上下文中使用不安全的构造”)。 (从 C# 2.0 版开始,将 sizeof 应用于内置类型不再需要使用不安全模式。)我喜欢Resharper,也喜欢解决方案分析,但是解决方案中的这段代码我在角落里有一个大红点,这让我总觉得有什么东西坏了。如果我告诉 resharper 忽略此错误,它会在几分钟内返回。

我会向 JetBrains 提出这个问题,但我查看了他们的跟踪器,他们已经记录了一个自 3 月份以来一直被忽略的记录。进一步看,他们至少还有另外两个记录在几年前的实例,两者都以“无法重现”的状态被驳回。我不想注册他们的跟踪器只是为了对这个错误进行投票。我仍然可以屏住呼吸好几年。最快的前进方法就是解决这个问题。

仍然正确并且以后给维护人员造成麻烦的可能性最小的最佳替代方案是什么?

我可以将其硬编码为:

int myEnumSize = 4;  

是否有更正确的解决方案? -- 哪个不使用 sizeof(enum)?

顺便说一句:

 Marshal.SizeOf() 

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

附言。有问题的代码很大程度上受到 Microsoft 的 UACSelfElvation 演示代码的影响。如果您想了解更多详细信息。但我认为它们不相关。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

独享拥抱 2024-10-10 05:27:09

看起来很丑,但可能有用:

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
大小(Enum4):8
sizeof(Enum5): 1
sizeof(Enum6): 2
大小(Enum7):4
大小(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

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

愁杀 2024-10-10 05:27:09

正确的解决方案是在此行之前添加注释,指出该工具生成的警告不正确。这将防止未来的维护人员感到困惑并试图修复未损坏的东西。

The correct solution would be to add a comment before this line stating that the warning generated by the tool is incorrect. This will prevent future maintainers from becoming confused and trying to fix something that's not broken.

逆蝶 2024-10-10 05:27:09

我想(如果你真的非常想)你可以在枚举上使用 switch/case。但我的猜测是 sizeof 的存在是有原因的。

I imagine (if you really, really want to) you could use a switch/case on the enumeration. But my guess is the sizeof is there for a reason.

叫思念不要吵 2024-10-10 05:27:09

如果您有兴趣获取枚举的基础数据对象的大小,也许更好的方法是首先获取 System.Type 对象。

Type type = typeof (MyEnum);
int enumSize = sizeof (Enum.GetUnderlyingType (type));

If you're interested in getting the size of the underlying data object of the enum, perhaps a better way would be to get hold of the System.Type object first.

Type type = typeof (MyEnum);
int enumSize = sizeof (Enum.GetUnderlyingType (type));
屋顶上的小猫咪 2024-10-10 05:27:09

您可以在 ReSharper 中忽略它,但这有点麻烦并且会损害/改变您的设计。您可以将 Enum 定义和获取类大小(使用 sizeof)的方法放入其自己的文件中,然后单击 ReSharper >选项...>>代码检查>设置>编辑要跳过的项目,然后选择该文件(我使用的是 R# 5.1)。

显然你不会得到代码分析,但你仍然可以得到代码格式清理。

You can ignore it in ReSharper but it's a bit of a pain and compromises/changes your design. You can put the Enum definition and a method to get the size (using sizeof) in a class in it's own file and click on ReSharper > Options... > Code Inspection > Settings > Edit Items to Skip and then select that file (I'm using R# 5.1).

Obviously you won't get code analysis but you still get the code format cleaning.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文