FieldBuilder.cs 8.0 KB

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