3 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
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:
13 // The above copyright notice and this permission notice shall be
14 // included in all copies or substantial portions of the Software.
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
29 // Paolo Molaro (lupus@ximian.com)
31 // (C) 2001-2002 Ximian, Inc. http://www.ximian.com
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;
46 private object def_value;
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) {
65 table_idx = tb.get_next_table_index (this, 0x04, true);
68 public override FieldAttributes Attributes {
72 public override Type DeclaringType {
76 public override RuntimeFieldHandle FieldHandle {
78 throw CreateNotSupportedException ();
82 public override Type FieldType {
86 public override string Name {
90 public override Type ReflectedType {
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 void SetRVAData (byte[] data) {
115 rva_data = (byte[])data.Clone ();
118 public void SetConstant( object defaultValue) {
121 /*if (defaultValue.GetType() != type)
122 throw new ArgumentException ("Constant doesn't match field type");*/
123 def_value = defaultValue;
126 public void SetCustomAttribute (CustomAttributeBuilder customBuilder) {
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;
137 } else if (attrname == "System.NonSerializedAttribute") {
138 attrs |= FieldAttributes.NotSerialized;
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 */
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;
152 cattrs = new CustomAttributeBuilder [1];
153 cattrs [0] = customBuilder;
157 public void SetCustomAttribute( ConstructorInfo con, byte[] binaryAttribute) {
159 SetCustomAttribute (new CustomAttributeBuilder (con, binaryAttribute));
162 public void SetMarshal( UnmanagedMarshal unmanagedMarshal) {
164 marshal_info = unmanagedMarshal;
165 attrs |= FieldAttributes.HasFieldMarshal;
168 public void SetOffset( int iOffset) {
173 public override void SetValue( object obj, object val, BindingFlags invokeAttr, Binder binder, CultureInfo culture) {
174 throw CreateNotSupportedException ();
177 private Exception CreateNotSupportedException ()
179 return new NotSupportedException ("The invoked member is not supported in a dynamic module.");
182 private void RejectIfCreated ()
184 if (typeb.is_created)
185 throw new InvalidOperationException ("Unable to change after type has been created.");
188 #if NET_2_0 || BOOTSTRAP_NET_2_0
189 public override FieldInfo Mono_GetGenericFieldDefinition ()