MonoField.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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.Collections.Generic;
  34. using System.Globalization;
  35. using System.Runtime.CompilerServices;
  36. using System.Runtime.InteropServices;
  37. using System.Runtime.Serialization;
  38. namespace System.Reflection {
  39. [Serializable]
  40. internal class MonoField : FieldInfo, ISerializable {
  41. internal IntPtr klass;
  42. internal RuntimeFieldHandle fhandle;
  43. string name;
  44. Type type;
  45. FieldAttributes attrs;
  46. public override FieldAttributes Attributes {
  47. get {
  48. return attrs;
  49. }
  50. }
  51. public override RuntimeFieldHandle FieldHandle {
  52. get {
  53. return fhandle;
  54. }
  55. }
  56. public override Type FieldType {
  57. get {
  58. return type;
  59. }
  60. }
  61. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  62. private extern Type GetParentType (bool declaring);
  63. public override Type ReflectedType {
  64. get {
  65. return GetParentType (false);
  66. }
  67. }
  68. public override Type DeclaringType {
  69. get {
  70. return GetParentType (true);
  71. }
  72. }
  73. public override string Name {
  74. get {
  75. return name;
  76. }
  77. }
  78. public override bool IsDefined (Type attributeType, bool inherit) {
  79. return MonoCustomAttrs.IsDefined (this, attributeType, inherit);
  80. }
  81. public override object[] GetCustomAttributes( bool inherit) {
  82. return MonoCustomAttrs.GetCustomAttributes (this, inherit);
  83. }
  84. public override object[] GetCustomAttributes( Type attributeType, bool inherit) {
  85. return MonoCustomAttrs.GetCustomAttributes (this, attributeType, inherit);
  86. }
  87. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  88. internal override extern int GetFieldOffset ();
  89. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  90. private extern object GetValueInternal (object obj);
  91. public override object GetValue (object obj)
  92. {
  93. if (!IsStatic && obj == null)
  94. throw new TargetException ("Non-static field requires a target");
  95. if (!IsLiteral)
  96. CheckGeneric ();
  97. return GetValueInternal (obj);
  98. }
  99. public override string ToString () {
  100. return String.Format ("{0} {1}", type, name);
  101. }
  102. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  103. private static extern void SetValueInternal (FieldInfo fi, object obj, object value);
  104. public override void SetValue (object obj, object val, BindingFlags invokeAttr, Binder binder, CultureInfo culture)
  105. {
  106. if (!IsStatic && obj == null)
  107. throw new TargetException ("Non-static field requires a target");
  108. if (IsLiteral)
  109. throw new FieldAccessException ("Cannot set a constant field");
  110. if (binder == null)
  111. binder = Binder.DefaultBinder;
  112. CheckGeneric ();
  113. if (val != null) {
  114. object newVal;
  115. newVal = binder.ChangeType (val, type, culture);
  116. if (newVal == null)
  117. throw new ArgumentException ("Object type " + val.GetType() + " cannot be converted to target type: " + type, "val");
  118. val = newVal;
  119. }
  120. SetValueInternal (this, obj, val);
  121. }
  122. internal MonoField Clone (string newName)
  123. {
  124. MonoField field = new MonoField ();
  125. field.name = newName;
  126. field.type = type;
  127. field.attrs = attrs;
  128. field.klass = klass;
  129. field.fhandle = fhandle;
  130. return field;
  131. }
  132. // ISerializable
  133. public void GetObjectData (SerializationInfo info, StreamingContext context)
  134. {
  135. MemberInfoSerializationHolder.Serialize (info, Name, ReflectedType,
  136. ToString(), MemberTypes.Field);
  137. }
  138. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  139. public override extern object GetRawConstantValue ();
  140. #if NET_4_0
  141. public override IList<CustomAttributeData> GetCustomAttributesData () {
  142. return CustomAttributeData.GetCustomAttributes (this);
  143. }
  144. #endif
  145. void CheckGeneric () {
  146. if (DeclaringType.ContainsGenericParameters)
  147. throw new InvalidOperationException ("Late bound operations cannot be performed on fields with types for which Type.ContainsGenericParameters is true.");
  148. }
  149. }
  150. }