Не удается назначить анонимного делегата подписанному делегату - PullRequest
0 голосов
/ 11 ноября 2018

Я ищу способ назначить разных делегатов в списке MethodInfo без предварительной информации о типах возвращаемых данных. Ниже приведен код, который я использую. Комментарии дают дополнительную информацию о том, что происходит. Это большой кусок кода, но я сократил его настолько, насколько смог.

Основной фрагмент

private const string methodName = "Execute";

    public static void Main()
    {
        ExampleClass1 e1 = new ExampleClass1();
        ExampleClass2 e2 = new ExampleClass2();
        ExampleClass3 e3 = new ExampleClass3();

        /* Code below Simulates:  "e3.GetString = e2.Execute;" */
        var method = e2.GetType().GetMethod(methodName);

        for (int i = 0; i < e3.DelegateList.Count; i++)
        {
            // First check the type of e2 return
            Type methodType = method.ReturnType;

            // Check that its the same return type as delegate
            if (methodType != e3.DelegateList[i].ReturnType)
                continue;

            // Assign delegate to method
            var returnType = e3.DelegateList[i].DelegateType;
            e3.DelegateList[i].Delegate = Delegate.CreateDelegate(returnType, e2, method);

            /* Code below only for debugging */
            Console.WriteLine("The delegate in the list: " + e3.DelegateList[i].Delegate);// Returns Type of StringHandler
            Console.WriteLine("The delegate in the object: " + e3.GetString);// Returns null
            e3.GetString = e3.DelegateList[i].Delegate;// This throws Error Cannot convert Delegate to StringHandler
        }

        /* Code above Simulates:  "e3.GetString = e2.Execute;" */

        e2.GetNumber = e1.Execute;

        e3.Execute();// Throws Null References Exception on

        // Read the key
        Console.ReadKey();
    }

Вспомогательные классы / код

Если вам нужна дополнительная информация о вспомогательных классах, см. Код ниже. Кроме того, это отдельная программа, которая должна быть запущена как есть.

 public class ExampleClass3
{
    public delegate string StringHandler();

    public delegate int IntHandler();

    public StringHandler GetString { get; set; }
    public IntHandler GetInt { get; set; }

    public List<DelegateInfo<Type, Type, Delegate>> DelegateList { get; set; }

    public ExampleClass3()
    {
        DelegateList = new List<DelegateInfo<Type, Type, Delegate>>();
        DelegateList.Add(new DelegateInfo<Type, Type, Delegate>(typeof(StringHandler), typeof(string), GetString));
        DelegateList.Add(new DelegateInfo<Type, Type, Delegate>(typeof(IntHandler), typeof(int), GetInt));
    }

    public object Execute()
    {
        Console.WriteLine(GetString());

        return null;
    }
}

public class ExampleClass2
{
    public delegate int NumberHandler();

    public NumberHandler GetNumber { get; set; }

    public string Execute() => $"Your Number Is {GetNumber()}";
}

public class ExampleClass1
{
    public int number = 5;

    public int Execute() => number;
}

public class DelegateInfo<T1, T2, T3>
{
    public DelegateInfo(T1 delegateType, T2 returnType, T3 @delegate)
    {
        DelegateType = delegateType;
        ReturnType = returnType;
        Delegate = @delegate;
    }

    public T1 DelegateType { get; set; }
    public T2 ReturnType { get; set; }
    public T3 Delegate { get; set; }
}

Ответы [ 2 ]

0 голосов
/ 11 ноября 2018

Я немного упростил ваш код, чтобы продемонстрировать, как мне это сделать. Прежде всего, не создавайте специальный класс DelegateInfo - по возможности придерживайтесь стандартной библиотеки отражений .NET. Они проделали действительно хорошую работу, но для того, чтобы научиться, нужно время.

Вот код:

    private const string methodName = "Execute";

    public static void Main()
    {
        ExampleClass1 e1 = new ExampleClass1();
        ExampleClass2 e2 = new ExampleClass2();
        ExampleClass3 e3 = new ExampleClass3();

        /* Code below Simulates:  "e3.GetString = e2.Execute;" */
        var method = e2.GetType().GetMethod(methodName);            
        Type methodType = method.ReturnType;

        // Create a Func<T> that will invoke the target method            
        var funcType = typeof(Func<>).MakeGenericType(methodType);
        var del = Delegate.CreateDelegate(funcType, e2, method);

        var properties = e3.GetType().GetProperties();
        for (int i = 0; i < properties.Length; i++)
        {              
            if (properties[i].PropertyType.IsAssignableFrom(funcType)) {
                properties[i].SetValue(e3, del );
            }
        }

        /* Code above Simulates:  "e3.GetString = e2.Execute;" */

        e2.GetNumber = e1.Execute;

        e3.Execute();

        // Read the key
        Console.ReadKey();
    }
    public class ExampleClass3
    {         
        public Func<String> GetString { get; set; }
        public Func<int> GetInt { get; set; }

        public ExampleClass3()
        { }

        public object Execute()
        {
            Console.WriteLine(GetString());
            return null;
        }
    }

    public class ExampleClass2
    {
        public Func<int> GetNumber { get; set; }

        public string Execute() => $"Your Number Is {GetNumber()}";
    }

    public class ExampleClass1
    {
        public int number = 5;

        public int Execute() => number;
    }

Прежде всего, обратите внимание, как я избавился от пользовательских определений делегатов в пользу Func. Это будет гораздо проще работать в общем виде. Обратите внимание, как ExampleClass3 определяется сейчас:

    public class ExampleClass3
    {         
        public Func<String> GetString { get; set; }
        public Func<int> GetInt { get; set; }

        public ExampleClass3()
        { }

        public object Execute()
        {
            Console.WriteLine(GetString());
            return null;
        }
    }

Я могу использовать тот факт, что все эти функции имеют тип Func, чтобы разработать общее решение для присвоения им значения. Основываясь на типе возврата целевого метода, я могу создать делегат Func соответствующего типа (и связать его с конкретным рассматриваемым экземпляром e2):

    var funcType = typeof(Func<>).MakeGenericType(methodType);
    var del = Delegate.CreateDelegate(funcType, e2, method);

Теперь я могу напрямую назначить этого делегата в качестве значения любого свойства с соответствующим типом делегата:

        var properties = e3.GetType().GetProperties();
        for (int i = 0; i < properties.Length; i++)
        {              
            if (properties[i].PropertyType.IsAssignableFrom(funcType)) {
                properties[i].SetValue(e3, del );
            }
        } 

Надеюсь, это поможет:)

0 голосов
/ 11 ноября 2018

Отсутствует актерский состав:

e3.GetString = (ExampleClass3.StringHandler)e3.DelegateList[i].Delegate;
...