2004-08-08 Zoltan Varga <vargaz@freemail.hu>
[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         public sealed class FieldBuilder : FieldInfo {
43                 private FieldAttributes attrs;
44                 private Type type;
45                 private String name;
46                 private object def_value;
47                 private int offset;
48                 private int table_idx;
49                 internal TypeBuilder typeb;
50                 private byte[] rva_data;
51                 private CustomAttributeBuilder[] cattrs;
52                 private UnmanagedMarshal marshal_info;
53                 private RuntimeFieldHandle handle;
54                 private Type[] modReq;
55                 private Type[] modOpt;
56
57                 internal FieldBuilder (TypeBuilder tb, string fieldName, Type type, FieldAttributes attributes, Type[] modReq, Type[] modOpt) {
58                         attrs = attributes;
59                         name = fieldName;
60                         this.type = type;
61                         this.modReq = modReq;
62                         this.modOpt = modOpt;
63                         offset = -1;
64                         typeb = tb;
65                         table_idx = tb.get_next_table_index (this, 0x04, true);
66                 }
67
68                 public override FieldAttributes Attributes {
69                         get { return attrs; }
70                 }
71
72                 public override Type DeclaringType {
73                         get { return typeb; }
74                 }
75
76                 public override RuntimeFieldHandle FieldHandle {
77                         get {
78                                 throw CreateNotSupportedException ();
79                         }
80                 }
81
82                 public override Type FieldType {
83                         get { return type; }
84                 }
85
86                 public override string Name {
87                         get { return name; }
88                 }
89
90                 public override Type ReflectedType {
91                         get { return typeb; }
92                 }
93
94                 public override object[] GetCustomAttributes(bool inherit) {
95                         throw CreateNotSupportedException ();
96                 }
97
98                 public override object[] GetCustomAttributes(Type attributeType, bool inherit) {
99                         throw CreateNotSupportedException ();
100                 }
101
102                 public FieldToken GetToken() {
103                         return new FieldToken (0x04000000 | table_idx);
104                 }
105
106                 public override object GetValue(object obj) {
107                         throw CreateNotSupportedException ();
108                 }
109
110                 public override bool IsDefined( Type attributeType, bool inherit) {
111                         throw CreateNotSupportedException ();
112                 }
113
114                 internal void SetRVAData (byte[] data) {
115                         rva_data = (byte[])data.Clone ();
116                 }
117
118                 public void SetConstant( object defaultValue) {
119                         RejectIfCreated ();
120
121                         /*if (defaultValue.GetType() != type)
122                                 throw new ArgumentException ("Constant doesn't match field type");*/
123                         def_value = defaultValue;
124                 }
125
126                 public void SetCustomAttribute (CustomAttributeBuilder customBuilder) {
127                         RejectIfCreated ();
128
129                         string attrname = customBuilder.Ctor.ReflectedType.FullName;
130                         if (attrname == "System.Runtime.InteropServices.FieldOffsetAttribute") {
131                                 byte[] data = customBuilder.Data;
132                                 offset = (int)data [2];
133                                 offset |= ((int)data [3]) << 8;
134                                 offset |= ((int)data [4]) << 16;
135                                 offset |= ((int)data [5]) << 24;
136                                 return;
137                         } else if (attrname == "System.NonSerializedAttribute") {
138                                 attrs |= FieldAttributes.NotSerialized;
139                                 return;
140                         } else if (attrname == "System.Runtime.InteropServices.MarshalAsAttribute") {
141                                 attrs |= FieldAttributes.HasFieldMarshal;
142                                 marshal_info = CustomAttributeBuilder.get_umarshal (customBuilder, true);
143                                 /* FIXME: check for errors */
144                                 return;
145                         }
146                         if (cattrs != null) {
147                                 CustomAttributeBuilder[] new_array = new CustomAttributeBuilder [cattrs.Length + 1];
148                                 cattrs.CopyTo (new_array, 0);
149                                 new_array [cattrs.Length] = customBuilder;
150                                 cattrs = new_array;
151                         } else {
152                                 cattrs = new CustomAttributeBuilder [1];
153                                 cattrs [0] = customBuilder;
154                         }
155                 }
156
157                 public void SetCustomAttribute( ConstructorInfo con, byte[] binaryAttribute) {
158                         RejectIfCreated ();
159                         SetCustomAttribute (new CustomAttributeBuilder (con, binaryAttribute));
160                 }
161
162                 public void SetMarshal( UnmanagedMarshal unmanagedMarshal) {
163                         RejectIfCreated ();
164                         marshal_info = unmanagedMarshal;
165                         attrs |= FieldAttributes.HasFieldMarshal;
166                 }
167
168                 public void SetOffset( int iOffset) {
169                         RejectIfCreated ();
170                         offset = iOffset;
171                 }
172
173                 public override void SetValue( object obj, object val, BindingFlags invokeAttr, Binder binder, CultureInfo culture) {
174                         throw CreateNotSupportedException ();
175                 }
176
177                 private Exception CreateNotSupportedException ()
178                 {
179                         return new NotSupportedException ("The invoked member is not supported in a dynamic module.");
180                 }
181
182                 private void RejectIfCreated ()
183                 {
184                         if (typeb.is_created)
185                                 throw new InvalidOperationException ("Unable to change after type has been created.");
186                 }
187
188 #if NET_2_0 || BOOTSTRAP_NET_2_0
189                 public override FieldInfo Mono_GetGenericFieldDefinition ()
190                 {
191                         return this;
192                 }
193 #endif
194         }
195 }
196