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 {
44 [ComDefaultInterface (typeof (_FieldBuilder))]
46 [ClassInterface (ClassInterfaceType.None)]
47 public sealed class FieldBuilder : FieldInfo, _FieldBuilder {
48 private FieldAttributes attrs;
51 private object def_value;
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;
62 internal FieldBuilder (TypeBuilder tb, string fieldName, Type type, FieldAttributes attributes, Type[] modReq, Type[] modOpt) {
70 table_idx = tb.get_next_table_index (this, 0x04, true);
73 public override FieldAttributes Attributes {
77 public override Type DeclaringType {
81 public override RuntimeFieldHandle FieldHandle {
83 throw CreateNotSupportedException ();
87 public override Type FieldType {
91 public override string Name {
95 public override Type ReflectedType {
99 public override object[] GetCustomAttributes(bool inherit) {
100 throw CreateNotSupportedException ();
103 public override object[] GetCustomAttributes(Type attributeType, bool inherit) {
104 throw CreateNotSupportedException ();
107 public FieldToken GetToken() {
108 return new FieldToken (0x04000000 | table_idx);
111 public override object GetValue(object obj) {
112 throw CreateNotSupportedException ();
115 public override bool IsDefined( Type attributeType, bool inherit) {
116 throw CreateNotSupportedException ();
119 internal override int GetFieldOffset () {
124 internal void SetRVAData (byte[] data) {
125 rva_data = (byte[])data.Clone ();
128 public void SetConstant( object defaultValue) {
131 /*if (defaultValue.GetType() != type)
132 throw new ArgumentException ("Constant doesn't match field type");*/
133 def_value = defaultValue;
136 public void SetCustomAttribute (CustomAttributeBuilder customBuilder) {
139 string attrname = customBuilder.Ctor.ReflectedType.FullName;
140 if (attrname == "System.Runtime.InteropServices.FieldOffsetAttribute") {
141 byte[] data = customBuilder.Data;
142 offset = (int)data [2];
143 offset |= ((int)data [3]) << 8;
144 offset |= ((int)data [4]) << 16;
145 offset |= ((int)data [5]) << 24;
147 } else if (attrname == "System.NonSerializedAttribute") {
148 attrs |= FieldAttributes.NotSerialized;
150 } else if (attrname == "System.Runtime.InteropServices.MarshalAsAttribute") {
151 attrs |= FieldAttributes.HasFieldMarshal;
152 marshal_info = CustomAttributeBuilder.get_umarshal (customBuilder, true);
153 /* FIXME: check for errors */
156 if (cattrs != null) {
157 CustomAttributeBuilder[] new_array = new CustomAttributeBuilder [cattrs.Length + 1];
158 cattrs.CopyTo (new_array, 0);
159 new_array [cattrs.Length] = customBuilder;
162 cattrs = new CustomAttributeBuilder [1];
163 cattrs [0] = customBuilder;
170 public void SetCustomAttribute( ConstructorInfo con, byte[] binaryAttribute) {
172 SetCustomAttribute (new CustomAttributeBuilder (con, binaryAttribute));
176 [Obsolete ("An alternate API is available: Emit the MarshalAs custom attribute instead.")]
178 public void SetMarshal( UnmanagedMarshal unmanagedMarshal) {
180 marshal_info = unmanagedMarshal;
181 attrs |= FieldAttributes.HasFieldMarshal;
184 public void SetOffset( int iOffset) {
189 public override void SetValue( object obj, object val, BindingFlags invokeAttr, Binder binder, CultureInfo culture) {
190 throw CreateNotSupportedException ();
193 private Exception CreateNotSupportedException ()
195 return new NotSupportedException ("The invoked member is not supported in a dynamic module.");
198 private void RejectIfCreated ()
200 if (typeb.is_created)
201 throw new InvalidOperationException ("Unable to change after type has been created.");
204 #if NET_2_0 || BOOTSTRAP_NET_2_0
205 public override FieldInfo Mono_GetGenericFieldDefinition ()
210 public override Module Module {
217 void _FieldBuilder.GetIDsOfNames ([In] ref Guid riid, IntPtr rgszNames, uint cNames, uint lcid, IntPtr rgDispId)
219 throw new NotImplementedException ();
222 void _FieldBuilder.GetTypeInfo (uint iTInfo, uint lcid, IntPtr ppTInfo)
224 throw new NotImplementedException ();
227 void _FieldBuilder.GetTypeInfoCount (out uint pcTInfo)
229 throw new NotImplementedException ();
232 void _FieldBuilder.Invoke (uint dispIdMember, [In] ref Guid riid, uint lcid, short wFlags, IntPtr pDispParams, IntPtr pVarResult, IntPtr pExcepInfo, IntPtr puArgErr)
234 throw new NotImplementedException ();