Improved error messages so that it becomes clear where configuration errors come...
[mono.git] / mcs / class / IKVM.Reflection / MemberInfo.cs
1 /*
2   Copyright (C) 2009 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
25 using System;
26 using System.Collections.Generic;
27 using System.Text;
28
29 namespace IKVM.Reflection
30 {
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
35         {
36                 // prevent external subclasses
37                 internal MemberInfo()
38                 {
39                 }
40
41                 public abstract string Name { get; }
42                 public abstract Type DeclaringType { get; }
43                 public abstract MemberTypes MemberType { get; }
44
45                 public virtual Type ReflectedType
46                 {
47                         get { return DeclaringType; }
48                 }
49
50                 internal abstract MemberInfo SetReflectedType(Type type);
51
52                 public virtual int MetadataToken
53                 {
54                         get { throw new NotSupportedException(); }
55                 }
56
57                 public abstract Module Module
58                 {
59                         get;
60                 }
61
62                 public virtual bool __IsMissing
63                 {
64                         get { return false; }
65                 }
66
67                 public bool IsDefined(Type attributeType, bool inherit)
68                 {
69                         return CustomAttributeData.__GetCustomAttributes(this, attributeType, inherit).Count != 0;
70                 }
71
72                 public IList<CustomAttributeData> __GetCustomAttributes(Type attributeType, bool inherit)
73                 {
74                         return CustomAttributeData.__GetCustomAttributes(this, attributeType, inherit);
75                 }
76
77                 public static bool operator ==(MemberInfo m1, MemberInfo m2)
78                 {
79                         return ReferenceEquals(m1, m2) || (!ReferenceEquals(m1, null) && m1.Equals(m2));
80                 }
81
82                 public static bool operator !=(MemberInfo m1, MemberInfo m2)
83                 {
84                         return !(m1 == m2);
85                 }
86
87                 internal virtual IList<CustomAttributeData> GetCustomAttributesData(Type attributeType)
88                 {
89                         return this.Module.GetCustomAttributes(this.MetadataToken, attributeType);
90                 }
91
92                 internal virtual bool BindingFlagsMatch(BindingFlags flags)
93                 {
94                         throw new InvalidOperationException();
95                 }
96
97                 internal virtual bool BindingFlagsMatchInherited(BindingFlags flags)
98                 {
99                         throw new InvalidOperationException();
100                 }
101
102                 protected static bool BindingFlagsMatch(bool state, BindingFlags flags, BindingFlags trueFlag, BindingFlags falseFlag)
103                 {
104                         return (state && (flags & trueFlag) == trueFlag)
105                                 || (!state && (flags & falseFlag) == falseFlag);
106                 }
107
108                 protected static T SetReflectedType<T>(T member, Type type)
109                         where T : MemberInfo
110                 {
111                         return member == null ? null : (T)member.SetReflectedType(type);
112                 }
113
114                 protected static T[] SetReflectedType<T>(T[] members, Type type)
115                         where T : MemberInfo
116                 {
117                         for (int i = 0; i < members.Length; i++)
118                         {
119                                 members[i] = SetReflectedType(members[i], type);
120                         }
121                         return members;
122                 }
123         }
124 }