3 // System.Reflection.Emit/FieldBuilder.cs
6 // Paolo Molaro (lupus@ximian.com)
8 // (C) 2001-2002 Ximian, Inc. http://www.ximian.com
12 using System.Reflection;
13 using System.Reflection.Emit;
14 using System.Globalization;
15 using System.Runtime.CompilerServices;
16 using System.Runtime.InteropServices;
18 namespace System.Reflection.Emit {
19 public sealed class FieldBuilder : FieldInfo {
20 private FieldAttributes attrs;
23 private object def_value;
25 private int table_idx;
26 internal TypeBuilder typeb;
27 private byte[] rva_data;
28 private CustomAttributeBuilder[] cattrs;
29 private UnmanagedMarshal marshal_info;
31 internal FieldBuilder (TypeBuilder tb, string fieldName, Type type, FieldAttributes attributes) {
37 table_idx = tb.get_next_table_index (this, 0x04, true);
40 public override FieldAttributes Attributes {
43 public override Type DeclaringType {
46 public override RuntimeFieldHandle FieldHandle {
47 get {return new RuntimeFieldHandle();}
49 public override Type FieldType {
52 public override string Name {
55 public override Type ReflectedType {
59 public override object[] GetCustomAttributes(bool inherit) {
62 public override object[] GetCustomAttributes(Type attributeType, bool inherit) {
65 public FieldToken GetToken() {
66 return new FieldToken (0x04000000 | table_idx);
68 public override object GetValue(object obj) {
71 public override bool IsDefined( Type attributeType, bool inherit) {
74 internal void SetRVAData (byte[] data) {
75 rva_data = (byte[])data.Clone ();
77 public void SetConstant( object defaultValue) {
78 /*if (defaultValue.GetType() != type)
79 throw new ArgumentException ("Constant doesn't match field type");*/
80 def_value = defaultValue;
83 public void SetCustomAttribute( CustomAttributeBuilder customBuilder) {
84 string attrname = customBuilder.Ctor.ReflectedType.FullName;
85 if (attrname == "System.Runtime.InteropServices.FieldOffsetAttribute") {
86 byte[] data = customBuilder.Data;
87 offset = (int)data [2];
88 offset |= ((int)data [3]) << 8;
89 offset |= ((int)data [4]) << 16;
90 offset |= ((int)data [5]) << 24;
92 } else if (attrname == "System.NonSerializedAttribute") {
93 attrs |= FieldAttributes.NotSerialized;
95 } else if (attrname == "System.Runtime.InteropServices.MarshalAsAttribute") {
96 marshal_info = CustomAttributeBuilder.get_umarshal (customBuilder, true);
97 /* FIXME: check for errors */
100 if (cattrs != null) {
101 CustomAttributeBuilder[] new_array = new CustomAttributeBuilder [cattrs.Length + 1];
102 cattrs.CopyTo (new_array, 0);
103 new_array [cattrs.Length] = customBuilder;
106 cattrs = new CustomAttributeBuilder [1];
107 cattrs [0] = customBuilder;
111 public void SetCustomAttribute( ConstructorInfo con, byte[] binaryAttribute) {
112 SetCustomAttribute (new CustomAttributeBuilder (con, binaryAttribute));
115 public void SetMarshal( UnmanagedMarshal unmanagedMarshal) {
116 marshal_info = unmanagedMarshal;
117 attrs |= FieldAttributes.HasFieldMarshal;
120 public void SetOffset( int iOffset) {
123 public override void SetValue( object obj, object val, BindingFlags invokeAttr, Binder binder, CultureInfo culture) {