summaryrefslogtreecommitdiff
path: root/src/mscorlib/shared/System/Reflection/EventInfo.cs
blob: ccd9acf648b5309e8c146be8e4ba946d692662d8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System.Diagnostics;

#if FEATURE_COMINTEROP
using EventRegistrationToken = System.Runtime.InteropServices.WindowsRuntime.EventRegistrationToken;
#endif //#if FEATURE_COMINTEROP

namespace System.Reflection
{
    public abstract class EventInfo : MemberInfo
    {
        protected EventInfo() { }

        public override MemberTypes MemberType => MemberTypes.Event;

        public abstract EventAttributes Attributes { get; }
        public bool IsSpecialName => (Attributes & EventAttributes.SpecialName) != 0;

        public MethodInfo[] GetOtherMethods() => GetOtherMethods(nonPublic: false);
        public virtual MethodInfo[] GetOtherMethods(bool nonPublic) { throw NotImplemented.ByDesign; }

        public virtual MethodInfo AddMethod => GetAddMethod(nonPublic: true);
        public virtual MethodInfo RemoveMethod => GetRemoveMethod(nonPublic: true);
        public virtual MethodInfo RaiseMethod => GetRaiseMethod(nonPublic: true);

        public MethodInfo GetAddMethod() => GetAddMethod(nonPublic: false);
        public MethodInfo GetRemoveMethod() => GetRemoveMethod(nonPublic: false);
        public MethodInfo GetRaiseMethod() => GetRaiseMethod(nonPublic: false);

        public abstract MethodInfo GetAddMethod(bool nonPublic);
        public abstract MethodInfo GetRemoveMethod(bool nonPublic);
        public abstract MethodInfo GetRaiseMethod(bool nonPublic);

        public virtual bool IsMulticast
        {
            get
            {
                Type cl = EventHandlerType;
                Type mc = typeof(MulticastDelegate);
                return mc.IsAssignableFrom(cl);
            }
        }

        public virtual Type EventHandlerType
        {
            get
            {
                MethodInfo m = GetAddMethod(true);
                ParameterInfo[] p = m.GetParametersNoCopy();
                Type del = typeof(Delegate);
                for (int i = 0; i < p.Length; i++)
                {
                    Type c = p[i].ParameterType;
                    if (c.IsSubclassOf(del))
                        return c;
                }
                return null;
            }
        }

        [DebuggerHidden]
        [DebuggerStepThrough]
        public virtual void AddEventHandler(object target, Delegate handler)
        {
            MethodInfo addMethod = GetAddMethod(nonPublic: false);

            if (addMethod == null)
                throw new InvalidOperationException(SR.InvalidOperation_NoPublicAddMethod);

#if FEATURE_COMINTEROP
            if (addMethod.ReturnType == typeof(EventRegistrationToken))
                throw new InvalidOperationException(SR.InvalidOperation_NotSupportedOnWinRTEvent);
#endif //#if FEATURE_COMINTEROP

            addMethod.Invoke(target, new object[] { handler });
        }

        [DebuggerHidden]
        [DebuggerStepThrough]
        public virtual void RemoveEventHandler(object target, Delegate handler)
        {
            MethodInfo removeMethod = GetRemoveMethod(nonPublic: false);

            if (removeMethod == null)
                throw new InvalidOperationException(SR.InvalidOperation_NoPublicRemoveMethod);

#if FEATURE_COMINTEROP
            ParameterInfo[] parameters = removeMethod.GetParametersNoCopy();
            if (parameters[0].ParameterType == typeof(EventRegistrationToken))
                throw new InvalidOperationException(SR.InvalidOperation_NotSupportedOnWinRTEvent);
#endif //#if FEATURE_COMINTEROP

            removeMethod.Invoke(target, new object[] { handler });
        }

        public override bool Equals(object obj) => base.Equals(obj);
        public override int GetHashCode() => base.GetHashCode();

        public static bool operator ==(EventInfo left, EventInfo right)
        {
            if (object.ReferenceEquals(left, right))
                return true;

            if ((object)left == null || (object)right == null)
                return false;

            return left.Equals(right);
        }

        public static bool operator !=(EventInfo left, EventInfo right) => !(left == right);
    }
}