Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/273.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,有没有一种方法可以做到: public class Test { public int num; public string name;} public enum MyEnum { Foo = new Test(){ num = 1, name = "hi" }, Bar = new Test() { num = 2, name = "Cool" }, } string s = MyEnum.Foo.name; int i = MyEnum.Bar.num; 这样我就可以

有没有一种方法可以做到:

public class Test { public int num; public string name;}

public enum MyEnum
{
    Foo = new Test(){ num = 1, name = "hi" }, 
    Bar = new Test() { num = 2, name = "Cool" },
}
string s = MyEnum.Foo.name;
int i = MyEnum.Bar.num;
这样我就可以做如下事情:

public class Test { public int num; public string name;}

public enum MyEnum
{
    Foo = new Test(){ num = 1, name = "hi" }, 
    Bar = new Test() { num = 2, name = "Cool" },
}
string s = MyEnum.Foo.name;
int i = MyEnum.Bar.num;
基本上,我有一个很大的项目列表,我觉得不需要自己的类。我喜欢直接从枚举引用类型。(我可以列举)

*****编辑:*****


尽管有很多人说这是不可能的,但我还是能够获得所需的功能(见下面的选择答案)

你不能这样做,因为C#中的
enum
是原始整数之上的一个精简抽象。但是,您可以使用“枚举模式”,这基本上就是Java实现枚举的方式:

public sealed class MyTestEnum
{
    public int Num { get; private set; }
    public string Name { get; private set; }

    private MyTestEnum(int num, string name)
    {
        Num = num;
        Name = name;
    }

    public static readonly Foo = new MyTestEnum(1, "hi");
    public static readonly Bar = new MyTestEnum(2, "cool");
}

注意,私有构造函数确保类的唯一实例是从类定义本身创建的
静态只读
实例。这还可以安全地将枚举类的实例与
==
进行比较,而无需实现
运算符==
Equals()
。但是,请注意,
MyTestEnum
变量可能是
null
,这与常规枚举变量可能是相反的。

如果您坚持使用枚举,可以在定义中指定基础枚举类型:

public enum MyEnum :int
{
    hi =1,
    Cool =2
}

public class test
{
    public void hello()
    {
        var e = MyEnum.Cool;
        var intValue = (int)e;
        var stringValue = e.ToString();
    }
}

通过使用属性和扩展方法可以获得所需的功能。 首先创建一个属性:

[AttributeUsage(System.AttributeTargets.All, AllowMultiple = false)]
public sealed class TestAttribute : Attribute
{
    public TestAttribute(int num, string name)
    {
        Num = num;
        Name = name;
    }

    public int Num { get; private set; }
    public string Name { get; private set; }
}
然后注释“高级”枚举类型:

public enum MyEnum
{
    [Test(1, "hi")]
    Foo,
    [Test(2, "cool")]
    Bar,
}
然后为高级枚举创建扩展:

public static class TestExtensions
{

    public static int Num(this MyEnum val)
    {
        int n = 0;
        FieldInfo fi = val.GetType().GetField(val.ToString());
        TestAttribute[] attrs =
        fi.GetCustomAttributes(typeof(TestAttribute),
                               false) as TestAttribute[];
        if (attrs.Length > 0)
        {
            n = attrs[0].Num;
        }
        return n;
    }

    public static string Name(this MyEnum val)
    {
        string n = "";
        FieldInfo fi = val.GetType().GetField(val.ToString());
        TestAttribute[] attrs =
        fi.GetCustomAttributes(typeof(TestAttribute),
                               false) as TestAttribute[];
        if (attrs.Length > 0)
        {
            n = attrs[0].Name;
        }
        return n;
    }

    public static string Title(this MyEnum val)
    {
        return Enum.GetName(val.GetType(), val);
    }

}
然后使用枚举:

 public class Program
{
    static void Main(string[] args)
    {
        Console.WriteLine(MyEnum.Bar.Name());

        foreach(MyEnum e in Enum.GetValues(typeof(MyEnum)))
        {
            EnumTest(e);
        }           
    }


    public static void EnumTest(MyEnum e)
    {
        Console.WriteLine(
           String.Format("{0}'s name={1}: num={2}", e.Title(), e.Name(), e.Num()));
    }
}

Enum没有给你他的灵活性。不,它甚至不允许这样做
Foo=new Test()
抛出错误
无法将类型“Test”隐式转换为“int”
我找到了一种使用扩展方法和属性来实现这一点的方法。(见下面的答案)这只适用于byte/int/long/etc。。。仅限基本类型。