FieldBuilder.cs 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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.Emit/FieldBuilder.cs
  25. //
  26. // Author:
  27. // Paolo Molaro ([email protected])
  28. //
  29. // (C) 2001-2002 Ximian, Inc. http://www.ximian.com
  30. //
  31. using System;
  32. using System.Reflection;
  33. using System.Reflection.Emit;
  34. using System.Globalization;
  35. using System.Runtime.CompilerServices;
  36. using System.Runtime.InteropServices;
  37. namespace System.Reflection.Emit {
  38. #if NET_2_0
  39. [ComVisible (true)]
  40. [ComDefaultInterface (typeof (_FieldBuilder))]
  41. #endif
  42. [ClassInterface (ClassInterfaceType.None)]
  43. public sealed class FieldBuilder : FieldInfo, _FieldBuilder {
  44. private FieldAttributes attrs;
  45. private Type type;
  46. private String name;
  47. private object def_value;
  48. private int offset;
  49. private int table_idx;
  50. internal TypeBuilder typeb;
  51. private byte[] rva_data;
  52. private CustomAttributeBuilder[] cattrs;
  53. private UnmanagedMarshal marshal_info;
  54. private RuntimeFieldHandle handle;
  55. private Type[] modReq;
  56. private Type[] modOpt;
  57. internal FieldBuilder (TypeBuilder tb, string fieldName, Type type, FieldAttributes attributes, Type[] modReq, Type[] modOpt) {
  58. attrs = attributes;
  59. name = fieldName;
  60. this.type = type;
  61. this.modReq = modReq;
  62. this.modOpt = modOpt;
  63. offset = -1;
  64. typeb = tb;
  65. table_idx = tb.get_next_table_index (this, 0x04, true);
  66. }
  67. public override FieldAttributes Attributes {
  68. get { return attrs; }
  69. }
  70. public override Type DeclaringType {
  71. get { return typeb; }
  72. }
  73. public override RuntimeFieldHandle FieldHandle {
  74. get {
  75. throw CreateNotSupportedException ();
  76. }
  77. }
  78. public override Type FieldType {
  79. get { return type; }
  80. }
  81. public override string Name {
  82. get { return name; }
  83. }
  84. public override Type ReflectedType {
  85. get { return typeb; }
  86. }
  87. public override object[] GetCustomAttributes(bool inherit) {
  88. /*
  89. * On MS.NET, this always returns not_supported, but we can't do this
  90. * since there would be no way to obtain custom attributes of
  91. * dynamically created ctors.
  92. */
  93. if (typeb.is_created)
  94. return MonoCustomAttrs.GetCustomAttributes (this, inherit);
  95. else
  96. throw CreateNotSupportedException ();
  97. }
  98. public override object[] GetCustomAttributes(Type attributeType, bool inherit) {
  99. if (typeb.is_created)
  100. return MonoCustomAttrs.GetCustomAttributes (this, attributeType, inherit);
  101. else
  102. throw CreateNotSupportedException ();
  103. }
  104. public FieldToken GetToken() {
  105. return new FieldToken (MetadataToken);
  106. }
  107. public override object GetValue(object obj) {
  108. throw CreateNotSupportedException ();
  109. }
  110. public override bool IsDefined( Type attributeType, bool inherit) {
  111. throw CreateNotSupportedException ();
  112. }
  113. internal override int GetFieldOffset () {
  114. /* FIXME: */
  115. return 0;
  116. }
  117. internal void SetRVAData (byte[] data) {
  118. rva_data = (byte[])data.Clone ();
  119. }
  120. public void SetConstant( object defaultValue) {
  121. RejectIfCreated ();
  122. /*if (defaultValue.GetType() != type)
  123. throw new ArgumentException ("Constant doesn't match field type");*/
  124. def_value = defaultValue;
  125. }
  126. public void SetCustomAttribute (CustomAttributeBuilder customBuilder) {
  127. RejectIfCreated ();
  128. string attrname = customBuilder.Ctor.ReflectedType.FullName;
  129. if (attrname == "System.Runtime.InteropServices.FieldOffsetAttribute") {
  130. byte[] data = customBuilder.Data;
  131. offset = (int)data [2];
  132. offset |= ((int)data [3]) << 8;
  133. offset |= ((int)data [4]) << 16;
  134. offset |= ((int)data [5]) << 24;
  135. return;
  136. } else if (attrname == "System.NonSerializedAttribute") {
  137. attrs |= FieldAttributes.NotSerialized;
  138. return;
  139. #if NET_2_0
  140. } else if (attrname == "System.Runtime.CompilerServices.SpecialNameAttribute") {
  141. attrs |= FieldAttributes.SpecialName;
  142. return;
  143. #endif
  144. } else if (attrname == "System.Runtime.InteropServices.MarshalAsAttribute") {
  145. attrs |= FieldAttributes.HasFieldMarshal;
  146. marshal_info = CustomAttributeBuilder.get_umarshal (customBuilder, true);
  147. /* FIXME: check for errors */
  148. return;
  149. }
  150. if (cattrs != null) {
  151. CustomAttributeBuilder[] new_array = new CustomAttributeBuilder [cattrs.Length + 1];
  152. cattrs.CopyTo (new_array, 0);
  153. new_array [cattrs.Length] = customBuilder;
  154. cattrs = new_array;
  155. } else {
  156. cattrs = new CustomAttributeBuilder [1];
  157. cattrs [0] = customBuilder;
  158. }
  159. }
  160. #if NET_2_0
  161. [ComVisible (true)]
  162. #endif
  163. public void SetCustomAttribute( ConstructorInfo con, byte[] binaryAttribute) {
  164. RejectIfCreated ();
  165. SetCustomAttribute (new CustomAttributeBuilder (con, binaryAttribute));
  166. }
  167. #if NET_2_0
  168. [Obsolete ("An alternate API is available: Emit the MarshalAs custom attribute instead.")]
  169. #endif
  170. public void SetMarshal( UnmanagedMarshal unmanagedMarshal) {
  171. RejectIfCreated ();
  172. marshal_info = unmanagedMarshal;
  173. attrs |= FieldAttributes.HasFieldMarshal;
  174. }
  175. public void SetOffset( int iOffset) {
  176. RejectIfCreated ();
  177. offset = iOffset;
  178. }
  179. public override void SetValue( object obj, object val, BindingFlags invokeAttr, Binder binder, CultureInfo culture) {
  180. throw CreateNotSupportedException ();
  181. }
  182. internal override UnmanagedMarshal UMarshal {
  183. get {
  184. return marshal_info;
  185. }
  186. }
  187. private Exception CreateNotSupportedException ()
  188. {
  189. return new NotSupportedException ("The invoked member is not supported in a dynamic module.");
  190. }
  191. private void RejectIfCreated ()
  192. {
  193. if (typeb.is_created)
  194. throw new InvalidOperationException ("Unable to change after type has been created.");
  195. }
  196. #if NET_2_0 || BOOTSTRAP_NET_2_0
  197. public override Module Module {
  198. get {
  199. return base.Module;
  200. }
  201. }
  202. #endif
  203. void _FieldBuilder.GetIDsOfNames ([In] ref Guid riid, IntPtr rgszNames, uint cNames, uint lcid, IntPtr rgDispId)
  204. {
  205. throw new NotImplementedException ();
  206. }
  207. void _FieldBuilder.GetTypeInfo (uint iTInfo, uint lcid, IntPtr ppTInfo)
  208. {
  209. throw new NotImplementedException ();
  210. }
  211. void _FieldBuilder.GetTypeInfoCount (out uint pcTInfo)
  212. {
  213. throw new NotImplementedException ();
  214. }
  215. void _FieldBuilder.Invoke (uint dispIdMember, [In] ref Guid riid, uint lcid, short wFlags, IntPtr pDispParams, IntPtr pVarResult, IntPtr pExcepInfo, IntPtr puArgErr)
  216. {
  217. throw new NotImplementedException ();
  218. }
  219. }
  220. }