Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/algorithm/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 方法,该方法接受多个枚举类型_C#_Enums - Fatal编程技术网

C# 方法,该方法接受多个枚举类型

C# 方法,该方法接受多个枚举类型,c#,enums,C#,Enums,我有一个有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 { Fault

我有一个有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);
     }
}
我怎样才能做到这一点


谢谢

既然枚举已经有一个int值,为什么不将其强制转换为int?

尝试使用object maybe

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);
    }
}

这不需要检查或添加任何内容,只需基本功能即可:

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);
    }
}
只需使用通用的:

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);
    }
}
有关同时使用泛型和枚举的信息,请参见此问题:

由于无法使用枚举进行继承,因此无法确保函数只能在编译级别使用列表、列表或列表进行调用。你有两个真正的选择

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移动到基类中,这样就不能直接调用它并提供受保护的重载

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);
}

枚举不能从其他枚举继承,请使用类。在这里你可以看到:不需要使用类,他可以使用泛型,就像我在下面描述的:@KendeJong:核心问题是OP想要在一个列表中向一个方法传递不同的枚举类型。对不起,这可能是一个不清楚的示例,但这是我问题的一个非常简化的版本。我只是想知道如何传递多种类型的枚举。然后使用类或传递3个枚举列表,并循环遍历每个列表。因此,您可以将此方法用于任何类型?如果T不能转换为int,那么这将失败。您在哪里使用其他枚举类型?他希望使用一个包含所有enum类型的列表,而不仅仅是Enum1;当然,有几种方法可以确保安全:
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);
}