[LoongArch64] Part-5:add loongarch support in some files for LoongArch64. (#21769)
[mono-project.git] / mcs / class / corlib / System.Reflection.Emit / FieldBuilder.cs
blobd38b51fa99862f913d1fce0a1991a26eea2241ad
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 #if MONO_FEATURE_SRE
35 using System;
36 using System.Reflection;
37 using System.Reflection.Emit;
38 using System.Globalization;
39 using System.Runtime.CompilerServices;
40 using System.Runtime.InteropServices;
42 namespace System.Reflection.Emit {
44 #if !MOBILE
45 [ComVisible (true)]
46 [ComDefaultInterface (typeof (_FieldBuilder))]
47 [ClassInterface (ClassInterfaceType.None)]
48 partial class FieldBuilder : _FieldBuilder
50 void _FieldBuilder.GetIDsOfNames ([In] ref Guid riid, IntPtr rgszNames, uint cNames, uint lcid, IntPtr rgDispId)
52 throw new NotImplementedException ();
55 void _FieldBuilder.GetTypeInfo (uint iTInfo, uint lcid, IntPtr ppTInfo)
57 throw new NotImplementedException ();
60 void _FieldBuilder.GetTypeInfoCount (out uint pcTInfo)
62 throw new NotImplementedException ();
65 void _FieldBuilder.Invoke (uint dispIdMember, [In] ref Guid riid, uint lcid, short wFlags, IntPtr pDispParams, IntPtr pVarResult, IntPtr pExcepInfo, IntPtr puArgErr)
67 throw new NotImplementedException ();
70 #endif
72 [StructLayout (LayoutKind.Sequential)]
73 public sealed partial class FieldBuilder : FieldInfo {
75 #pragma warning disable 169, 414
76 private FieldAttributes attrs;
77 private Type type;
78 private String name;
79 private object def_value;
80 private int offset;
81 internal TypeBuilder typeb;
82 private byte[] rva_data;
83 private CustomAttributeBuilder[] cattrs;
84 private UnmanagedMarshal marshal_info;
85 private RuntimeFieldHandle handle;
86 private Type[] modReq;
87 private Type[] modOpt;
88 #pragma warning restore 169, 414
90 internal FieldBuilder (TypeBuilder tb, string fieldName, Type type, FieldAttributes attributes, Type[] modReq, Type[] modOpt)
92 if (type == null)
93 throw new ArgumentNullException ("type");
95 attrs = attributes;
96 name = fieldName;
97 this.type = type;
98 this.modReq = modReq;
99 this.modOpt = modOpt;
100 offset = -1;
101 typeb = tb;
103 ((ModuleBuilder) tb.Module).RegisterToken (this, GetToken ().Token);
106 public override FieldAttributes Attributes {
107 get { return attrs; }
110 public override Type DeclaringType {
111 get { return typeb; }
114 public override RuntimeFieldHandle FieldHandle {
115 get {
116 throw CreateNotSupportedException ();
120 public override Type FieldType {
121 get { return type; }
124 public override string Name {
125 get { return name; }
128 public override Type ReflectedType {
129 get { return typeb; }
132 public override object[] GetCustomAttributes(bool inherit) {
134 * On MS.NET, this always returns not_supported, but we can't do this
135 * since there would be no way to obtain custom attributes of
136 * dynamically created ctors.
138 if (typeb.is_created)
139 return MonoCustomAttrs.GetCustomAttributes (this, inherit);
140 else
141 throw CreateNotSupportedException ();
144 public override object[] GetCustomAttributes(Type attributeType, bool inherit) {
145 if (typeb.is_created)
146 return MonoCustomAttrs.GetCustomAttributes (this, attributeType, inherit);
147 else
148 throw CreateNotSupportedException ();
151 public override int MetadataToken { get { return ((ModuleBuilder) typeb.Module).GetToken (this); } }
153 public FieldToken GetToken() {
154 return new FieldToken (MetadataToken);
157 public override object GetValue(object obj) {
158 throw CreateNotSupportedException ();
161 public override bool IsDefined( Type attributeType, bool inherit) {
162 throw CreateNotSupportedException ();
165 internal override int GetFieldOffset () {
166 /* FIXME: */
167 return 0;
170 internal void SetRVAData (byte[] data) {
171 rva_data = (byte[])data.Clone ();
174 public void SetConstant( object defaultValue) {
175 RejectIfCreated ();
177 /*if (defaultValue.GetType() != type)
178 throw new ArgumentException ("Constant doesn't match field type");*/
179 def_value = defaultValue;
182 public void SetCustomAttribute (CustomAttributeBuilder customBuilder) {
183 RejectIfCreated ();
185 if (customBuilder == null)
186 throw new ArgumentNullException ("customBuilder");
188 string attrname = customBuilder.Ctor.ReflectedType.FullName;
189 if (attrname == "System.Runtime.InteropServices.FieldOffsetAttribute") {
190 byte[] data = customBuilder.Data;
191 offset = (int)data [2];
192 offset |= ((int)data [3]) << 8;
193 offset |= ((int)data [4]) << 16;
194 offset |= ((int)data [5]) << 24;
195 return;
196 } else if (attrname == "System.NonSerializedAttribute") {
197 attrs |= FieldAttributes.NotSerialized;
198 return;
199 } else if (attrname == "System.Runtime.CompilerServices.SpecialNameAttribute") {
200 attrs |= FieldAttributes.SpecialName;
201 return;
202 } else if (attrname == "System.Runtime.InteropServices.MarshalAsAttribute") {
203 attrs |= FieldAttributes.HasFieldMarshal;
204 marshal_info = CustomAttributeBuilder.get_umarshal (customBuilder, true);
205 /* FIXME: check for errors */
206 return;
208 if (cattrs != null) {
209 CustomAttributeBuilder[] new_array = new CustomAttributeBuilder [cattrs.Length + 1];
210 cattrs.CopyTo (new_array, 0);
211 new_array [cattrs.Length] = customBuilder;
212 cattrs = new_array;
213 } else {
214 cattrs = new CustomAttributeBuilder [1];
215 cattrs [0] = customBuilder;
219 [ComVisible (true)]
220 public void SetCustomAttribute( ConstructorInfo con, byte[] binaryAttribute) {
221 RejectIfCreated ();
222 SetCustomAttribute (new CustomAttributeBuilder (con, binaryAttribute));
225 [Obsolete ("An alternate API is available: Emit the MarshalAs custom attribute instead.")]
226 public void SetMarshal( UnmanagedMarshal unmanagedMarshal) {
227 RejectIfCreated ();
228 marshal_info = unmanagedMarshal;
229 attrs |= FieldAttributes.HasFieldMarshal;
232 public void SetOffset( int iOffset) {
233 RejectIfCreated ();
234 if (iOffset < 0)
235 throw new ArgumentException ("Negative field offset is not allowed");
236 offset = iOffset;
239 public override void SetValue( object obj, object val, BindingFlags invokeAttr, Binder binder, CultureInfo culture) {
240 throw CreateNotSupportedException ();
243 private Exception CreateNotSupportedException ()
245 return new NotSupportedException ("The invoked member is not supported in a dynamic module.");
248 private void RejectIfCreated ()
250 if (typeb.is_created)
251 throw new InvalidOperationException ("Unable to change after type has been created.");
254 internal void ResolveUserTypes () {
255 type = TypeBuilder.ResolveUserType (type);
256 TypeBuilder.ResolveUserTypes (modReq);
257 TypeBuilder.ResolveUserTypes (modOpt);
258 if (marshal_info != null)
259 marshal_info.marshaltyperef = TypeBuilder.ResolveUserType (marshal_info.marshaltyperef);
262 internal FieldInfo RuntimeResolve () {
263 // typeb.CreateType() populates this.handle
264 var type_handle = new RuntimeTypeHandle (typeb.CreateType () as RuntimeType);
265 return FieldInfo.GetFieldFromHandle (handle, type_handle);
268 public override Module Module {
269 get {
270 return base.Module;
276 #endif