2 Copyright (C) 2009 Jeroen Frijters
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.
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:
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.
26 using System.Collections.Generic;
29 namespace IKVM.Reflection
31 // disable warnings that complain about us having == and != operators without also overriding Equals/GetHashCode,
32 // this is intentional because most subtypes use reference equality
33 #pragma warning disable 660, 661
34 public abstract class MemberInfo : ICustomAttributeProvider
36 // prevent external subclasses
41 public abstract string Name { get; }
42 public abstract Type DeclaringType { get; }
43 public abstract MemberTypes MemberType { get; }
45 public virtual Type ReflectedType
47 get { return DeclaringType; }
50 internal abstract MemberInfo SetReflectedType(Type type);
52 public virtual int MetadataToken
54 get { throw new NotSupportedException(); }
57 public abstract Module Module
62 public virtual bool __IsMissing
67 public bool IsDefined(Type attributeType, bool inherit)
69 return CustomAttributeData.__GetCustomAttributes(this, attributeType, inherit).Count != 0;
72 public IList<CustomAttributeData> __GetCustomAttributes(Type attributeType, bool inherit)
74 return CustomAttributeData.__GetCustomAttributes(this, attributeType, inherit);
77 public static bool operator ==(MemberInfo m1, MemberInfo m2)
79 return ReferenceEquals(m1, m2) || (!ReferenceEquals(m1, null) && m1.Equals(m2));
82 public static bool operator !=(MemberInfo m1, MemberInfo m2)
87 internal virtual IList<CustomAttributeData> GetCustomAttributesData(Type attributeType)
89 return this.Module.GetCustomAttributes(this.MetadataToken, attributeType);
92 internal virtual bool BindingFlagsMatch(BindingFlags flags)
94 throw new InvalidOperationException();
97 internal virtual bool BindingFlagsMatchInherited(BindingFlags flags)
99 throw new InvalidOperationException();
102 protected static bool BindingFlagsMatch(bool state, BindingFlags flags, BindingFlags trueFlag, BindingFlags falseFlag)
104 return (state && (flags & trueFlag) == trueFlag)
105 || (!state && (flags & falseFlag) == falseFlag);
108 protected static T SetReflectedType<T>(T member, Type type)
111 return member == null ? null : (T)member.SetReflectedType(type);
114 protected static T[] SetReflectedType<T>(T[] members, Type type)
117 for (int i = 0; i < members.Length; i++)
119 members[i] = SetReflectedType(members[i], type);