Exemplo n.º 1
0
        public static bool Equate(this System.Delegate a, System.Delegate b)
        {
            // remove delegate assignment overhead
            a = a.Truncate();
            b = b.Truncate();

            // (1) method and target match
            if (a == b)
            {
                return(true);
            }

            // null
            if (a == null || b == null)
            {
                return(false);
            }

            // (2) if the target and member match
            if (a.Target == b.Target && a.Method == b.Method)
            {
                return(true);
            }

            // (3) compiled method bodies match
            if (a.Target != b.Target)
            {
                return(false);
            }
            byte[] a_body = a.Method.GetMethodBody().GetILAsByteArray();
            byte[] b_body = b.Method.GetMethodBody().GetILAsByteArray();
            if (a_body.Length != b_body.Length)
            {
                return(false);
            }
            for (int i = 0; i < a_body.Length; i++)
            {
                if (a_body[i] != b_body[i])
                {
                    return(false);
                }
            }
            return(true);
        }