0

値が 10001 ~ 10010の 10 個のイベントを含む呼び出しenumがあるとします。入力値 (例: 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

次の例は、値を (コンソールから読み取って) 取得し、それを enum メンバーに対応する数値にマップし、対応するイベント (この場合は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 に答える