MonoField.cs 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. //
  2. // System.Reflection/MonoField.cs
  3. // The class used to represent Fields from the mono runtime.
  4. //
  5. // Author:
  6. // Paolo Molaro ([email protected])
  7. //
  8. // (C) 2001 Ximian, Inc. http://www.ximian.com
  9. //
  10. using System;
  11. using System.Globalization;
  12. using System.Runtime.CompilerServices;
  13. using System.Runtime.InteropServices;
  14. namespace System.Reflection {
  15. internal struct MonoFieldInfo {
  16. public Type parent;
  17. public Type type;
  18. public String name;
  19. public FieldAttributes attrs;
  20. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  21. internal static extern void get_field_info (MonoField field, out MonoFieldInfo info);
  22. }
  23. internal class MonoField : FieldInfo {
  24. internal IntPtr klass;
  25. internal RuntimeFieldHandle fhandle;
  26. public override FieldAttributes Attributes {
  27. get {
  28. MonoFieldInfo info;
  29. MonoFieldInfo.get_field_info (this, out info);
  30. return info.attrs;
  31. }
  32. }
  33. public override RuntimeFieldHandle FieldHandle {
  34. get {return fhandle;}
  35. }
  36. public override Type FieldType {
  37. get {
  38. MonoFieldInfo info;
  39. MonoFieldInfo.get_field_info (this, out info);
  40. return info.type;
  41. }
  42. }
  43. public override Type ReflectedType {
  44. get {
  45. MonoFieldInfo info;
  46. MonoFieldInfo.get_field_info (this, out info);
  47. return info.parent;
  48. }
  49. }
  50. public override Type DeclaringType {
  51. get {
  52. MonoFieldInfo info;
  53. MonoFieldInfo.get_field_info (this, out info);
  54. return info.parent;
  55. }
  56. }
  57. public override string Name {
  58. get {
  59. MonoFieldInfo info;
  60. MonoFieldInfo.get_field_info (this, out info);
  61. return info.name;
  62. }
  63. }
  64. public override bool IsDefined (Type attributeType, bool inherit) {
  65. return MonoCustomAttrs.IsDefined (this, attributeType, inherit);
  66. }
  67. public override object[] GetCustomAttributes( bool inherit) {
  68. return MonoCustomAttrs.GetCustomAttributes (this, inherit);
  69. }
  70. public override object[] GetCustomAttributes( Type attributeType, bool inherit) {
  71. return MonoCustomAttrs.GetCustomAttributes (this, attributeType, inherit);
  72. }
  73. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  74. private extern object GetValueInternal (object obj);
  75. public override object GetValue (object obj)
  76. {
  77. return GetValueInternal (obj);
  78. }
  79. public override string ToString () {
  80. MonoFieldInfo info;
  81. MonoFieldInfo.get_field_info (this, out info);
  82. return String.Format ("{0} {1}", info.type, info.name);
  83. }
  84. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  85. private static extern void SetValueInternal (FieldInfo fi, object obj, object value);
  86. public override void SetValue (object obj, object val, BindingFlags invokeAttr, Binder binder, CultureInfo culture)
  87. {
  88. if (!IsStatic && obj == null)
  89. throw new ArgumentNullException ("obj");
  90. if (binder == null)
  91. binder = Binder.DefaultBinder;
  92. object realval = binder.ChangeType (val, FieldType, culture);
  93. SetValueInternal (this, obj, realval);
  94. }
  95. }
  96. }