FieldInfo.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. //
  2. // System.Reflection.FieldInfo.cs
  3. //
  4. // Author:
  5. // Miguel de Icaza ([email protected])
  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).
  10. //
  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.
  29. using System.Collections.Generic;
  30. using System.Diagnostics;
  31. using System.Globalization;
  32. using System.Runtime.CompilerServices;
  33. using System.Runtime.InteropServices;
  34. namespace System.Reflection {
  35. [Serializable]
  36. partial class FieldInfo : MemberInfo {
  37. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  38. private static extern FieldInfo internal_from_handle_type (IntPtr field_handle, IntPtr type_handle);
  39. public static FieldInfo GetFieldFromHandle (RuntimeFieldHandle handle)
  40. {
  41. if (handle.Value == IntPtr.Zero)
  42. throw new ArgumentException ("The handle is invalid.");
  43. return internal_from_handle_type (handle.Value, IntPtr.Zero);
  44. }
  45. [ComVisible (false)]
  46. public static FieldInfo GetFieldFromHandle (RuntimeFieldHandle handle, RuntimeTypeHandle declaringType)
  47. {
  48. if (handle.Value == IntPtr.Zero)
  49. throw new ArgumentException ("The handle is invalid.");
  50. FieldInfo fi = internal_from_handle_type (handle.Value, declaringType.Value);
  51. if (fi == null)
  52. throw new ArgumentException ("The field handle and the type handle are incompatible.");
  53. return fi;
  54. }
  55. internal virtual int GetFieldOffset ()
  56. {
  57. throw new SystemException ("This method should not be called");
  58. }
  59. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  60. private extern MarshalAsAttribute get_marshal_info ();
  61. internal object[] GetPseudoCustomAttributes ()
  62. {
  63. int count = 0;
  64. if (IsNotSerialized)
  65. count ++;
  66. if (DeclaringType.IsExplicitLayout)
  67. count ++;
  68. MarshalAsAttribute marshalAs = get_marshal_info ();
  69. if (marshalAs != null)
  70. count ++;
  71. if (count == 0)
  72. return null;
  73. object[] attrs = new object [count];
  74. count = 0;
  75. if (IsNotSerialized)
  76. attrs [count ++] = new NonSerializedAttribute ();
  77. if (DeclaringType.IsExplicitLayout)
  78. attrs [count ++] = new FieldOffsetAttribute (GetFieldOffset ());
  79. if (marshalAs != null)
  80. attrs [count ++] = marshalAs;
  81. return attrs;
  82. }
  83. internal CustomAttributeData[] GetPseudoCustomAttributesData ()
  84. {
  85. int count = 0;
  86. if (IsNotSerialized)
  87. count++;
  88. if (DeclaringType.IsExplicitLayout)
  89. count++;
  90. MarshalAsAttribute marshalAs = get_marshal_info ();
  91. if (marshalAs != null)
  92. count++;
  93. if (count == 0)
  94. return null;
  95. CustomAttributeData[] attrsData = new CustomAttributeData [count];
  96. count = 0;
  97. if (IsNotSerialized)
  98. attrsData [count++] = new CustomAttributeData ((typeof (NonSerializedAttribute)).GetConstructor (Type.EmptyTypes));
  99. if (DeclaringType.IsExplicitLayout) {
  100. var ctorArgs = new CustomAttributeTypedArgument[] { new CustomAttributeTypedArgument (typeof (int), GetFieldOffset ()) };
  101. attrsData [count++] = new CustomAttributeData (
  102. (typeof (FieldOffsetAttribute)).GetConstructor (new[] { typeof (int) }),
  103. ctorArgs,
  104. EmptyArray<CustomAttributeNamedArgument>.Value);
  105. }
  106. if (marshalAs != null) {
  107. var ctorArgs = new CustomAttributeTypedArgument[] { new CustomAttributeTypedArgument (typeof (UnmanagedType), marshalAs.Value) };
  108. attrsData [count++] = new CustomAttributeData (
  109. (typeof (MarshalAsAttribute)).GetConstructor (new[] { typeof (UnmanagedType) }),
  110. ctorArgs,
  111. EmptyArray<CustomAttributeNamedArgument>.Value);//FIXME Get named params
  112. }
  113. return attrsData;
  114. }
  115. }
  116. }