MonoField.cs 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  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. using System.Diagnostics;
  39. using System.Diagnostics.Contracts;
  40. namespace System.Reflection {
  41. abstract class RuntimeFieldInfo : FieldInfo, ISerializable
  42. {
  43. internal BindingFlags BindingFlags {
  44. get {
  45. return 0;
  46. }
  47. }
  48. internal RuntimeType GetDeclaringTypeInternal ()
  49. {
  50. return (RuntimeType) DeclaringType;
  51. }
  52. RuntimeType ReflectedTypeInternal {
  53. get {
  54. return (RuntimeType) ReflectedType;
  55. }
  56. }
  57. #region ISerializable Implementation
  58. public void GetObjectData(SerializationInfo info, StreamingContext context)
  59. {
  60. if (info == null)
  61. throw new ArgumentNullException("info");
  62. Contract.EndContractBlock();
  63. MemberInfoSerializationHolder.GetSerializationInfo(
  64. info,
  65. Name,
  66. ReflectedTypeInternal,
  67. ToString(),
  68. MemberTypes.Field);
  69. }
  70. #endregion
  71. }
  72. abstract class RtFieldInfo : RuntimeFieldInfo
  73. {
  74. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  75. internal extern object UnsafeGetValue (object obj);
  76. internal void CheckConsistency(Object target)
  77. {
  78. // only test instance fields
  79. if ((Attributes & FieldAttributes.Static) != FieldAttributes.Static)
  80. {
  81. if (!DeclaringType.IsInstanceOfType(target))
  82. {
  83. if (target == null)
  84. {
  85. #if FEATURE_LEGACYNETCF
  86. if (CompatibilitySwitches.IsAppEarlierThanWindowsPhone8)
  87. throw new ArgumentNullException(Environment.GetResourceString("RFLCT.Targ_StatFldReqTarg"));
  88. else
  89. #endif
  90. throw new TargetException(Environment.GetResourceString("RFLCT.Targ_StatFldReqTarg"));
  91. }
  92. else
  93. {
  94. throw new ArgumentException(
  95. String.Format(CultureInfo.CurrentUICulture, Environment.GetResourceString("Arg_FieldDeclTarget"),
  96. Name, DeclaringType, target.GetType()));
  97. }
  98. }
  99. }
  100. }
  101. [DebuggerStepThroughAttribute]
  102. [Diagnostics.DebuggerHidden]
  103. internal void UnsafeSetValue (Object obj, Object value, BindingFlags invokeAttr, Binder binder, CultureInfo culture)
  104. {
  105. bool domainInitialized = false;
  106. RuntimeFieldHandle.SetValue (this, obj, value, null, Attributes, null, ref domainInitialized);
  107. }
  108. [DebuggerStepThroughAttribute]
  109. [Diagnostics.DebuggerHidden]
  110. public override void SetValueDirect(TypedReference obj, Object value)
  111. {
  112. if (obj.IsNull)
  113. throw new ArgumentException(Environment.GetResourceString("Arg_TypedReference_Null"));
  114. Contract.EndContractBlock();
  115. unsafe
  116. {
  117. // Passing TypedReference by reference is easier to make correct in native code
  118. RuntimeFieldHandle.SetValueDirect(this, (RuntimeType)FieldType, &obj, value, (RuntimeType)DeclaringType);
  119. }
  120. }
  121. [DebuggerStepThroughAttribute]
  122. [Diagnostics.DebuggerHidden]
  123. public override Object GetValueDirect(TypedReference obj)
  124. {
  125. if (obj.IsNull)
  126. throw new ArgumentException(Environment.GetResourceString("Arg_TypedReference_Null"));
  127. Contract.EndContractBlock();
  128. unsafe
  129. {
  130. // Passing TypedReference by reference is easier to make correct in native code
  131. return RuntimeFieldHandle.GetValueDirect(this, (RuntimeType)FieldType, &obj, (RuntimeType)DeclaringType);
  132. }
  133. }
  134. }
  135. [Serializable]
  136. [StructLayout (LayoutKind.Sequential)]
  137. internal class MonoField : RtFieldInfo {
  138. internal IntPtr klass;
  139. internal RuntimeFieldHandle fhandle;
  140. string name;
  141. Type type;
  142. FieldAttributes attrs;
  143. public override FieldAttributes Attributes {
  144. get {
  145. return attrs;
  146. }
  147. }
  148. public override RuntimeFieldHandle FieldHandle {
  149. get {
  150. return fhandle;
  151. }
  152. }
  153. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  154. extern Type ResolveType ();
  155. public override Type FieldType {
  156. get {
  157. if (type == null)
  158. type = ResolveType ();
  159. return type;
  160. }
  161. }
  162. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  163. private extern Type GetParentType (bool declaring);
  164. public override Type ReflectedType {
  165. get {
  166. return GetParentType (false);
  167. }
  168. }
  169. public override Type DeclaringType {
  170. get {
  171. return GetParentType (true);
  172. }
  173. }
  174. public override string Name {
  175. get {
  176. return name;
  177. }
  178. }
  179. public override bool IsDefined (Type attributeType, bool inherit) {
  180. return MonoCustomAttrs.IsDefined (this, attributeType, inherit);
  181. }
  182. public override object[] GetCustomAttributes( bool inherit) {
  183. return MonoCustomAttrs.GetCustomAttributes (this, inherit);
  184. }
  185. public override object[] GetCustomAttributes( Type attributeType, bool inherit) {
  186. return MonoCustomAttrs.GetCustomAttributes (this, attributeType, inherit);
  187. }
  188. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  189. internal override extern int GetFieldOffset ();
  190. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  191. private extern object GetValueInternal (object obj);
  192. public override object GetValue (object obj)
  193. {
  194. if (!IsStatic) {
  195. if (obj == null)
  196. throw new TargetException ("Non-static field requires a target");
  197. if (!DeclaringType.IsAssignableFrom (obj.GetType ()))
  198. throw new ArgumentException (string.Format (
  199. "Field {0} defined on type {1} is not a field on the target object which is of type {2}.",
  200. Name, DeclaringType, obj.GetType ()),
  201. "obj");
  202. }
  203. if (!IsLiteral)
  204. CheckGeneric ();
  205. return GetValueInternal (obj);
  206. }
  207. public override string ToString () {
  208. return String.Format ("{0} {1}", FieldType, name);
  209. }
  210. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  211. private static extern void SetValueInternal (FieldInfo fi, object obj, object value);
  212. public override void SetValue (object obj, object val, BindingFlags invokeAttr, Binder binder, CultureInfo culture)
  213. {
  214. if (!IsStatic) {
  215. if (obj == null)
  216. throw new TargetException ("Non-static field requires a target");
  217. if (!DeclaringType.IsAssignableFrom (obj.GetType ()))
  218. throw new ArgumentException (string.Format (
  219. "Field {0} defined on type {1} is not a field on the target object which is of type {2}.",
  220. Name, DeclaringType, obj.GetType ()),
  221. "obj");
  222. }
  223. if (IsLiteral)
  224. throw new FieldAccessException ("Cannot set a constant field");
  225. if (binder == null)
  226. binder = Type.DefaultBinder;
  227. CheckGeneric ();
  228. if (val != null) {
  229. RuntimeType fieldType = (RuntimeType) FieldType;
  230. val = fieldType.CheckValue (val, binder, culture, invokeAttr);
  231. }
  232. SetValueInternal (this, obj, val);
  233. }
  234. internal MonoField Clone (string newName)
  235. {
  236. MonoField field = new MonoField ();
  237. field.name = newName;
  238. field.type = type;
  239. field.attrs = attrs;
  240. field.klass = klass;
  241. field.fhandle = fhandle;
  242. return field;
  243. }
  244. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  245. public override extern object GetRawConstantValue ();
  246. public override IList<CustomAttributeData> GetCustomAttributesData () {
  247. return CustomAttributeData.GetCustomAttributes (this);
  248. }
  249. void CheckGeneric () {
  250. if (DeclaringType.ContainsGenericParameters)
  251. throw new InvalidOperationException ("Late bound operations cannot be performed on fields with types for which Type.ContainsGenericParameters is true.");
  252. }
  253. //seclevel { transparent = 0, safe-critical = 1, critical = 2}
  254. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  255. public extern int get_core_clr_security_level ();
  256. public override bool IsSecurityTransparent {
  257. get { return get_core_clr_security_level () == 0; }
  258. }
  259. public override bool IsSecurityCritical {
  260. get { return get_core_clr_security_level () > 0; }
  261. }
  262. public override bool IsSecuritySafeCritical {
  263. get { return get_core_clr_security_level () == 1; }
  264. }
  265. }
  266. }