【问题标题】:How to convert a ref [enum type] to a ref int?如何将 ref [枚举类型] 转换为 ref int?
【发布时间】:2016-11-09 00:21:02
【问题描述】:

我想将 Interlocked.CompareExchange 与继承自 int 的枚举类型一起使用,如下所示:

public enum MyEnum : int { A, B }

public class MyClass
{
    private static readonly MyEnum s_field = MyEnum.A;

    public void Foo()
    {
        if (Interlocked.CompareExchange(ref s_field, MyEnum.B, MyEnum.A) == MyEnum.A)
        {
            Console.WriteLine("Changed from A to B");
        }
    }
}

但是,CompareExchange 仅适用于引用类型和选择值类型(请参阅here)。由于 MyEnum 确实是皮肤下的 int,我认为我应该能够将它作为 ref int 传递:

// should call CompareExchange(ref int, int, int) overload
Interlocked.CompareExchange(ref s_field, (int)MyEnum.B, (int)MyEnum.A);

但是,这似乎也不起作用。我收到以下错误:

错误 CS1503:参数 1:无法从 'ref MyEnum' 转换为 'ref int'

在传入之前进行铸造,例如ref (int)s_field,也无济于事。

我该如何解决这个问题?有什么方法可以将CompareExchange 与枚举一起使用,还是我必须使用整数?

【问题讨论】:

  • 您使用枚举有什么特别的原因吗? Interlocked 仅限于此 regareds 中的数字...您可以使用常量吗(我知道,... 设计气味,不过)?
  • @AndreasNiedermair 是的,我只是认为使用枚举的代码看起来更好。
  • 你不能做你想做的事,因为Interlocked 没有提供 API 的enum 变体。你必须使用int。
  • 为什么不先把它转成int再把变量传给ref呢?
  • 您的字段是readonly 并且您想通过方法(即不是构造函数)传递它?为什么要允许这样做?

标签: c# .net enums pass-by-reference interlocked


【解决方案1】:

你想使用联合吗?

public enum MyEnum : int { A, B }

[StructLayout(LayoutKind.Explicit)]
struct IntEnumUnion
{
    [FieldOffset(0)]
    public MyEnum Enum;
    [FieldOffset(0)]
    public int Int;
}

private static IntEnumUnion s_field;
s_field.Enum = MyEnum.A;

if (Interlocked.CompareExchange(ref s_field.Int, (int)MyEnum.B, (int)MyEnum.A)
    == (int)MyEnum.A)
{
    Console.WriteLine("Changed from A to B");
}

当然,有点麻烦……

【讨论】:

    【解决方案2】:

    评估后的值转换呢?

            int value = (int)MyEnum.A;
    
            var result = Interlocked.CompareExchange(ref value, (int)MyEnum.A, (int)MyEnum.B);
    
            if((MyEnum)result == MyEnum.A)
                System.Console.WriteLine("Changed from A to B");
    

    【讨论】:

      【解决方案3】:

      也许你可以使用:

      static readonly object lockInstance = new object();
      
      public static TSimple CompareExchange<TSimple>(ref TSimple location1, TSimple value, TSimple comparand)
      {
        lock (lockInstance)
        {
          var location1Read = location1;
          if (EqualityComparer<TSimple>.Default.Equals(location1Read, comparand))
          {
            // location1 write
            location1 = value;
          }
          return location1Read;
        }
      }
      

      注意:lock 仅防止通过此特定方法对location1 进行更改。在我的方法运行时,它不能阻止其他线程以其他方式操作location1。如果这是个问题,可以使用int 并使用public static class MyEnum { public const int A = 0; public const int B = 1; }。

      【讨论】:

        【解决方案4】:

        我相信现在可以使用 .NET Core 中引入的 Unsafe 类来实现这一点。运行此命令将包含该类的包安装到您的应用中:

        Install-Package System.Runtime.CompilerServices.Unsafe
        

        那你就可以Interlocked.CE(ref Unsafe.As&lt;MyEnum, int&gt;(ref s_field), (int)MyEnum.B, (int)MyEnum.A)了。请注意,这需要 C# 7 语言支持 ref 返回,因此您需要 VS2017 或更高版本。

        【讨论】:

          猜你喜欢
          • 2010-09-06
          • 1970-01-01
          • 2012-09-30
          • 1970-01-01
          • 1970-01-01
          • 2011-12-30
          • 1970-01-01
          相关资源
          最近更新 更多