5

我正在尝试将一些 VB.net 代码转换为 C#。我使用 SharpDevelop 来完成繁重的工作;但它生成的代码在一些枚举操作上被破坏了,我不知道如何手动修复它。

VB.net 原代码:

Enum ePlacement
    Left = 1
    Right = 2
    Top = 4
    Bottom = 8
    TopLeft = Top Or Left
    TopRight = Top Or Right
    BottomLeft = Bottom Or Left
    BottomRight = Bottom Or Right
End Enum

Private mPlacement As ePlacement

''...

mPlacement = (mPlacement And Not ePlacement.Left) Or ePlacement.Right

生成的 C# 代码:

public enum ePlacement
{
    Left = 1,
    Right = 2,
    Top = 4,
    Bottom = 8,
    TopLeft = Top | Left,
    TopRight = Top | Right,
    BottomLeft = Bottom | Left,
    BottomRight = Bottom | Right
}

private ePlacement mPlacement;

//...

//Generates CS0023:   Operator '!' cannot be applied to operand of type 'Popup.Popup.ePlacement'
mPlacement = (mPlacement & !ePlacement.Left) | ePlacement.Right;

Resharper 建议将[Flags]属性添加到枚举中;但这样做不会影响错误。

4

1 回答 1

11

在 VBNot中用于逻辑和按位非。

在 C#!中是布尔非,~是按位非。

所以只需使用:

mPlacement = (mPlacement & ~ePlacement.Left) | ePlacement.Right;
于 2012-11-07T18:33:06.370 回答