Mon Jan 27 17:07:38 CET 2003 Paolo Molaro <lupus@ximian.com>
[mono.git] / mcs / class / corlib / System.Reflection.Emit / FieldBuilder.cs
1
2 //
3 // System.Reflection.Emit/FieldBuilder.cs
4 //
5 // Author:
6 //   Paolo Molaro (lupus@ximian.com)
7 //
8 // (C) 2001-2002 Ximian, Inc.  http://www.ximian.com
9 //
10
11 using System;
12 using System.Reflection;
13 using System.Reflection.Emit;
14 using System.Globalization;
15 using System.Runtime.CompilerServices;
16 using System.Runtime.InteropServices;
17
18 namespace System.Reflection.Emit {
19         public sealed class FieldBuilder : FieldInfo {
20                 private FieldAttributes attrs;
21                 private Type type;
22                 private String name;
23                 private object def_value;
24                 private int offset;
25                 private int table_idx;
26                 internal TypeBuilder typeb;
27                 private byte[] rva_data;
28                 private CustomAttributeBuilder[] cattrs;
29                 private UnmanagedMarshal marshal_info;
30                 private RuntimeFieldHandle handle;
31
32                 internal FieldBuilder (TypeBuilder tb, string fieldName, Type type, FieldAttributes attributes) {
33                         attrs = attributes;
34                         name = fieldName;
35                         this.type = type;
36                         offset = -1;
37                         typeb = tb;
38                         table_idx = tb.get_next_table_index (this, 0x04, true);
39                 }
40
41                 public override FieldAttributes Attributes {
42                         get {return attrs;}
43                 }
44                 public override Type DeclaringType {
45                         get {return typeb;}
46                 }
47                 public override RuntimeFieldHandle FieldHandle {
48                         get {return new RuntimeFieldHandle();}
49                 }
50                 public override Type FieldType {
51                         get {return type;}
52                 }
53                 public override string Name {
54                         get {return name;}
55                 }
56                 public override Type ReflectedType {
57                         get {return typeb;}
58                 }
59
60                 public override object[] GetCustomAttributes(bool inherit) {
61                         return null;
62                 }
63                 public override object[] GetCustomAttributes(Type attributeType, bool inherit) {
64                         return null;
65                 }
66                 public FieldToken GetToken() {
67                         return new FieldToken (0x04000000 | table_idx);
68                 }
69                 public override object GetValue(object obj) {
70                         return null;
71                 }
72                 public override bool IsDefined( Type attributeType, bool inherit) {
73                         return false;
74                 }
75                 internal void SetRVAData (byte[] data) {
76                         rva_data = (byte[])data.Clone ();
77                 }
78                 public void SetConstant( object defaultValue) {
79                         /*if (defaultValue.GetType() != type)
80                                 throw new ArgumentException ("Constant doesn't match field type");*/
81                         def_value = defaultValue;
82                 }
83
84                 public void SetCustomAttribute( CustomAttributeBuilder customBuilder) {
85                         string attrname = customBuilder.Ctor.ReflectedType.FullName;
86                         if (attrname == "System.Runtime.InteropServices.FieldOffsetAttribute") {
87                                 byte[] data = customBuilder.Data;
88                                 offset = (int)data [2];
89                                 offset |= ((int)data [3]) << 8;
90                                 offset |= ((int)data [4]) << 16;
91                                 offset |= ((int)data [5]) << 24;
92                                 return;
93                         } else if (attrname == "System.NonSerializedAttribute") {
94                                 attrs |= FieldAttributes.NotSerialized;
95                                 return;
96                         } else if (attrname == "System.Runtime.InteropServices.MarshalAsAttribute") {
97                                 attrs |= FieldAttributes.HasFieldMarshal;
98                                 marshal_info = CustomAttributeBuilder.get_umarshal (customBuilder, true);
99                                 /* FIXME: check for errors */
100                                 return;
101                         }
102                         if (cattrs != null) {
103                                 CustomAttributeBuilder[] new_array = new CustomAttributeBuilder [cattrs.Length + 1];
104                                 cattrs.CopyTo (new_array, 0);
105                                 new_array [cattrs.Length] = customBuilder;
106                                 cattrs = new_array;
107                         } else {
108                                 cattrs = new CustomAttributeBuilder [1];
109                                 cattrs [0] = customBuilder;
110                         }
111                 }
112
113                 public void SetCustomAttribute( ConstructorInfo con, byte[] binaryAttribute) {
114                         SetCustomAttribute (new CustomAttributeBuilder (con, binaryAttribute));
115                 }
116
117                 public void SetMarshal( UnmanagedMarshal unmanagedMarshal) {
118                         marshal_info = unmanagedMarshal;
119                         attrs |= FieldAttributes.HasFieldMarshal;
120                 }
121
122                 public void SetOffset( int iOffset) {
123                         offset = iOffset;
124                 }
125                 public override void SetValue( object obj, object val, BindingFlags invokeAttr, Binder binder, CultureInfo culture) {
126                 }
127
128         }
129 }
130