2003-11-21 Zoltan Varga <vargaz@freemail.hu>
[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                 private Type[] modReq;
32                 private Type[] modOpt;
33
34                 internal FieldBuilder (TypeBuilder tb, string fieldName, Type type, FieldAttributes attributes, Type[] modReq, Type[] modOpt) {
35                         attrs = attributes;
36                         name = fieldName;
37                         this.type = type;
38                         this.modReq = modReq;
39                         this.modOpt = modOpt;
40                         offset = -1;
41                         typeb = tb;
42                         table_idx = tb.get_next_table_index (this, 0x04, true);
43                 }
44
45                 public override FieldAttributes Attributes {
46                         get {return attrs;}
47                 }
48                 public override Type DeclaringType {
49                         get {return typeb;}
50                 }
51                 public override RuntimeFieldHandle FieldHandle {
52                         get {return new RuntimeFieldHandle();}
53                 }
54                 public override Type FieldType {
55                         get {return type;}
56                 }
57                 public override string Name {
58                         get {return name;}
59                 }
60                 public override Type ReflectedType {
61                         get {return typeb;}
62                 }
63
64                 public override object[] GetCustomAttributes(bool inherit) {
65                         return null;
66                 }
67                 public override object[] GetCustomAttributes(Type attributeType, bool inherit) {
68                         return null;
69                 }
70                 public FieldToken GetToken() {
71                         return new FieldToken (0x04000000 | table_idx);
72                 }
73                 public override object GetValue(object obj) {
74                         return null;
75                 }
76                 public override bool IsDefined( Type attributeType, bool inherit) {
77                         return false;
78                 }
79                 internal void SetRVAData (byte[] data) {
80                         rva_data = (byte[])data.Clone ();
81                 }
82                 public void SetConstant( object defaultValue) {
83                         /*if (defaultValue.GetType() != type)
84                                 throw new ArgumentException ("Constant doesn't match field type");*/
85                         def_value = defaultValue;
86                 }
87
88                 public void SetCustomAttribute( CustomAttributeBuilder customBuilder) {
89                         string attrname = customBuilder.Ctor.ReflectedType.FullName;
90                         if (attrname == "System.Runtime.InteropServices.FieldOffsetAttribute") {
91                                 byte[] data = customBuilder.Data;
92                                 offset = (int)data [2];
93                                 offset |= ((int)data [3]) << 8;
94                                 offset |= ((int)data [4]) << 16;
95                                 offset |= ((int)data [5]) << 24;
96                                 return;
97                         } else if (attrname == "System.NonSerializedAttribute") {
98                                 attrs |= FieldAttributes.NotSerialized;
99                                 return;
100                         } else if (attrname == "System.Runtime.InteropServices.MarshalAsAttribute") {
101                                 attrs |= FieldAttributes.HasFieldMarshal;
102                                 marshal_info = CustomAttributeBuilder.get_umarshal (customBuilder, true);
103                                 /* FIXME: check for errors */
104                                 return;
105                         }
106                         if (cattrs != null) {
107                                 CustomAttributeBuilder[] new_array = new CustomAttributeBuilder [cattrs.Length + 1];
108                                 cattrs.CopyTo (new_array, 0);
109                                 new_array [cattrs.Length] = customBuilder;
110                                 cattrs = new_array;
111                         } else {
112                                 cattrs = new CustomAttributeBuilder [1];
113                                 cattrs [0] = customBuilder;
114                         }
115                 }
116
117                 public void SetCustomAttribute( ConstructorInfo con, byte[] binaryAttribute) {
118                         SetCustomAttribute (new CustomAttributeBuilder (con, binaryAttribute));
119                 }
120
121                 public void SetMarshal( UnmanagedMarshal unmanagedMarshal) {
122                         marshal_info = unmanagedMarshal;
123                         attrs |= FieldAttributes.HasFieldMarshal;
124                 }
125
126                 public void SetOffset( int iOffset) {
127                         offset = iOffset;
128                 }
129                 public override void SetValue( object obj, object val, BindingFlags invokeAttr, Binder binder, CultureInfo culture) {
130                 }
131
132         }
133 }
134