2008-12-08 Ivan N. Zlatev <contact@i-nz.net>
[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 #if NET_2_0
43         [ComVisible (true)]
44         [ComDefaultInterface (typeof (_FieldBuilder))]
45 #endif
46         [ClassInterface (ClassInterfaceType.None)]
47         public sealed class FieldBuilder : FieldInfo, _FieldBuilder {
48         
49 #pragma warning disable 169, 414
50                 private FieldAttributes attrs;
51                 private Type type;
52                 private String name;
53                 private object def_value;
54                 private int offset;
55                 private int table_idx;
56                 internal TypeBuilder typeb;
57                 private byte[] rva_data;
58                 private CustomAttributeBuilder[] cattrs;
59                 private UnmanagedMarshal marshal_info;
60                 private RuntimeFieldHandle handle;
61                 private Type[] modReq;
62                 private Type[] modOpt;
63 #pragma warning restore 169, 414
64
65                 internal FieldBuilder (TypeBuilder tb, string fieldName, Type type, FieldAttributes attributes, Type[] modReq, Type[] modOpt)
66                 {
67                         if (type == null)
68                                 throw new ArgumentNullException ("type");
69
70                         attrs = attributes;
71                         name = fieldName;
72                         this.type = type;
73                         this.modReq = modReq;
74                         this.modOpt = modOpt;
75                         offset = -1;
76                         typeb = tb;
77                         table_idx = tb.get_next_table_index (this, 0x04, true);
78                 }
79
80                 public override FieldAttributes Attributes {
81                         get { return attrs; }
82                 }
83
84                 public override Type DeclaringType {
85                         get { return typeb; }
86                 }
87
88                 public override RuntimeFieldHandle FieldHandle {
89                         get {
90                                 throw CreateNotSupportedException ();
91                         }
92                 }
93
94                 public override Type FieldType {
95                         get { return type; }
96                 }
97
98                 public override string Name {
99                         get { return name; }
100                 }
101
102                 public override Type ReflectedType {
103                         get { return typeb; }
104                 }
105
106                 public override object[] GetCustomAttributes(bool inherit) {
107                         /*
108                          * On MS.NET, this always returns not_supported, but we can't do this
109                          * since there would be no way to obtain custom attributes of 
110                          * dynamically created ctors.
111                          */
112                         if (typeb.is_created)
113                                 return MonoCustomAttrs.GetCustomAttributes (this, inherit);
114                         else
115                                 throw CreateNotSupportedException ();
116                 }
117
118                 public override object[] GetCustomAttributes(Type attributeType, bool inherit) {
119                         if (typeb.is_created)
120                                 return MonoCustomAttrs.GetCustomAttributes (this, attributeType, inherit);
121                         else
122                                 throw CreateNotSupportedException ();
123                 }
124
125                 public FieldToken GetToken() {
126                         return new FieldToken (MetadataToken);
127                 }
128
129                 public override object GetValue(object obj) {
130                         throw CreateNotSupportedException ();
131                 }
132
133                 public override bool IsDefined( Type attributeType, bool inherit) {
134                         throw CreateNotSupportedException ();
135                 }
136
137                 internal override int GetFieldOffset () {
138                         /* FIXME: */
139                         return 0;
140                 }
141
142                 internal void SetRVAData (byte[] data) {
143                         rva_data = (byte[])data.Clone ();
144                 }
145
146                 public void SetConstant( object defaultValue) {
147                         RejectIfCreated ();
148
149                         /*if (defaultValue.GetType() != type)
150                                 throw new ArgumentException ("Constant doesn't match field type");*/
151                         def_value = defaultValue;
152                 }
153
154                 public void SetCustomAttribute (CustomAttributeBuilder customBuilder) {
155                         RejectIfCreated ();
156
157                         string attrname = customBuilder.Ctor.ReflectedType.FullName;
158                         if (attrname == "System.Runtime.InteropServices.FieldOffsetAttribute") {
159                                 byte[] data = customBuilder.Data;
160                                 offset = (int)data [2];
161                                 offset |= ((int)data [3]) << 8;
162                                 offset |= ((int)data [4]) << 16;
163                                 offset |= ((int)data [5]) << 24;
164                                 return;
165                         } else if (attrname == "System.NonSerializedAttribute") {
166                                 attrs |= FieldAttributes.NotSerialized;
167                                 return;
168 #if NET_2_0
169                         } else if (attrname == "System.Runtime.CompilerServices.SpecialNameAttribute") {
170                                 attrs |= FieldAttributes.SpecialName;
171                                 return;
172 #endif
173                         } else if (attrname == "System.Runtime.InteropServices.MarshalAsAttribute") {
174                                 attrs |= FieldAttributes.HasFieldMarshal;
175                                 marshal_info = CustomAttributeBuilder.get_umarshal (customBuilder, true);
176                                 /* FIXME: check for errors */
177                                 return;
178                         }
179                         if (cattrs != null) {
180                                 CustomAttributeBuilder[] new_array = new CustomAttributeBuilder [cattrs.Length + 1];
181                                 cattrs.CopyTo (new_array, 0);
182                                 new_array [cattrs.Length] = customBuilder;
183                                 cattrs = new_array;
184                         } else {
185                                 cattrs = new CustomAttributeBuilder [1];
186                                 cattrs [0] = customBuilder;
187                         }
188                 }
189
190 #if NET_2_0
191                 [ComVisible (true)]
192 #endif
193                 public void SetCustomAttribute( ConstructorInfo con, byte[] binaryAttribute) {
194                         RejectIfCreated ();
195                         SetCustomAttribute (new CustomAttributeBuilder (con, binaryAttribute));
196                 }
197
198 #if NET_2_0
199                 [Obsolete ("An alternate API is available: Emit the MarshalAs custom attribute instead.")]
200 #endif
201                 public void SetMarshal( UnmanagedMarshal unmanagedMarshal) {
202                         RejectIfCreated ();
203                         marshal_info = unmanagedMarshal;
204                         attrs |= FieldAttributes.HasFieldMarshal;
205                 }
206
207                 public void SetOffset( int iOffset) {
208                         RejectIfCreated ();
209                         offset = iOffset;
210                 }
211
212                 public override void SetValue( object obj, object val, BindingFlags invokeAttr, Binder binder, CultureInfo culture) {
213                         throw CreateNotSupportedException ();
214                 }
215
216                 internal override UnmanagedMarshal UMarshal {
217                         get {
218                                 return marshal_info;
219                         }
220                 }
221
222                 private Exception CreateNotSupportedException ()
223                 {
224                         return new NotSupportedException ("The invoked member is not supported in a dynamic module.");
225                 }
226
227                 private void RejectIfCreated ()
228                 {
229                         if (typeb.is_created)
230                                 throw new InvalidOperationException ("Unable to change after type has been created.");
231                 }
232
233 #if NET_2_0 || BOOTSTRAP_NET_2_0
234                 public override Module Module {
235                         get {
236                                 return base.Module;
237                         }
238                 }
239 #endif
240
241                 void _FieldBuilder.GetIDsOfNames ([In] ref Guid riid, IntPtr rgszNames, uint cNames, uint lcid, IntPtr rgDispId)
242                 {
243                         throw new NotImplementedException ();
244                 }
245
246                 void _FieldBuilder.GetTypeInfo (uint iTInfo, uint lcid, IntPtr ppTInfo)
247                 {
248                         throw new NotImplementedException ();
249                 }
250
251                 void _FieldBuilder.GetTypeInfoCount (out uint pcTInfo)
252                 {
253                         throw new NotImplementedException ();
254                 }
255
256                 void _FieldBuilder.Invoke (uint dispIdMember, [In] ref Guid riid, uint lcid, short wFlags, IntPtr pDispParams, IntPtr pVarResult, IntPtr pExcepInfo, IntPtr puArgErr)
257                 {
258                         throw new NotImplementedException ();
259                 }
260         }
261 }
262