2 // System.Reflection.FieldInfo.cs
5 // Miguel de Icaza (miguel@ximian.com)
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:
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
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
{
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
);
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
);
58 throw new ArgumentException ("The field handle and the type handle are incompatible.");
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 ()
77 if (DeclaringType
.IsExplicitLayout
)
80 MarshalAsAttribute marshalAs
= get_marshal_info ();
81 if (marshalAs
!= null)
86 object[] attrs
= new object [count
];
90 attrs
[count
++] = new NonSerializedAttribute ();
91 if (DeclaringType
.IsExplicitLayout
)
92 attrs
[count
++] = new FieldOffsetAttribute (GetFieldOffset ());
93 if (marshalAs
!= null)
94 attrs
[count
++] = marshalAs
;
99 internal CustomAttributeData
[] GetPseudoCustomAttributesData ()
106 if (DeclaringType
.IsExplicitLayout
)
109 MarshalAsAttribute marshalAs
= get_marshal_info ();
110 if (marshalAs
!= null)
115 CustomAttributeData
[] attrsData
= new CustomAttributeData
[count
];
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) }
),
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) }
),
133 EmptyArray
<CustomAttributeNamedArgument
>.Value
);//FIXME Get named params