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