do not check order sequence if option /order was not used
[mono.git] / mcs / class / IKVM.Reflection / EventInfo.cs
1 /*
2   Copyright (C) 2009-2012 Jeroen Frijters
3
4   This software is provided 'as-is', without any express or implied
5   warranty.  In no event will the authors be held liable for any damages
6   arising from the use of this software.
7
8   Permission is granted to anyone to use this software for any purpose,
9   including commercial applications, and to alter it and redistribute it
10   freely, subject to the following restrictions:
11
12   1. The origin of this software must not be misrepresented; you must not
13      claim that you wrote the original software. If you use this software
14      in a product, an acknowledgment in the product documentation would be
15      appreciated but is not required.
16   2. Altered source versions must be plainly marked as such, and must not be
17      misrepresented as being the original software.
18   3. This notice may not be removed or altered from any source distribution.
19
20   Jeroen Frijters
21   jeroen@frijters.net
22   
23 */
24 using System.Collections.Generic;
25 using System.Diagnostics;
26
27 namespace IKVM.Reflection
28 {
29         public abstract class EventInfo : MemberInfo
30         {
31                 // prevent external subclasses
32                 internal EventInfo()
33                 {
34                 }
35
36                 public sealed override MemberTypes MemberType
37                 {
38                         get { return MemberTypes.Event; }
39                 }
40
41                 public abstract EventAttributes Attributes { get; }
42                 public abstract MethodInfo GetAddMethod(bool nonPublic);
43                 public abstract MethodInfo GetRaiseMethod(bool nonPublic);
44                 public abstract MethodInfo GetRemoveMethod(bool nonPublic);
45                 public abstract MethodInfo[] GetOtherMethods(bool nonPublic);
46                 public abstract MethodInfo[] __GetMethods();
47                 public abstract Type EventHandlerType { get; }
48                 internal abstract bool IsPublic { get; }
49                 internal abstract bool IsNonPrivate { get; }
50                 internal abstract bool IsStatic { get; }
51
52                 public bool IsSpecialName
53                 {
54                         get { return (Attributes & EventAttributes.SpecialName) != 0; }
55                 }
56
57                 public MethodInfo GetAddMethod()
58                 {
59                         return GetAddMethod(false);
60                 }
61
62                 public MethodInfo GetRaiseMethod()
63                 {
64                         return GetRaiseMethod(false);
65                 }
66
67                 public MethodInfo GetRemoveMethod()
68                 {
69                         return GetRemoveMethod(false);
70                 }
71
72                 public MethodInfo[] GetOtherMethods()
73                 {
74                         return GetOtherMethods(false);
75                 }
76
77                 internal virtual EventInfo BindTypeParameters(Type type)
78                 {
79                         return new GenericEventInfo(this.DeclaringType.BindTypeParameters(type), this);
80                 }
81
82                 public override string ToString()
83                 {
84                         return this.DeclaringType.ToString() + " " + Name;
85                 }
86
87                 internal sealed override bool BindingFlagsMatch(BindingFlags flags)
88                 {
89                         return BindingFlagsMatch(IsPublic, flags, BindingFlags.Public, BindingFlags.NonPublic)
90                                 && BindingFlagsMatch(IsStatic, flags, BindingFlags.Static, BindingFlags.Instance);
91                 }
92
93                 internal sealed override bool BindingFlagsMatchInherited(BindingFlags flags)
94                 {
95                         return IsNonPrivate
96                                 && BindingFlagsMatch(IsPublic, flags, BindingFlags.Public, BindingFlags.NonPublic)
97                                 && BindingFlagsMatch(IsStatic, flags, BindingFlags.Static | BindingFlags.FlattenHierarchy, BindingFlags.Instance);
98                 }
99
100                 internal sealed override MemberInfo SetReflectedType(Type type)
101                 {
102                         return new EventInfoWithReflectedType(type, this);
103                 }
104
105                 internal sealed override List<CustomAttributeData> GetPseudoCustomAttributes(Type attributeType)
106                 {
107                         // events don't have pseudo custom attributes
108                         return null;
109                 }
110         }
111
112         sealed class EventInfoWithReflectedType : EventInfo
113         {
114                 private readonly Type reflectedType;
115                 private readonly EventInfo eventInfo;
116
117                 internal EventInfoWithReflectedType(Type reflectedType, EventInfo eventInfo)
118                 {
119                         Debug.Assert(reflectedType != eventInfo.DeclaringType);
120                         this.reflectedType = reflectedType;
121                         this.eventInfo = eventInfo;
122                 }
123
124                 public override EventAttributes Attributes
125                 {
126                         get { return eventInfo.Attributes; }
127                 }
128
129                 public override MethodInfo GetAddMethod(bool nonPublic)
130                 {
131                         return SetReflectedType(eventInfo.GetAddMethod(nonPublic), reflectedType);
132                 }
133
134                 public override MethodInfo GetRaiseMethod(bool nonPublic)
135                 {
136                         return SetReflectedType(eventInfo.GetRaiseMethod(nonPublic), reflectedType);
137                 }
138
139                 public override MethodInfo GetRemoveMethod(bool nonPublic)
140                 {
141                         return SetReflectedType(eventInfo.GetRemoveMethod(nonPublic), reflectedType);
142                 }
143
144                 public override MethodInfo[] GetOtherMethods(bool nonPublic)
145                 {
146                         return SetReflectedType(eventInfo.GetOtherMethods(nonPublic), reflectedType);
147                 }
148
149                 public override MethodInfo[] __GetMethods()
150                 {
151                         return SetReflectedType(eventInfo.__GetMethods(), reflectedType);
152                 }
153
154                 public override Type EventHandlerType
155                 {
156                         get { return eventInfo.EventHandlerType; }
157                 }
158
159                 internal override bool IsPublic
160                 {
161                         get { return eventInfo.IsPublic; }
162                 }
163
164                 internal override bool IsNonPrivate
165                 {
166                         get { return eventInfo.IsNonPrivate; }
167                 }
168
169                 internal override bool IsStatic
170                 {
171                         get { return eventInfo.IsStatic; }
172                 }
173
174                 internal override EventInfo BindTypeParameters(Type type)
175                 {
176                         return eventInfo.BindTypeParameters(type);
177                 }
178
179                 public override string ToString()
180                 {
181                         return eventInfo.ToString();
182                 }
183
184                 public override bool __IsMissing
185                 {
186                         get { return eventInfo.__IsMissing; }
187                 }
188
189                 public override Type DeclaringType
190                 {
191                         get { return eventInfo.DeclaringType; }
192                 }
193
194                 public override Type ReflectedType
195                 {
196                         get { return reflectedType; }
197                 }
198
199                 public override bool Equals(object obj)
200                 {
201                         EventInfoWithReflectedType other = obj as EventInfoWithReflectedType;
202                         return other != null
203                                 && other.reflectedType == reflectedType
204                                 && other.eventInfo == eventInfo;
205                 }
206
207                 public override int GetHashCode()
208                 {
209                         return reflectedType.GetHashCode() ^ eventInfo.GetHashCode();
210                 }
211
212                 public override int MetadataToken
213                 {
214                         get { return eventInfo.MetadataToken; }
215                 }
216
217                 public override Module Module
218                 {
219                         get { return eventInfo.Module; }
220                 }
221
222                 public override string Name
223                 {
224                         get { return eventInfo.Name; }
225                 }
226
227                 internal override bool IsBaked
228                 {
229                         get { return eventInfo.IsBaked; }
230                 }
231
232                 internal override int GetCurrentToken()
233                 {
234                         return eventInfo.GetCurrentToken();
235                 }
236         }
237 }