2
0

FieldBuilder.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. //
  2. // System.Reflection.Emit/FieldBuilder.cs
  3. //
  4. // Author:
  5. // Paolo Molaro ([email protected])
  6. //
  7. // (C) 2001-2002 Ximian, Inc. http://www.ximian.com
  8. //
  9. using System;
  10. using System.Reflection;
  11. using System.Reflection.Emit;
  12. using System.Globalization;
  13. using System.Runtime.CompilerServices;
  14. using System.Runtime.InteropServices;
  15. namespace System.Reflection.Emit {
  16. public sealed class FieldBuilder : FieldInfo {
  17. private FieldAttributes attrs;
  18. private Type type;
  19. private String name;
  20. private object def_value;
  21. private int offset;
  22. private int table_idx;
  23. internal TypeBuilder typeb;
  24. private byte[] rva_data;
  25. private CustomAttributeBuilder[] cattrs;
  26. private UnmanagedMarshal marshal_info;
  27. private RuntimeFieldHandle handle;
  28. internal FieldBuilder (TypeBuilder tb, string fieldName, Type type, FieldAttributes attributes) {
  29. attrs = attributes;
  30. name = fieldName;
  31. this.type = type;
  32. offset = -1;
  33. typeb = tb;
  34. table_idx = tb.get_next_table_index (this, 0x04, true);
  35. }
  36. public override FieldAttributes Attributes {
  37. get {return attrs;}
  38. }
  39. public override Type DeclaringType {
  40. get {return typeb;}
  41. }
  42. public override RuntimeFieldHandle FieldHandle {
  43. get {return new RuntimeFieldHandle();}
  44. }
  45. public override Type FieldType {
  46. get {return type;}
  47. }
  48. public override string Name {
  49. get {return name;}
  50. }
  51. public override Type ReflectedType {
  52. get {return typeb;}
  53. }
  54. public override object[] GetCustomAttributes(bool inherit) {
  55. return null;
  56. }
  57. public override object[] GetCustomAttributes(Type attributeType, bool inherit) {
  58. return null;
  59. }
  60. public FieldToken GetToken() {
  61. return new FieldToken (0x04000000 | table_idx);
  62. }
  63. public override object GetValue(object obj) {
  64. return null;
  65. }
  66. public override bool IsDefined( Type attributeType, bool inherit) {
  67. return false;
  68. }
  69. internal void SetRVAData (byte[] data) {
  70. rva_data = (byte[])data.Clone ();
  71. }
  72. public void SetConstant( object defaultValue) {
  73. /*if (defaultValue.GetType() != type)
  74. throw new ArgumentException ("Constant doesn't match field type");*/
  75. def_value = defaultValue;
  76. }
  77. public void SetCustomAttribute( CustomAttributeBuilder customBuilder) {
  78. string attrname = customBuilder.Ctor.ReflectedType.FullName;
  79. if (attrname == "System.Runtime.InteropServices.FieldOffsetAttribute") {
  80. byte[] data = customBuilder.Data;
  81. offset = (int)data [2];
  82. offset |= ((int)data [3]) << 8;
  83. offset |= ((int)data [4]) << 16;
  84. offset |= ((int)data [5]) << 24;
  85. return;
  86. } else if (attrname == "System.NonSerializedAttribute") {
  87. attrs |= FieldAttributes.NotSerialized;
  88. return;
  89. } else if (attrname == "System.Runtime.InteropServices.MarshalAsAttribute") {
  90. marshal_info = CustomAttributeBuilder.get_umarshal (customBuilder, true);
  91. /* FIXME: check for errors */
  92. return;
  93. }
  94. if (cattrs != null) {
  95. CustomAttributeBuilder[] new_array = new CustomAttributeBuilder [cattrs.Length + 1];
  96. cattrs.CopyTo (new_array, 0);
  97. new_array [cattrs.Length] = customBuilder;
  98. cattrs = new_array;
  99. } else {
  100. cattrs = new CustomAttributeBuilder [1];
  101. cattrs [0] = customBuilder;
  102. }
  103. }
  104. public void SetCustomAttribute( ConstructorInfo con, byte[] binaryAttribute) {
  105. SetCustomAttribute (new CustomAttributeBuilder (con, binaryAttribute));
  106. }
  107. public void SetMarshal( UnmanagedMarshal unmanagedMarshal) {
  108. marshal_info = unmanagedMarshal;
  109. attrs |= FieldAttributes.HasFieldMarshal;
  110. }
  111. public void SetOffset( int iOffset) {
  112. offset = iOffset;
  113. }
  114. public override void SetValue( object obj, object val, BindingFlags invokeAttr, Binder binder, CultureInfo culture) {
  115. }
  116. }
  117. }