Add IKVM.Reflection
[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                 public abstract string Name { get; }
37                 public abstract Type DeclaringType { get; }
38                 public abstract MemberTypes MemberType { get; }
39
40                 public virtual int MetadataToken
41                 {
42                         get { throw new NotSupportedException(); }
43                 }
44
45                 public abstract Module Module
46                 {
47                         get;
48                 }
49
50                 public bool IsDefined(Type attributeType, bool inherit)
51                 {
52                         return CustomAttributeData.__GetCustomAttributes(this, attributeType, inherit).Count != 0;
53                 }
54
55                 public IList<CustomAttributeData> __GetCustomAttributes(Type attributeType, bool inherit)
56                 {
57                         return CustomAttributeData.__GetCustomAttributes(this, attributeType, inherit);
58                 }
59
60                 public static bool operator ==(MemberInfo m1, MemberInfo m2)
61                 {
62                         return ReferenceEquals(m1, m2) || (!ReferenceEquals(m1, null) && m1.Equals(m2));
63                 }
64
65                 public static bool operator !=(MemberInfo m1, MemberInfo m2)
66                 {
67                         return !(m1 == m2);
68                 }
69
70                 internal virtual IList<CustomAttributeData> GetCustomAttributesData(Type attributeType)
71                 {
72                         return this.Module.GetCustomAttributes(this.MetadataToken, attributeType);
73                 }
74
75                 internal static bool BindingFlagsMatch(bool state, BindingFlags flags, BindingFlags trueFlag, BindingFlags falseFlag)
76                 {
77                         return (state && (flags & trueFlag) == trueFlag)
78                                 || (!state && (flags & falseFlag) == falseFlag);
79                 }
80         }
81 }