Хранение делегатов в Dictionary

212
04 апреля 2018, 08:09

Здравствуйте, подскажите как бы Вы написали следующий функционал, хочу узнать как можно реализовать это более эффективно, спасибо.

enum Type
{
    Type1,
    Type2,
    Type3,
    Type4,
    Type5,
    Type6,
    Type7,
    Type8,
    Type9,
    Type10
}
class MethodsWrapper
{
    #region Variable      
    double[] Values = new double[] { 1, 2, 3, 4, 5, };
    delegate OutT MethodReturnIntWithOneInput<OutT, In1>(In1 P1);
    Dictionary<Type, MethodReturnIntWithOneInput<int, int>> MethodsReturnsIntWithOneInput;
    Dictionary<Type, MethodReturnIntWithOneInput<bool, DateTime>> MethodsReturnsBoolWithOneInput;
    delegate OutT MethodReturnIntWithOneInput<OutT, In1, In2>(In1 In1, In2 In2);
    Dictionary<Type, MethodReturnIntWithOneInput<int, double, int>> MethodsReturnIntWithTwoInput;
    Dictionary<Type, MethodReturnIntWithOneInput<string, string, int>> MethodsReturnStringWithTwoInput;
    delegate OutT MethodReturnWithoutInput<OutT>();
    Dictionary<Type, MethodReturnWithoutInput<int>> MethodsReturnsInt;
    #endregion
    #region Construction
    internal MethodsWrapper()
    {
        MethodsReturnsIntWithOneInput = new Dictionary<Type, MethodReturnIntWithOneInput<int, int>>();
        MethodsReturnsIntWithOneInput.Add(Type.Type1, MethodType1);
        MethodsReturnsIntWithOneInput.Add(Type.Type2, MethodType2);
        MethodsReturnsBoolWithOneInput = new Dictionary<Type, MethodReturnIntWithOneInput<bool, DateTime>>();
        MethodsReturnsBoolWithOneInput.Add(Type.Type3, MethodType3);
        MethodsReturnsBoolWithOneInput.Add(Type.Type4, MethodType4);
        MethodsReturnIntWithTwoInput = new Dictionary<Type, MethodReturnIntWithOneInput<int, double, int>>();
        MethodsReturnIntWithTwoInput.Add(Type.Type5, MethodType5);
        MethodsReturnStringWithTwoInput = new Dictionary<Type, MethodReturnIntWithOneInput<string, string, int>>();
        MethodsReturnStringWithTwoInput.Add(Type.Type6, MethodType6);
        MethodsReturnStringWithTwoInput.Add(Type.Type7, MethodType7);
        MethodsReturnsInt = new Dictionary<Type, MethodReturnWithoutInput<int>>();
        MethodsReturnsInt.Add(Type.Type8, MethodType8);
        MethodsReturnsInt.Add(Type.Type9, MethodType9);
        MethodsReturnsInt.Add(Type.Type10, MethodType10);
    }
    #endregion
    #region Get
    internal int GetAnswer(Type Type, int In1)
    {
        return MethodsReturnsIntWithOneInput[Type].Invoke(In1);
    }
    internal bool GetAnswer(Type Type, DateTime In1)
    {
        return MethodsReturnsBoolWithOneInput[Type].Invoke(In1);
    }
    internal int GetAnswer(Type Type, double In1, int In2)
    {
        return MethodsReturnIntWithTwoInput[Type].Invoke(In1, In2);
    }
    internal string GetAnswer(Type Type, string In1, int In2)
    {
        return MethodsReturnStringWithTwoInput[Type].Invoke(In1, In2);
    }
    internal int GetAnswer(Type Type)
    {
        return MethodsReturnsInt[Type].Invoke();
    }
    #endregion
    #region Methods
    int MethodType1(int In1)
    {
        if(Values[1] == In1)
            return 1;
        return 0;
    }
    int MethodType2(int In1)
    {
        if(Values[2] == In1)
            return 2;
        return 0;
    }
    bool MethodType3(DateTime In1)
    {
        if(Values[1] == 1 && In1 == DateTime.Now.AddSeconds(-5))
            return true;
        return false;
    }
    bool MethodType4(DateTime In1)
    {
        return false;
    }
    int MethodType5(double In1, int In2)
    {
        if(Values[3] == In1)
            return 0;
        return 1;
    }
    string MethodType6(string In1, int In2)
    {
        if(Values[4] == (double)In2)
            return "123";
        return "321";
    }
    string MethodType7(string In1, int In2)
    {
        if(Values[0] == 1.5555)
            return "123";
        return "some stuff";
    }
    int MethodType8()
    {
        if(Values[1] == 2.5555)
            return 0;
        return 1;
    }
    int MethodType9()
    {
        if(Values[6] == 13.5555)
            return 0;
        return 1;
    }
    int MethodType10()
    {
        return 2;
    }
    #endregion
}
READ ALSO
SQL-инъекции, как бороться?

SQL-инъекции, как бороться?

Доброго времени сутокВ разрабатываемом приложении работа с данными БД происходит через запросы, формируемые следующим образом (например...

213
Частое пробуждение Alarm Manager Google Play

Частое пробуждение Alarm Manager Google Play

Подскажите что это значит (скрин), и какие способы решения есть, и соответственно из-за чего это может происходить ? Используется реклама Kidoz,...

193
WebSocket получение большого сообщения

WebSocket получение большого сообщения

При получении сообщения от сокета оно обрезаетсяПолучаю примерно 680,000 символов, хотя должно прийти сообщение примерно в 3 раза больше

249
С# и Excel - Копирование диапазона ячеек в массив

С# и Excel - Копирование диапазона ячеек в массив

Есть xls файл с большим количеством строкИспользую C#, библиотеку interop

224