Merge pull request #439 from mono-soc-2012/garyb/iconfix
[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 abstract int GetCurrentToken();
88
89                 internal abstract List<CustomAttributeData> GetPseudoCustomAttributes(Type attributeType);
90
91                 internal abstract bool IsBaked { get; }
92
93                 internal virtual bool BindingFlagsMatch(BindingFlags flags)
94                 {
95                         throw new InvalidOperationException();
96                 }
97
98                 internal virtual bool BindingFlagsMatchInherited(BindingFlags flags)
99                 {
100                         throw new InvalidOperationException();
101                 }
102
103                 protected static bool BindingFlagsMatch(bool state, BindingFlags flags, BindingFlags trueFlag, BindingFlags falseFlag)
104                 {
105                         return (state && (flags & trueFlag) == trueFlag)
106                                 || (!state && (flags & falseFlag) == falseFlag);
107                 }
108
109                 protected static T SetReflectedType<T>(T member, Type type)
110                         where T : MemberInfo
111                 {
112                         return member == null ? null : (T)member.SetReflectedType(type);
113                 }
114
115                 protected static T[] SetReflectedType<T>(T[] members, Type type)
116                         where T : MemberInfo
117                 {
118                         for (int i = 0; i < members.Length; i++)
119                         {
120                                 members[i] = SetReflectedType(members[i], type);
121                         }
122                         return members;
123                 }
124         }
125 }