2
0

RuntimeFieldInfo.cs 11 KB

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