0

假设我有一个包含 10 个事件的enum调用,其值为 10001 - 10010 。我想使用输入值(例如 10007)来检查枚举中的成员是否具有相应的值。一旦找到具有相应输入值的事件,我就会获取该事件并通过连接一个委托向它注册一个方法(假设我拥有所有必要的代码)。我只需要知道枚举中的成员是否具有与输入值相同的值,然后为该事件注册一个新方法。如何仅使用输入值查找某个事件?我猜一个或任何其他循环不是我最好的选择。eEventIDmEvent1 - mEvent10intfor-loop

编辑:这是我到目前为止...

public delegate void EventDel(int mEvtIdx);

public enum eVtEvtId
{
    Event1,
    Event2,
    Event3,
    Event4,
    Event5,
    Event6,
    Event7,
    Event8,
    Event9,
    Event10,

}

public void Subscribe(int mInVal)
{
    eVtEvtID mEventID;
    int mEventIndex = mInVal;

    if(Enum.IsDefined(typeof(mEventID), mEventIndex))
    {
        mEventID += EventDelegate([insert method here])
    }

    else
    {
        // will warn the user that the event does
        // not yet exist in the enum
    }

}
4

2 回答 2

4

使用枚举的TryParse方法。

基本上是这样的:

YourEnum enumVal;
if (Enum.TryParse(yourInputString, out enumVal)) {
   // use enumVal here
}
于 2012-10-19T05:33:47.127 回答
0

这是一个示例,演示如何获取一个值(从控制台读取它)将其映射到与枚举成员对应的数值,定位相应的事件(在本例中为Program类)并将事件处理程序附加到它。

using System;

namespace ConsoleApplication6
{
    public enum Events
    {
        Event1 = 10001,
        Event2 = 10002,
        Event3 = 10003,
        Event4 = 10004,
        Event5 = 10005,
        Event6 = 10006,
        Event7 = 10007,
        Event8 = 10008,
        Event9 = 10009,
        Event10 = 10010
    }

    public class Program
    {
        public static event Action Event1;
        public static event Action Event2;
        public static event Action Event3;
        public static event Action Event4;
        public static event Action Event5;
        public static event Action Event6;
        public static event Action Event7;
        public static event Action Event8;
        public static event Action Event9;
        public static event Action Event10;

        private static void OnEvent1()
        {
            if(Event1 != null)
            {
                Event1();
            }
        }

        private static void OnEvent2()
        {
            if (Event2 != null)
            {
                Event2();
            }
        }

        private static void OnEvent3()
        {
            if (Event3 != null)
            {
                Event3();
            }
        }

        private static void OnEvent4()
        {
            if (Event4 != null)
            {
                Event4();
            }
        }

        private static void OnEvent5()
        {
            if (Event5 != null)
            {
                Event5();
            }
        }

        private static void OnEvent6()
        {
            if (Event6 != null)
            {
                Event6();
            }
        }

        private static void OnEvent7()
        {
            if (Event7 != null)
            {
                Event7();
            }
        }

        private static void OnEvent8()
        {
            if (Event8 != null)
            {
                Event8();
            }
        }

        private static void OnEvent9()
        {
            if (Event9 != null)
            {
                Event9();
            }
        }

        private static void OnEvent10()
        {
            if (Event10 != null)
            {
                Event10();
            }
        }

        static void Main(string[] args)
        {
            while(true)
            {
                var input = Console.ReadLine();

                if(input == "q")
                {
                    return;
                }

                int value;

                if(int.TryParse(input, out value))
                {
                    var eventValue = (Events) value;
                    var eventName = Enum.GetName(typeof (Events), eventValue);

                    var matchingEvent = typeof(Program).GetEvent(eventName);

                    if (matchingEvent != null)
                    {
                        var action = new Action(() => Console.WriteLine("Event " + eventName + " has been handled"));
                        matchingEvent.AddEventHandler(null, action);
                    }

                }

                OnEvent1();
                OnEvent2();
                OnEvent3();
                OnEvent4();
                OnEvent5();
                OnEvent6();
                OnEvent7();
                OnEvent8();
                OnEvent9();
                OnEvent10();
            }
        }
    }
}
于 2012-10-19T05:48:50.270 回答