System.Drawing: added email to icon and test file headers
[mono.git] / mcs / class / IKVM.Reflection / FieldInfo.cs
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;
25 using System.Collections.Generic;
26 using System.Diagnostics;
27
28 namespace IKVM.Reflection
29 {
30         public abstract class FieldInfo : MemberInfo
31         {
32                 // prevent external subclasses
33                 internal FieldInfo()
34                 {
35                 }
36
37                 public sealed override MemberTypes MemberType
38                 {
39                         get { return MemberTypes.Field; }
40                 }
41
42                 public abstract FieldAttributes Attributes { get; }
43                 public abstract void __GetDataFromRVA(byte[] data, int offset, int length);
44                 public abstract int __FieldRVA { get; }
45                 public abstract Object GetRawConstantValue();
46                 internal abstract FieldSignature FieldSignature { get; }
47
48                 public Type FieldType
49                 {
50                         get { return this.FieldSignature.FieldType; }
51                 }
52
53                 public CustomModifiers __GetCustomModifiers()
54                 {
55                         return this.FieldSignature.GetCustomModifiers();
56                 }
57
58                 public Type[] GetOptionalCustomModifiers()
59                 {
60                         return __GetCustomModifiers().GetOptional();
61                 }
62
63                 public Type[] GetRequiredCustomModifiers()
64                 {
65                         return __GetCustomModifiers().GetRequired();
66                 }
67
68                 public bool IsStatic
69                 {
70                         get { return (Attributes & FieldAttributes.Static) != 0; }
71                 }
72
73                 public bool IsLiteral
74                 {
75                         get { return (Attributes & FieldAttributes.Literal) != 0; }
76                 }
77
78                 public bool IsInitOnly
79                 {
80                         get { return (Attributes & FieldAttributes.InitOnly) != 0; }
81                 }
82
83                 public bool IsNotSerialized
84                 {
85                         get { return (Attributes & FieldAttributes.NotSerialized) != 0; }
86                 }
87
88                 public bool IsSpecialName
89                 {
90                         get { return (Attributes & FieldAttributes.SpecialName) != 0; }
91                 }
92
93                 public bool IsPublic
94                 {
95                         get { return (Attributes & FieldAttributes.FieldAccessMask) == FieldAttributes.Public; }
96                 }
97
98                 public bool IsPrivate
99                 {
100                         get { return (Attributes & FieldAttributes.FieldAccessMask) == FieldAttributes.Private; }
101                 }
102
103                 public bool IsFamily
104                 {
105                         get { return (Attributes & FieldAttributes.FieldAccessMask) == FieldAttributes.Family; }
106                 }
107
108                 public bool IsFamilyOrAssembly
109                 {
110                         get { return (Attributes & FieldAttributes.FieldAccessMask) == FieldAttributes.FamORAssem; }
111                 }
112
113                 public bool IsAssembly
114                 {
115                         get { return (Attributes & FieldAttributes.FieldAccessMask) == FieldAttributes.Assembly; }
116                 }
117
118                 public bool IsFamilyAndAssembly
119                 {
120                         get { return (Attributes & FieldAttributes.FieldAccessMask) == FieldAttributes.FamANDAssem; }
121                 }
122
123                 public bool IsPinvokeImpl
124                 {
125                         get { return (Attributes & FieldAttributes.PinvokeImpl) != 0; }
126                 }
127
128                 public virtual FieldInfo __GetFieldOnTypeDefinition()
129                 {
130                         return this;
131                 }
132
133                 internal abstract int ImportTo(Emit.ModuleBuilder module);
134
135                 internal virtual FieldInfo BindTypeParameters(Type type)
136                 {
137                         return new GenericFieldInstance(this.DeclaringType.BindTypeParameters(type), this);
138                 }
139
140                 internal sealed override bool BindingFlagsMatch(BindingFlags flags)
141                 {
142                         return BindingFlagsMatch(IsPublic, flags, BindingFlags.Public, BindingFlags.NonPublic)
143                                 && BindingFlagsMatch(IsStatic, flags, BindingFlags.Static, BindingFlags.Instance);
144                 }
145
146                 internal sealed override bool BindingFlagsMatchInherited(BindingFlags flags)
147                 {
148                         return (Attributes & FieldAttributes.FieldAccessMask) > FieldAttributes.Private
149                                 && BindingFlagsMatch(IsPublic, flags, BindingFlags.Public, BindingFlags.NonPublic)
150                                 && BindingFlagsMatch(IsStatic, flags, BindingFlags.Static | BindingFlags.FlattenHierarchy, BindingFlags.Instance);
151                 }
152
153                 internal sealed override MemberInfo SetReflectedType(Type type)
154                 {
155                         return new FieldInfoWithReflectedType(type, this);
156                 }
157         }
158
159         sealed class FieldInfoWithReflectedType : FieldInfo
160         {
161                 private readonly Type reflectedType;
162                 private readonly FieldInfo field;
163
164                 internal FieldInfoWithReflectedType(Type reflectedType, FieldInfo field)
165                 {
166                         Debug.Assert(reflectedType != field.DeclaringType);
167                         this.reflectedType = reflectedType;
168                         this.field = field;
169                 }
170
171                 public override FieldAttributes Attributes
172                 {
173                         get { return field.Attributes; }
174                 }
175
176                 public override void __GetDataFromRVA(byte[] data, int offset, int length)
177                 {
178                         field.__GetDataFromRVA(data, offset, length);
179                 }
180
181                 public override int __FieldRVA
182                 {
183                         get { return field.__FieldRVA; }
184                 }
185
186                 public override Object GetRawConstantValue()
187                 {
188                         return field.GetRawConstantValue();
189                 }
190
191                 internal override FieldSignature FieldSignature
192                 {
193                         get { return field.FieldSignature; }
194                 }
195
196                 public override FieldInfo __GetFieldOnTypeDefinition()
197                 {
198                         return field.__GetFieldOnTypeDefinition();
199                 }
200
201                 internal override int ImportTo(Emit.ModuleBuilder module)
202                 {
203                         return field.ImportTo(module);
204                 }
205
206                 internal override FieldInfo BindTypeParameters(Type type)
207                 {
208                         return field.BindTypeParameters(type);
209                 }
210
211                 public override bool __IsMissing
212                 {
213                         get { return field.__IsMissing; }
214                 }
215
216                 public override Type DeclaringType
217                 {
218                         get { return field.DeclaringType; }
219                 }
220
221                 public override Type ReflectedType
222                 {
223                         get { return reflectedType; }
224                 }
225
226                 public override bool Equals(object obj)
227                 {
228                         FieldInfoWithReflectedType other = obj as FieldInfoWithReflectedType;
229                         return other != null
230                                 && other.reflectedType == reflectedType
231                                 && other.field == field;
232                 }
233
234                 public override int GetHashCode()
235                 {
236                         return reflectedType.GetHashCode() ^ field.GetHashCode();
237                 }
238
239                 internal override IList<CustomAttributeData> GetCustomAttributesData(Type attributeType)
240                 {
241                         return field.GetCustomAttributesData(attributeType);
242                 }
243
244                 public override int MetadataToken
245                 {
246                         get { return field.MetadataToken; }
247                 }
248
249                 public override Module Module
250                 {
251                         get { return field.Module; }
252                 }
253
254                 public override string Name
255                 {
256                         get { return field.Name; }
257                 }
258
259                 public override string ToString()
260                 {
261                         return field.ToString();
262                 }
263         }
264 }