C#相当于“Not MyEnum.SomeValue”

时间:2012-11-07 18:30:17

标签: c# vb.net vb.net-to-c#

我正在尝试将一些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]属性;但这样做不会影响错误。

1 个答案:

答案 0 :(得分:11)

在VB中Not用于逻辑和按位NOT。

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

所以只需使用:

mPlacement = (mPlacement & ~ePlacement.Left) | ePlacement.Right;