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 EventInfo()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; } GetAddMethod(bool nonPublic)42 public abstract MethodInfo GetAddMethod(bool nonPublic); GetRaiseMethod(bool nonPublic)43 public abstract MethodInfo GetRaiseMethod(bool nonPublic); GetRemoveMethod(bool nonPublic)44 public abstract MethodInfo GetRemoveMethod(bool nonPublic); GetOtherMethods(bool nonPublic)45 public abstract MethodInfo[] GetOtherMethods(bool nonPublic); __GetMethods()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 GetAddMethod()57 public MethodInfo GetAddMethod() 58 { 59 return GetAddMethod(false); 60 } 61 GetRaiseMethod()62 public MethodInfo GetRaiseMethod() 63 { 64 return GetRaiseMethod(false); 65 } 66 GetRemoveMethod()67 public MethodInfo GetRemoveMethod() 68 { 69 return GetRemoveMethod(false); 70 } 71 GetOtherMethods()72 public MethodInfo[] GetOtherMethods() 73 { 74 return GetOtherMethods(false); 75 } 76 77 public MethodInfo AddMethod 78 { 79 get { return GetAddMethod(true); } 80 } 81 82 public MethodInfo RaiseMethod 83 { 84 get { return GetRaiseMethod(true); } 85 } 86 87 public MethodInfo RemoveMethod 88 { 89 get { return GetRemoveMethod(true); } 90 } 91 BindTypeParameters(Type type)92 internal virtual EventInfo BindTypeParameters(Type type) 93 { 94 return new GenericEventInfo(this.DeclaringType.BindTypeParameters(type), this); 95 } 96 ToString()97 public override string ToString() 98 { 99 return this.DeclaringType.ToString() + " " + Name; 100 } 101 BindingFlagsMatch(BindingFlags flags)102 internal sealed override bool BindingFlagsMatch(BindingFlags flags) 103 { 104 return BindingFlagsMatch(IsPublic, flags, BindingFlags.Public, BindingFlags.NonPublic) 105 && BindingFlagsMatch(IsStatic, flags, BindingFlags.Static, BindingFlags.Instance); 106 } 107 BindingFlagsMatchInherited(BindingFlags flags)108 internal sealed override bool BindingFlagsMatchInherited(BindingFlags flags) 109 { 110 return IsNonPrivate 111 && BindingFlagsMatch(IsPublic, flags, BindingFlags.Public, BindingFlags.NonPublic) 112 && BindingFlagsMatch(IsStatic, flags, BindingFlags.Static | BindingFlags.FlattenHierarchy, BindingFlags.Instance); 113 } 114 SetReflectedType(Type type)115 internal sealed override MemberInfo SetReflectedType(Type type) 116 { 117 return new EventInfoWithReflectedType(type, this); 118 } 119 GetPseudoCustomAttributes(Type attributeType)120 internal sealed override List<CustomAttributeData> GetPseudoCustomAttributes(Type attributeType) 121 { 122 // events don't have pseudo custom attributes 123 return null; 124 } 125 } 126 127 sealed class EventInfoWithReflectedType : EventInfo 128 { 129 private readonly Type reflectedType; 130 private readonly EventInfo eventInfo; 131 EventInfoWithReflectedType(Type reflectedType, EventInfo eventInfo)132 internal EventInfoWithReflectedType(Type reflectedType, EventInfo eventInfo) 133 { 134 Debug.Assert(reflectedType != eventInfo.DeclaringType); 135 this.reflectedType = reflectedType; 136 this.eventInfo = eventInfo; 137 } 138 139 public override EventAttributes Attributes 140 { 141 get { return eventInfo.Attributes; } 142 } 143 GetAddMethod(bool nonPublic)144 public override MethodInfo GetAddMethod(bool nonPublic) 145 { 146 return SetReflectedType(eventInfo.GetAddMethod(nonPublic), reflectedType); 147 } 148 GetRaiseMethod(bool nonPublic)149 public override MethodInfo GetRaiseMethod(bool nonPublic) 150 { 151 return SetReflectedType(eventInfo.GetRaiseMethod(nonPublic), reflectedType); 152 } 153 GetRemoveMethod(bool nonPublic)154 public override MethodInfo GetRemoveMethod(bool nonPublic) 155 { 156 return SetReflectedType(eventInfo.GetRemoveMethod(nonPublic), reflectedType); 157 } 158 GetOtherMethods(bool nonPublic)159 public override MethodInfo[] GetOtherMethods(bool nonPublic) 160 { 161 return SetReflectedType(eventInfo.GetOtherMethods(nonPublic), reflectedType); 162 } 163 __GetMethods()164 public override MethodInfo[] __GetMethods() 165 { 166 return SetReflectedType(eventInfo.__GetMethods(), reflectedType); 167 } 168 169 public override Type EventHandlerType 170 { 171 get { return eventInfo.EventHandlerType; } 172 } 173 174 internal override bool IsPublic 175 { 176 get { return eventInfo.IsPublic; } 177 } 178 179 internal override bool IsNonPrivate 180 { 181 get { return eventInfo.IsNonPrivate; } 182 } 183 184 internal override bool IsStatic 185 { 186 get { return eventInfo.IsStatic; } 187 } 188 BindTypeParameters(Type type)189 internal override EventInfo BindTypeParameters(Type type) 190 { 191 return eventInfo.BindTypeParameters(type); 192 } 193 ToString()194 public override string ToString() 195 { 196 return eventInfo.ToString(); 197 } 198 199 public override bool __IsMissing 200 { 201 get { return eventInfo.__IsMissing; } 202 } 203 204 public override Type DeclaringType 205 { 206 get { return eventInfo.DeclaringType; } 207 } 208 209 public override Type ReflectedType 210 { 211 get { return reflectedType; } 212 } 213 Equals(object obj)214 public override bool Equals(object obj) 215 { 216 EventInfoWithReflectedType other = obj as EventInfoWithReflectedType; 217 return other != null 218 && other.reflectedType == reflectedType 219 && other.eventInfo == eventInfo; 220 } 221 GetHashCode()222 public override int GetHashCode() 223 { 224 return reflectedType.GetHashCode() ^ eventInfo.GetHashCode(); 225 } 226 227 public override int MetadataToken 228 { 229 get { return eventInfo.MetadataToken; } 230 } 231 232 public override Module Module 233 { 234 get { return eventInfo.Module; } 235 } 236 237 public override string Name 238 { 239 get { return eventInfo.Name; } 240 } 241 242 internal override bool IsBaked 243 { 244 get { return eventInfo.IsBaked; } 245 } 246 GetCurrentToken()247 internal override int GetCurrentToken() 248 { 249 return eventInfo.GetCurrentToken(); 250 } 251 } 252 } 253