MonoField.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. //
  2. // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining
  5. // a copy of this software and associated documentation files (the
  6. // "Software"), to deal in the Software without restriction, including
  7. // without limitation the rights to use, copy, modify, merge, publish,
  8. // distribute, sublicense, and/or sell copies of the Software, and to
  9. // permit persons to whom the Software is furnished to do so, subject to
  10. // the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be
  13. // included in all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  16. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  17. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  18. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  19. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  20. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  21. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  22. //
  23. //
  24. // System.Reflection/MonoField.cs
  25. // The class used to represent Fields from the mono runtime.
  26. //
  27. // Author:
  28. // Paolo Molaro ([email protected])
  29. //
  30. // (C) 2001 Ximian, Inc. http://www.ximian.com
  31. //
  32. using System;
  33. using System.Globalization;
  34. using System.Runtime.CompilerServices;
  35. using System.Runtime.InteropServices;
  36. namespace System.Reflection {
  37. internal class MonoField : FieldInfo {
  38. internal IntPtr klass;
  39. internal RuntimeFieldHandle fhandle;
  40. string name;
  41. Type type;
  42. FieldAttributes attrs;
  43. public override FieldAttributes Attributes {
  44. get {
  45. return attrs;
  46. }
  47. }
  48. public override RuntimeFieldHandle FieldHandle {
  49. get {
  50. return fhandle;
  51. }
  52. }
  53. public override Type FieldType {
  54. get {
  55. return type;
  56. }
  57. }
  58. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  59. private extern Type GetParentType (bool declaring);
  60. public override Type ReflectedType {
  61. get {
  62. return GetParentType (false);
  63. }
  64. }
  65. public override Type DeclaringType {
  66. get {
  67. return GetParentType (true);
  68. }
  69. }
  70. public override string Name {
  71. get {
  72. return name;
  73. }
  74. }
  75. public override bool IsDefined (Type attributeType, bool inherit) {
  76. return MonoCustomAttrs.IsDefined (this, attributeType, inherit);
  77. }
  78. public override object[] GetCustomAttributes( bool inherit) {
  79. return MonoCustomAttrs.GetCustomAttributes (this, inherit);
  80. }
  81. public override object[] GetCustomAttributes( Type attributeType, bool inherit) {
  82. return MonoCustomAttrs.GetCustomAttributes (this, attributeType, inherit);
  83. }
  84. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  85. internal override extern int GetFieldOffset ();
  86. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  87. private extern object GetValueInternal (object obj);
  88. public override object GetValue (object obj)
  89. {
  90. return GetValueInternal (obj);
  91. }
  92. public override string ToString () {
  93. return String.Format ("{0} {1}", type, name);
  94. }
  95. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  96. private static extern void SetValueInternal (FieldInfo fi, object obj, object value);
  97. public override void SetValue (object obj, object val, BindingFlags invokeAttr, Binder binder, CultureInfo culture)
  98. {
  99. if (!IsStatic && obj == null)
  100. throw new ArgumentNullException ("obj");
  101. if (binder == null)
  102. binder = Binder.DefaultBinder;
  103. if (val != null) {
  104. val = binder.ChangeType (val, type, culture);
  105. if (val == null)
  106. throw new ArgumentException ("Object type cannot be converted to target type.", "val");
  107. }
  108. SetValueInternal (this, obj, val);
  109. }
  110. #if NET_2_0 || BOOTSTRAP_NET_2_0
  111. [MonoTODO]
  112. public override Type[] OptionalCustomModifiers {
  113. get {
  114. throw new NotImplementedException ();
  115. }
  116. }
  117. [MonoTODO]
  118. public override Type[] RequiredCustomModifiers {
  119. get {
  120. throw new NotImplementedException ();
  121. }
  122. }
  123. #endif
  124. #if NET_2_0 || BOOTSTRAP_NET_2_0
  125. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  126. public override extern FieldInfo Mono_GetGenericFieldDefinition ();
  127. #endif
  128. }
  129. }