[LoongArch64] Part-5:add loongarch support in some files for LoongArch64. (#21769)
[mono-project.git] / mcs / class / corlib / System.Reflection / FieldInfo.cs
blob747b021c9bcd5617054d76a5d4510efc56e42fb7
1 //
2 // System.Reflection.FieldInfo.cs
3 //
4 // Author:
5 // Miguel de Icaza (miguel@ximian.com)
6 //
7 // (C) Ximian, Inc. http://www.ximian.com
8 // Copyright (C) 2004-2005 Novell, Inc (http://www.novell.com)
9 // Copyright 2011 Xamarin Inc (http://www.xamarin.com).
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
18 //
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
21 //
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
30 using System.Collections.Generic;
31 using System.Diagnostics;
32 using System.Globalization;
33 using System.Runtime.CompilerServices;
34 using System.Runtime.InteropServices;
36 namespace System.Reflection {
38 [Serializable]
39 partial class FieldInfo : MemberInfo {
41 [MethodImplAttribute(MethodImplOptions.InternalCall)]
42 private static extern FieldInfo internal_from_handle_type (IntPtr field_handle, IntPtr type_handle);
44 public static FieldInfo GetFieldFromHandle (RuntimeFieldHandle handle)
46 if (handle.Value == IntPtr.Zero)
47 throw new ArgumentException ("The handle is invalid.");
48 return internal_from_handle_type (handle.Value, IntPtr.Zero);
51 [ComVisible (false)]
52 public static FieldInfo GetFieldFromHandle (RuntimeFieldHandle handle, RuntimeTypeHandle declaringType)
54 if (handle.Value == IntPtr.Zero)
55 throw new ArgumentException ("The handle is invalid.");
56 FieldInfo fi = internal_from_handle_type (handle.Value, declaringType.Value);
57 if (fi == null)
58 throw new ArgumentException ("The field handle and the type handle are incompatible.");
59 return fi;
62 internal virtual int GetFieldOffset ()
64 throw new SystemException ("This method should not be called");
67 [MethodImplAttribute(MethodImplOptions.InternalCall)]
68 private extern MarshalAsAttribute get_marshal_info ();
70 internal object[] GetPseudoCustomAttributes ()
72 int count = 0;
74 if (IsNotSerialized)
75 count ++;
77 if (DeclaringType.IsExplicitLayout)
78 count ++;
80 MarshalAsAttribute marshalAs = get_marshal_info ();
81 if (marshalAs != null)
82 count ++;
84 if (count == 0)
85 return null;
86 object[] attrs = new object [count];
87 count = 0;
89 if (IsNotSerialized)
90 attrs [count ++] = new NonSerializedAttribute ();
91 if (DeclaringType.IsExplicitLayout)
92 attrs [count ++] = new FieldOffsetAttribute (GetFieldOffset ());
93 if (marshalAs != null)
94 attrs [count ++] = marshalAs;
96 return attrs;
99 internal CustomAttributeData[] GetPseudoCustomAttributesData ()
101 int count = 0;
103 if (IsNotSerialized)
104 count++;
106 if (DeclaringType.IsExplicitLayout)
107 count++;
109 MarshalAsAttribute marshalAs = get_marshal_info ();
110 if (marshalAs != null)
111 count++;
113 if (count == 0)
114 return null;
115 CustomAttributeData[] attrsData = new CustomAttributeData [count];
116 count = 0;
118 if (IsNotSerialized)
119 attrsData [count++] = new CustomAttributeData ((typeof (NonSerializedAttribute)).GetConstructor (Type.EmptyTypes));
120 if (DeclaringType.IsExplicitLayout) {
121 var ctorArgs = new CustomAttributeTypedArgument[] { new CustomAttributeTypedArgument (typeof (int), GetFieldOffset ()) };
122 attrsData [count++] = new CustomAttributeData (
123 (typeof (FieldOffsetAttribute)).GetConstructor (new[] { typeof (int) }),
124 ctorArgs,
125 EmptyArray<CustomAttributeNamedArgument>.Value);
128 if (marshalAs != null) {
129 var ctorArgs = new CustomAttributeTypedArgument[] { new CustomAttributeTypedArgument (typeof (UnmanagedType), marshalAs.Value) };
130 attrsData [count++] = new CustomAttributeData (
131 (typeof (MarshalAsAttribute)).GetConstructor (new[] { typeof (UnmanagedType) }),
132 ctorArgs,
133 EmptyArray<CustomAttributeNamedArgument>.Value);//FIXME Get named params
136 return attrsData;