**** Merged from MCS ****
[mono-project.git] / mcs / class / corlib / System.Reflection.Emit / FieldBuilder.cs
blob200f1cb4e577dd9f5d1573005fd6c6f0832206ab
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.
26 // System.Reflection.Emit/FieldBuilder.cs
28 // Author:
29 // Paolo Molaro (lupus@ximian.com)
31 // (C) 2001-2002 Ximian, Inc. http://www.ximian.com
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;
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;
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);
68 public override FieldAttributes Attributes {
69 get { return attrs; }
72 public override Type DeclaringType {
73 get { return typeb; }
76 public override RuntimeFieldHandle FieldHandle {
77 get {
78 throw CreateNotSupportedException ();
82 public override Type FieldType {
83 get { return type; }
86 public override string Name {
87 get { return name; }
90 public override Type ReflectedType {
91 get { return typeb; }
94 public override object[] GetCustomAttributes(bool inherit) {
95 throw CreateNotSupportedException ();
98 public override object[] GetCustomAttributes(Type attributeType, bool inherit) {
99 throw CreateNotSupportedException ();
102 public FieldToken GetToken() {
103 return new FieldToken (0x04000000 | table_idx);
106 public override object GetValue(object obj) {
107 throw CreateNotSupportedException ();
110 public override bool IsDefined( Type attributeType, bool inherit) {
111 throw CreateNotSupportedException ();
114 internal override int GetFieldOffset () {
115 /* FIXME: */
116 return 0;
119 internal void SetRVAData (byte[] data) {
120 rva_data = (byte[])data.Clone ();
123 public void SetConstant( object defaultValue) {
124 RejectIfCreated ();
126 /*if (defaultValue.GetType() != type)
127 throw new ArgumentException ("Constant doesn't match field type");*/
128 def_value = defaultValue;
131 public void SetCustomAttribute (CustomAttributeBuilder customBuilder) {
132 RejectIfCreated ();
134 string attrname = customBuilder.Ctor.ReflectedType.FullName;
135 if (attrname == "System.Runtime.InteropServices.FieldOffsetAttribute") {
136 byte[] data = customBuilder.Data;
137 offset = (int)data [2];
138 offset |= ((int)data [3]) << 8;
139 offset |= ((int)data [4]) << 16;
140 offset |= ((int)data [5]) << 24;
141 return;
142 } else if (attrname == "System.NonSerializedAttribute") {
143 attrs |= FieldAttributes.NotSerialized;
144 return;
145 } else if (attrname == "System.Runtime.InteropServices.MarshalAsAttribute") {
146 attrs |= FieldAttributes.HasFieldMarshal;
147 marshal_info = CustomAttributeBuilder.get_umarshal (customBuilder, true);
148 /* FIXME: check for errors */
149 return;
151 if (cattrs != null) {
152 CustomAttributeBuilder[] new_array = new CustomAttributeBuilder [cattrs.Length + 1];
153 cattrs.CopyTo (new_array, 0);
154 new_array [cattrs.Length] = customBuilder;
155 cattrs = new_array;
156 } else {
157 cattrs = new CustomAttributeBuilder [1];
158 cattrs [0] = customBuilder;
162 public void SetCustomAttribute( ConstructorInfo con, byte[] binaryAttribute) {
163 RejectIfCreated ();
164 SetCustomAttribute (new CustomAttributeBuilder (con, binaryAttribute));
167 public void SetMarshal( UnmanagedMarshal unmanagedMarshal) {
168 RejectIfCreated ();
169 marshal_info = unmanagedMarshal;
170 attrs |= FieldAttributes.HasFieldMarshal;
173 public void SetOffset( int iOffset) {
174 RejectIfCreated ();
175 offset = iOffset;
178 public override void SetValue( object obj, object val, BindingFlags invokeAttr, Binder binder, CultureInfo culture) {
179 throw CreateNotSupportedException ();
182 private Exception CreateNotSupportedException ()
184 return new NotSupportedException ("The invoked member is not supported in a dynamic module.");
187 private void RejectIfCreated ()
189 if (typeb.is_created)
190 throw new InvalidOperationException ("Unable to change after type has been created.");
193 #if NET_2_0 || BOOTSTRAP_NET_2_0
194 public override FieldInfo Mono_GetGenericFieldDefinition ()
196 return this;
198 #endif