Enum.IsDefined、Enum.Parse和Enum.GetName。 这3个方法的用法是什么 有什么作用 特别是里面的参数 最好举例说明 谢谢

解决方案 »

  1.   

    看看这个吧1、关于enum的定义
    enum Fabric{Cotton = 1,Silk = 2,Wool = 4,Rayon = 8,Other = 128}
    2、符号名和常数值的互相转换            Fabric fab = Fabric.Cotton;
                int fabNum = (int)fab;//转换为常数值。必须使用强制转换。            Fabric fabString = (Fabric)1;//常数值转换成符号名。如果使用ToString(),则是((Fabric)1).ToString(),注意必须有括号。
                string fabType = fab.ToString();//显示符号名
                string fabVal = fab.ToString ("D");//显示常数值3、获得所有符号名的方法(具体参见Enum类)        public enum MyFamily
            {
                YANGZHIPING = 1,
                GUANGUIQIN = 2,
                YANGHAORAN = 4,
                LIWEI = 8,
                GUANGUIZHI = 16,
                LISIWEN = 32, 
                LISIHUA = 64,
            }            foreach (string s in Enum.GetNames(typeof(MyFamily)))
                {
                    Console.WriteLine(s);
                }4、将枚举作为位标志来处理
    根据下面的两个例子,粗略地说,一方面,设置标志[Flags]或者[FlagsAttribute],则表明要将符号名列举出来;另一方面,可以通过强制转换,将数字转换为符号名。说不准确。看下面的例子体会吧。注意:
              例一:          Fabric fab = Fabric.Cotton | Fabric.Rayon | Fabric.Silk;
               Console.WriteLine("MyFabric = {0}", fab);//输出:Fabric.Cotton | Fabric.Rayon | Fabric.Silk;
    例二:class FlagsAttributeDemo
    {
        // Define an Enum without FlagsAttribute.
        enum SingleHue : short
        {
            Black = 0,
            Red = 1,
            Green = 2,
            Blue = 4
        };    // Define an Enum with FlagsAttribute.
        [FlagsAttribute] 
        enum MultiHue : short
        {
            Black = 0,
            Red = 1,
            Green = 2,
            Blue = 4
        };    static void Main( ) 
        {
            Console.WriteLine( 
                "This example of the FlagsAttribute attribute \n" +
                "generates the following output." );
            Console.WriteLine( 
                "\nAll possible combinations of values of an \n" + 
                "Enum without FlagsAttribute:\n" );
            
            // Display all possible combinations of values.
            for( int val = 0; val <= 8; val++ )
                Console.WriteLine( "{0,3} - {1}", val, ( (SingleHue)val ).ToString( ) );        Console.WriteLine( "\nAll possible combinations of values of an \n" + "Enum with FlagsAttribute:\n" ); 
            
            // Display all possible combinations of values.
            // Also display an invalid value.
            for( int val = 0; val <= 8; val++ )
                Console.WriteLine ( "{0,3} - {1}", val, ( (MultiHue)val ).ToString( ) );
        } 
    } /*
    This example of the FlagsAttribute attribute
    generates the following output.All possible combinations of values of an 
    Enum without FlagsAttribute:0 - Black
    1 - Red
    2 - Green
    3 - 3
    4 - Blue
    5 - 5
    6 - 6
    7 - 7
    8 - 8All possible combinations of values of an
    Enum with FlagsAttribute: 0 - Black
    1 - Red
    2 - Green
    3 - Red, Green
    4 - Blue
    5 - Red, Blue
    6 - Green, Blue
    7 - Red, Green, Blue
    8 - 8
    */
    5、枚举作为函数参数。经常和switch结合起来使用。下面举例        public static double GetPrice(Fabric fab)
            {
                switch (fab)
                {
                    case Fabric.Cotton: return (3.55);
                    case Fabric.Silk: return (5.65);
                    case Fabric.Wool: return (4.05);
                    case Fabric.Rayon: return (3.20);
                    case Fabric.Other: return (2.50);
                    default: return (0.0);
                }
            }6、上面三点一个完整的例子        //1、enum的定义
            public enum Fabric : short
            {
                Cotton = 1,
                Silk = 2,
                Wool = 3,
                Rayon = 8,
                Other = 128
            }        //将枚举作为参数传递
            public static double GetPrice(Fabric fab)
            {
                switch (fab)
                {
                    case Fabric.Cotton: return (3.55);
                    case Fabric.Silk : return (5.65);
                    case Fabric.Wool: return (4.05);
                    case Fabric.Rayon: return (3.20);
                    case Fabric.Other: return (2.50);
                    default: return (0.0);
                } 
            }        public static void Main()
            {
                Fabric fab = Fabric.Cotton;
                int fabNum = (int)fab;
                string fabType = fab.ToString();
                string fabVal = fab.ToString ("D");
                double cost = GetPrice(fab);
                Console.WriteLine("fabNum = {0}\nfabType = {1}\nfabVal = {2}\n", fabNum, fabType, fabVal);
                Console.WriteLine("cost = {0}", cost); 
            }7、Enum类的使用Enum.IsDefinde、Enum.Parse两种方法经常一起使用,来确定一个值或符号是否是一个枚举的成员,然后创建一个实例。Enum.GetName打印出一个成员的值;Enum.GetNames打印出所有成员的值。其中注意typeof的使用。这一点很重要。        public enum MyFamily
            {
                YANGZHIPING = 1,
                GUANGUIQIN = 2,
                YANGHAORAN = 4,
                LIWEI = 8,
                GUANGUIZHI = 16,
                LISIWEN = 32, 
                LISIHUA = 64,
            }            string s = "YANGHAORAN";
                if (Enum.IsDefined(typeof(MyFamily), s))
                {
                    MyFamily f = (MyFamily)Enum.Parse(typeof(MyFamily), s);
                    GetMyFamily(f);
                    Console.WriteLine("The name is:" + Enum. GetName(typeof(MyFamily), 2));
                    string[] sa = Enum.GetNames(typeof(MyFamily)); 
                    foreach (string ss in sa)
                    {
                        Console.WriteLine(ss);
                    }
                }
      

  2.   


    using System;public class GetNameTest {
        enum Colors { Red, Green, Blue, Yellow };
        enum Styles { Plaid, Striped, Tartan, Corduroy };    public static void Main() {        Console.WriteLine("The 4th value of the Colors Enum is {0}", Enum.GetName(typeof(Colors), 3));
            Console.WriteLine("The 4th value of the Styles Enum is {0}", Enum.GetName(typeof(Styles), 3));
        }
    }
    using System;public class ParseTest
    {
        [FlagsAttribute]
        enum Colors { Red = 1, Green = 2, Blue = 4, Yellow = 8 };    public static void Main()
        {
            Console.WriteLine("The entries of the Colors Enum are:");
            foreach (string colorName in Enum.GetNames(typeof(Colors)))
            {
                Console.WriteLine("{0}={1}", colorName, 
                                             Convert.ToInt32(Enum.Parse(typeof(Colors), colorName)));
            }
            Console.WriteLine();
            Colors myOrange = (Colors)Enum.Parse(typeof(Colors), "Red, Yellow");
            Console.WriteLine("The myOrange value {1} has the combined entries of {0}", 
                               myOrange, Convert.ToInt64(myOrange));
        }
    }
      

  3.   

    解释,实际上查MSDN就可以了
    为了简单,直接看声明也知道
    //
    // 摘要:
    //     返回指定枚举中是否存在具有指定值的常数的指示。
    //
    // 参数:
    //   enumType:
    //     枚举类型。
    //
    //   value:
    //     enumType 的常数的值或名称。
    //
    // 返回结果:
    //     如果 enumType 的某个常数具有等于 value 的值,则为 true;否则为 false。
    //
    // 异常:
    //   System.ArgumentNullException:
    //     enumType 或 value 为 null。
    //
    //   System.ArgumentException:
    //     enumType 不是 Enum。- 或 - value 的类型不是 enumType。- 或 - value 的类型不是 enumType 的基础类型。
    //
    //   System.InvalidOperationException:
    //     value 不是 System.SByte、System.Int16、System.Int32、System.Int64、System.Byte、System.UInt16、System.UInt32、System.UInt64
    //     或 System.String 类型。
    [ComVisible(true)]
    public static bool IsDefined(Type enumType, object value);//
    // 摘要:
    //     将一个或多个枚举常数的名称或数字值的字符串表示转换成等效的枚举对象。一个参数指定该操作是否区分大小写。
    //
    // 参数:
    //   enumType:
    //     枚举的 System.Type。
    //
    //   ignoreCase:
    //     如果为 true,则忽略大小写;否则考虑大小写。
    //
    //   value:
    //     包含要转换的值或名称的字符串。
    //
    // 返回结果:
    //     enumType 类型的对象,其值由 value 表示。
    //
    // 异常:
    //   System.ArgumentNullException:
    //     enumType 或 value 为 null。
    //
    //   System.ArgumentException:
    //     enumType 不是 System.Enum。- 或 - value 是空字符串 ("") 或只包含空白。- 或 - value 是一个名称,但不是为该枚举定义的已命名常数之一。
    [ComVisible(true)]
    public static object Parse(Type enumType, string value, bool ignoreCase);
    例子
    enum EnumTest
    {
        et1 = 12,
        et2
    }private void button1_Click(object sender, EventArgs e)
    {
        textBox1.Text += string.Format(
            "Enum.IsDefined(typeof(EnumTest), 12) = {0}\r\n",
            Enum.IsDefined(typeof(EnumTest), 12));
        textBox1.Text += string.Format(
            "Enum.Parse(typeof(EnumTest), \"et1\") = {0}\r\n",
            Enum.Parse(typeof(EnumTest), "et1"));
        textBox1.Text += string.Format(
            "Enum.GetName(typeof(EnumTest), et1) = {0}\r\n",
            Enum.GetName(typeof(EnumTest), EnumTest.et1));
    }输出
    [code=BatchFile]Enum.IsDefined(typeof(EnumTest), 12) = True
    Enum.Parse(typeof(EnumTest), "et1") = et1
    Enum.GetName(typeof(EnumTest), et1) = et1[/code]