Acquisition of delegation by object method _C_reflection_

Time:2019-6-26

Foreword: Time is tight, first write the key code, then optimize:

Thank you for the articles shared by other bloggers. Reference: C Reflection Delegation Creator

 

1 – Defines classes that contain delegates:

public class TimeCycle
    {
        /// <summary>
        /// Unique Identification
        /// </summary>
        public int ID { get; set; }
        /// <summary>
        /// Static method delegation (define only delegation parameters)
        /// </summary>
        public Action<TimeCycle, string> Action { get; set; }
        /// <summary>
        /// Instance Method Delegation (Instance Type + Define Delegation Parameters)
        /// </summary>
        public Action<MyTipsService, TimeCycle, string> Action2 { get; set; }
    }

 

2-Initialization class:

Creating a delegate method 1 — Creating a delegate for a static method requires only two parameters: delegate type and method information:

Delegate.CreateDelegate(typeof(Action<TimeCycle, string>), myTipType.GetMethod(“SendTipsToDingding”))

 

Creating delegation method 2 — Delegation of creating instance method requires 2 + 1 parameters: in addition to the delegation type and method information above; and,,There are also entrusted methods that correspond.Instance type!!!

Namely: Action < TimeCycle, string > type must be changed to delegate with instance type: Action < MyTipsService, TimeCycle, string >, and why? It should be because delegated invocation of non-static methods requires instances to rely on instances, and only instances can invoke instance methods.

Therefore, the delegate created by the instance method must contain the instance type in order to create normally.

 

Note: A common mistake in creating delegation failures requires checking whether the delegation is static or not and the corresponding parameters:

System.ArgumentException: “cannot be bound to the target method because its signature or security transparency is incompatible with the signature or security transparency of the delegate type.”

public partial class MyTipsService : ServiceBase
    {

        public void Test()
        {
            Type myTipType = typeof(MyTipsService);
            // Create a static method delegate 1:
            TimeCycle timeCycle1 = new TimeCycle
            {
                ID = 1,
                Action = (Action<TimeCycle, string>)Delegate.CreateDelegate(typeof(Action<TimeCycle, string>), myTipType.GetMethod("MyProjectBugTips")),
            };
            // Create instance method delegation 2:
            TimeCycle timeCycle2 = new TimeCycle
            {
                ID = 1,
                Action2 = (Action<MyTipsService, TimeCycle, string>)Delegate.CreateDelegate(typeof(Action<MyTipsService, TimeCycle, string>), myTipType.GetMethod("SendTipsToDingding")),
            };

            var a = 1;
        }


        # Region Gets Reminder Messages

        /// <summary>
        /// Daily reminders
        /// </summary>
        public void SendTipsToDingding(TimeCycle timeCycle, string typeStr)
        {

        }

        /// <summary>
        /// A reminder of my project BUG
        /// </summary>
        public static void MyProjectBugTips(TimeCycle timeCycle, string typeStr)
        {

        }

        #endregion

    }