2010-06-21 Atsushi Enomoto <atsushi@ximian.com>
[mcs.git] / class / corlib / System.Reflection.Emit / FieldBuilder.cs
blob43550f811eac0b505d85f7920cdf44211f94d9fe
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 [ComVisible (true)]
43 [ComDefaultInterface (typeof (_FieldBuilder))]
44 [ClassInterface (ClassInterfaceType.None)]
45 public sealed class FieldBuilder : FieldInfo, _FieldBuilder {
47 #pragma warning disable 169, 414
48 private FieldAttributes attrs;
49 private Type type;
50 private String name;
51 private object def_value;
52 private int offset;
53 private int table_idx;
54 internal TypeBuilder typeb;
55 private byte[] rva_data;
56 private CustomAttributeBuilder[] cattrs;
57 private UnmanagedMarshal marshal_info;
58 private RuntimeFieldHandle handle;
59 private Type[] modReq;
60 private Type[] modOpt;
61 #pragma warning restore 169, 414
63 internal FieldBuilder (TypeBuilder tb, string fieldName, Type type, FieldAttributes attributes, Type[] modReq, Type[] modOpt)
65 if (type == null)
66 throw new ArgumentNullException ("type");
68 attrs = attributes;
69 name = fieldName;
70 this.type = type;
71 this.modReq = modReq;
72 this.modOpt = modOpt;
73 offset = -1;
74 typeb = tb;
75 table_idx = tb.get_next_table_index (this, 0x04, true);
78 public override FieldAttributes Attributes {
79 get { return attrs; }
82 public override Type DeclaringType {
83 get { return typeb; }
86 public override RuntimeFieldHandle FieldHandle {
87 get {
88 throw CreateNotSupportedException ();
92 public override Type FieldType {
93 get { return type; }
96 public override string Name {
97 get { return name; }
100 public override Type ReflectedType {
101 get { return typeb; }
104 public override object[] GetCustomAttributes(bool inherit) {
106 * On MS.NET, this always returns not_supported, but we can't do this
107 * since there would be no way to obtain custom attributes of
108 * dynamically created ctors.
110 if (typeb.is_created)
111 return MonoCustomAttrs.GetCustomAttributes (this, inherit);
112 else
113 throw CreateNotSupportedException ();
116 public override object[] GetCustomAttributes(Type attributeType, bool inherit) {
117 if (typeb.is_created)
118 return MonoCustomAttrs.GetCustomAttributes (this, attributeType, inherit);
119 else
120 throw CreateNotSupportedException ();
123 public FieldToken GetToken() {
124 return new FieldToken (MetadataToken);
127 public override object GetValue(object obj) {
128 throw CreateNotSupportedException ();
131 public override bool IsDefined( Type attributeType, bool inherit) {
132 throw CreateNotSupportedException ();
135 internal override int GetFieldOffset () {
136 /* FIXME: */
137 return 0;
140 internal void SetRVAData (byte[] data) {
141 rva_data = (byte[])data.Clone ();
144 public void SetConstant( object defaultValue) {
145 RejectIfCreated ();
147 /*if (defaultValue.GetType() != type)
148 throw new ArgumentException ("Constant doesn't match field type");*/
149 def_value = defaultValue;
152 public void SetCustomAttribute (CustomAttributeBuilder customBuilder) {
153 RejectIfCreated ();
155 string attrname = customBuilder.Ctor.ReflectedType.FullName;
156 if (attrname == "System.Runtime.InteropServices.FieldOffsetAttribute") {
157 byte[] data = customBuilder.Data;
158 offset = (int)data [2];
159 offset |= ((int)data [3]) << 8;
160 offset |= ((int)data [4]) << 16;
161 offset |= ((int)data [5]) << 24;
162 return;
163 } else if (attrname == "System.NonSerializedAttribute") {
164 attrs |= FieldAttributes.NotSerialized;
165 return;
166 } else if (attrname == "System.Runtime.CompilerServices.SpecialNameAttribute") {
167 attrs |= FieldAttributes.SpecialName;
168 return;
169 } else if (attrname == "System.Runtime.InteropServices.MarshalAsAttribute") {
170 attrs |= FieldAttributes.HasFieldMarshal;
171 marshal_info = CustomAttributeBuilder.get_umarshal (customBuilder, true);
172 /* FIXME: check for errors */
173 return;
175 if (cattrs != null) {
176 CustomAttributeBuilder[] new_array = new CustomAttributeBuilder [cattrs.Length + 1];
177 cattrs.CopyTo (new_array, 0);
178 new_array [cattrs.Length] = customBuilder;
179 cattrs = new_array;
180 } else {
181 cattrs = new CustomAttributeBuilder [1];
182 cattrs [0] = customBuilder;
186 [ComVisible (true)]
187 public void SetCustomAttribute( ConstructorInfo con, byte[] binaryAttribute) {
188 RejectIfCreated ();
189 SetCustomAttribute (new CustomAttributeBuilder (con, binaryAttribute));
192 [Obsolete ("An alternate API is available: Emit the MarshalAs custom attribute instead.")]
193 public void SetMarshal( UnmanagedMarshal unmanagedMarshal) {
194 RejectIfCreated ();
195 marshal_info = unmanagedMarshal;
196 attrs |= FieldAttributes.HasFieldMarshal;
199 public void SetOffset( int iOffset) {
200 RejectIfCreated ();
201 offset = iOffset;
204 public override void SetValue( object obj, object val, BindingFlags invokeAttr, Binder binder, CultureInfo culture) {
205 throw CreateNotSupportedException ();
208 internal override UnmanagedMarshal UMarshal {
209 get {
210 return marshal_info;
214 private Exception CreateNotSupportedException ()
216 return new NotSupportedException ("The invoked member is not supported in a dynamic module.");
219 private void RejectIfCreated ()
221 if (typeb.is_created)
222 throw new InvalidOperationException ("Unable to change after type has been created.");
225 public override Module Module {
226 get {
227 return base.Module;
231 void _FieldBuilder.GetIDsOfNames ([In] ref Guid riid, IntPtr rgszNames, uint cNames, uint lcid, IntPtr rgDispId)
233 throw new NotImplementedException ();
236 void _FieldBuilder.GetTypeInfo (uint iTInfo, uint lcid, IntPtr ppTInfo)
238 throw new NotImplementedException ();
241 void _FieldBuilder.GetTypeInfoCount (out uint pcTInfo)
243 throw new NotImplementedException ();
246 void _FieldBuilder.Invoke (uint dispIdMember, [In] ref Guid riid, uint lcid, short wFlags, IntPtr pDispParams, IntPtr pVarResult, IntPtr pExcepInfo, IntPtr puArgErr)
248 throw new NotImplementedException ();