do not check order sequence if option /order was not used
[mono.git] / mcs / class / corlib / System.Reflection.Emit / FieldBuilder.cs
1
2 //
3 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
4 //
5 // Permission is hereby granted, free of charge, to any person obtaining
6 // a copy of this software and associated documentation files (the
7 // "Software"), to deal in the Software without restriction, including
8 // without limitation the rights to use, copy, modify, merge, publish,
9 // distribute, sublicense, and/or sell copies of the Software, and to
10 // permit persons to whom the Software is furnished to do so, subject to
11 // the following conditions:
12 // 
13 // The above copyright notice and this permission notice shall be
14 // included in all copies or substantial portions of the Software.
15 // 
16 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 //
24
25 //
26 // System.Reflection.Emit/FieldBuilder.cs
27 //
28 // Author:
29 //   Paolo Molaro (lupus@ximian.com)
30 //
31 // (C) 2001-2002 Ximian, Inc.  http://www.ximian.com
32 //
33
34 using System;
35 using System.Reflection;
36 using System.Reflection.Emit;
37 using System.Globalization;
38 using System.Runtime.CompilerServices;
39 using System.Runtime.InteropServices;
40
41 namespace System.Reflection.Emit {
42         [ComVisible (true)]
43         [ComDefaultInterface (typeof (_FieldBuilder))]
44         [ClassInterface (ClassInterfaceType.None)]
45         [StructLayout (LayoutKind.Sequential)]
46         public sealed class FieldBuilder : FieldInfo, _FieldBuilder {
47         
48 #pragma warning disable 169, 414
49                 private FieldAttributes attrs;
50                 private Type type;
51                 private String name;
52                 private object def_value;
53                 private int offset;
54                 private int table_idx;
55                 internal TypeBuilder typeb;
56                 private byte[] rva_data;
57                 private CustomAttributeBuilder[] cattrs;
58                 private UnmanagedMarshal marshal_info;
59                 private RuntimeFieldHandle handle;
60                 private Type[] modReq;
61                 private Type[] modOpt;
62 #pragma warning restore 169, 414
63
64                 internal FieldBuilder (TypeBuilder tb, string fieldName, Type type, FieldAttributes attributes, Type[] modReq, Type[] modOpt)
65                 {
66                         if (type == null)
67                                 throw new ArgumentNullException ("type");
68
69                         attrs = attributes;
70                         name = fieldName;
71                         this.type = type;
72                         this.modReq = modReq;
73                         this.modOpt = modOpt;
74                         offset = -1;
75                         typeb = tb;
76                         table_idx = tb.get_next_table_index (this, 0x04, true);
77
78                         ((ModuleBuilder) tb.Module).RegisterToken (this, GetToken ().Token);
79                 }
80
81                 public override FieldAttributes Attributes {
82                         get { return attrs; }
83                 }
84
85                 public override Type DeclaringType {
86                         get { return typeb; }
87                 }
88
89                 public override RuntimeFieldHandle FieldHandle {
90                         get {
91                                 throw CreateNotSupportedException ();
92                         }
93                 }
94
95                 public override Type FieldType {
96                         get { return type; }
97                 }
98
99                 public override string Name {
100                         get { return name; }
101                 }
102
103                 public override Type ReflectedType {
104                         get { return typeb; }
105                 }
106
107                 public override object[] GetCustomAttributes(bool inherit) {
108                         /*
109                          * On MS.NET, this always returns not_supported, but we can't do this
110                          * since there would be no way to obtain custom attributes of 
111                          * dynamically created ctors.
112                          */
113                         if (typeb.is_created)
114                                 return MonoCustomAttrs.GetCustomAttributes (this, inherit);
115                         else
116                                 throw CreateNotSupportedException ();
117                 }
118
119                 public override object[] GetCustomAttributes(Type attributeType, bool inherit) {
120                         if (typeb.is_created)
121                                 return MonoCustomAttrs.GetCustomAttributes (this, attributeType, inherit);
122                         else
123                                 throw CreateNotSupportedException ();
124                 }
125
126                 public FieldToken GetToken() {
127                         return new FieldToken (MetadataToken);
128                 }
129
130                 public override object GetValue(object obj) {
131                         throw CreateNotSupportedException ();
132                 }
133
134                 public override bool IsDefined( Type attributeType, bool inherit) {
135                         throw CreateNotSupportedException ();
136                 }
137
138                 internal override int GetFieldOffset () {
139                         /* FIXME: */
140                         return 0;
141                 }
142
143                 internal void SetRVAData (byte[] data) {
144                         rva_data = (byte[])data.Clone ();
145                 }
146
147                 public void SetConstant( object defaultValue) {
148                         RejectIfCreated ();
149
150                         /*if (defaultValue.GetType() != type)
151                                 throw new ArgumentException ("Constant doesn't match field type");*/
152                         def_value = defaultValue;
153                 }
154
155                 public void SetCustomAttribute (CustomAttributeBuilder customBuilder) {
156                         RejectIfCreated ();
157
158                         string attrname = customBuilder.Ctor.ReflectedType.FullName;
159                         if (attrname == "System.Runtime.InteropServices.FieldOffsetAttribute") {
160                                 byte[] data = customBuilder.Data;
161                                 offset = (int)data [2];
162                                 offset |= ((int)data [3]) << 8;
163                                 offset |= ((int)data [4]) << 16;
164                                 offset |= ((int)data [5]) << 24;
165                                 return;
166                         } else if (attrname == "System.NonSerializedAttribute") {
167                                 attrs |= FieldAttributes.NotSerialized;
168                                 return;
169                         } else if (attrname == "System.Runtime.CompilerServices.SpecialNameAttribute") {
170                                 attrs |= FieldAttributes.SpecialName;
171                                 return;
172                         } else if (attrname == "System.Runtime.InteropServices.MarshalAsAttribute") {
173                                 attrs |= FieldAttributes.HasFieldMarshal;
174                                 marshal_info = CustomAttributeBuilder.get_umarshal (customBuilder, true);
175                                 /* FIXME: check for errors */
176                                 return;
177                         }
178                         if (cattrs != null) {
179                                 CustomAttributeBuilder[] new_array = new CustomAttributeBuilder [cattrs.Length + 1];
180                                 cattrs.CopyTo (new_array, 0);
181                                 new_array [cattrs.Length] = customBuilder;
182                                 cattrs = new_array;
183                         } else {
184                                 cattrs = new CustomAttributeBuilder [1];
185                                 cattrs [0] = customBuilder;
186                         }
187                 }
188
189                 [ComVisible (true)]
190                 public void SetCustomAttribute( ConstructorInfo con, byte[] binaryAttribute) {
191                         RejectIfCreated ();
192                         SetCustomAttribute (new CustomAttributeBuilder (con, binaryAttribute));
193                 }
194
195                 [Obsolete ("An alternate API is available: Emit the MarshalAs custom attribute instead.")]
196                 public void SetMarshal( UnmanagedMarshal unmanagedMarshal) {
197                         RejectIfCreated ();
198                         marshal_info = unmanagedMarshal;
199                         attrs |= FieldAttributes.HasFieldMarshal;
200                 }
201
202                 public void SetOffset( int iOffset) {
203                         RejectIfCreated ();
204                         offset = iOffset;
205                 }
206
207                 public override void SetValue( object obj, object val, BindingFlags invokeAttr, Binder binder, CultureInfo culture) {
208                         throw CreateNotSupportedException ();
209                 }
210
211                 private Exception CreateNotSupportedException ()
212                 {
213                         return new NotSupportedException ("The invoked member is not supported in a dynamic module.");
214                 }
215
216                 private void RejectIfCreated ()
217                 {
218                         if (typeb.is_created)
219                                 throw new InvalidOperationException ("Unable to change after type has been created.");
220                 }
221
222                 public override Module Module {
223                         get {
224                                 return base.Module;
225                         }
226                 }
227
228                 void _FieldBuilder.GetIDsOfNames ([In] ref Guid riid, IntPtr rgszNames, uint cNames, uint lcid, IntPtr rgDispId)
229                 {
230                         throw new NotImplementedException ();
231                 }
232
233                 void _FieldBuilder.GetTypeInfo (uint iTInfo, uint lcid, IntPtr ppTInfo)
234                 {
235                         throw new NotImplementedException ();
236                 }
237
238                 void _FieldBuilder.GetTypeInfoCount (out uint pcTInfo)
239                 {
240                         throw new NotImplementedException ();
241                 }
242
243                 void _FieldBuilder.Invoke (uint dispIdMember, [In] ref Guid riid, uint lcid, short wFlags, IntPtr pDispParams, IntPtr pVarResult, IntPtr pExcepInfo, IntPtr puArgErr)
244                 {
245                         throw new NotImplementedException ();
246                 }
247         }
248 }
249