3

我有一个有 3 种枚举类型的类

我想要一个可以将所有 3 个枚举作为参数并获取枚举的整数值的方法。

public enum Enum1
{
    Fire = 0,
    Hour_24 = 1,
    Key_Switch = 2,
    Follower = 3,
    Entry_Delay1 = 4,
    Entry_Delay2 = 5,
    Intertior = 6,
    Local_Only = 7,
}

public enum Enum2
{
    Faulted = 0,
    Tampered = 1,
    Trouble = 2,
    Bypassed = 3,
    Inhibited = 4, 
    Low_Battery = 5,
    Loss_Supervision = 6,
    Reserved,
    Alarm_Memory = 8,
    Bypass_Memory = 9
}

private void BuildMessage ()
{
     List<Enum1> Enum1List = new List<Enum1>();
     FillBits(Enum1List);  // => Here I get an error.
}

// This method should accept Enum1 and Enum2
private Byte[] FillBits(List<Enum> EnumList)
{
     foreach (Enum e in EnumList)
     {
        int value = Convert.ToInt32(e);
     }
}

我怎样才能做到这一点?

谢谢

4

5 回答 5

7

只需使用泛型:

private Byte[] FillBits<T>(List<T> EnumList)
        where T : struct, IConvertible
{
    if (!typeof(T).IsEnum) 
    {
        throw new ArgumentException("T must be an enumerated type");
    }
    foreach (var e in EnumList)
    {
        int value = Convert.ToInt32(e);
    }
}

请参阅此问题以同时使用泛型和枚举:

创建将 T 限制为枚举的通用方法

于 2013-04-17T08:45:15.283 回答
2

既然枚举已经有一个 int 值,为什么不直接将它转换为 int 呢?

于 2013-04-17T08:36:48.967 回答
2

尝试使用对象也许?

private void BuildMessage()
{
    var enum1List = new List<object>();
    FillBits(enum1List);  // => Here I get an error.
}

// This method should accept Enum1 and Enum2
private Byte[] FillBits(IEnumerable<object> enumList)
{
    foreach (Enum e in enumList)
    {
        int value = Convert.ToInt32(e);
    }
}
于 2013-04-17T08:40:22.400 回答
2

这应该有效(没有检查或任何添加,只是基本功能):

private void BuildMessage()
{
    List<Enum1> Enum1List = new List<Enum1>();
    Enum1List.Add(Enum1.Fire);
    Enum1List.Add(Enum1.Follower);
    FillBits(Enum1List);
}

private Byte[] FillBits<T>(List<T> enumList)
{
    foreach (var e in enumList)
    {
        int value = Convert.ToInt32(e);
    }
}
于 2013-04-17T08:43:57.410 回答
2
private Byte[] FillBits<T>(List<T> EnumList) where T: struct, IConvertable
{
     foreach (Enum e in EnumList)
     {
        int value = Convert.ToInt32(e);
     }
}

因为您不能使用 Enums 继承,所以您没有真正的方法来确保只能使用 a或List<Enum1>在编译级别调用该函数。你有两个真正的选择List<Enum2>List<Enum3>

1) 运行时类型检查

private Byte[] FillBits<T>(List<T> EnumList) Where T:struct, IConvertable
{
     if (typeof(T) != typeof(Enum1) && 
         typeof(T) != typeof(Enum2) && 
         typeof(T) != typeof(Enum3)) {

         throw new <EXCEPTION OF YOUR CHOICE!>;
     }

     foreach (Enum e in EnumList)
     {
        int value = Convert.ToInt32(e);
     }
}

或者移动FillBits<T>到一个基类中,这样它就不能被直接调用并提供受保护的重载。

private Byte[] PrivateFillBits<T>(List<T> EnumList) where T: struct, IConvertable
{ ... }

protected Byte[] FillBits(List<Enum1> EnumList) {
   return this.PrivateFillBits(EnumList);
}

protected Byte[] FillBits(List<Enum2> EnumList) {
   return this.PrivateFillBits(EnumList);
}

protected Byte[] FillBits(List<Enum3> EnumList) {
   return this.PrivateFillBits(EnumList);
}
于 2013-04-17T08:50:40.697 回答