EnumBuilder.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447
  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/EnumBuilder.cs
  25. //
  26. // Author:
  27. // Paolo Molaro ([email protected])
  28. //
  29. // (C) 2001 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 (_EnumBuilder))]
  42. [ClassInterface (ClassInterfaceType.None)]
  43. partial class EnumBuilder : _EnumBuilder
  44. {
  45. void _EnumBuilder.GetIDsOfNames ([In] ref Guid riid, IntPtr rgszNames, uint cNames, uint lcid, IntPtr rgDispId)
  46. {
  47. throw new NotImplementedException ();
  48. }
  49. void _EnumBuilder.GetTypeInfo (uint iTInfo, uint lcid, IntPtr ppTInfo)
  50. {
  51. throw new NotImplementedException ();
  52. }
  53. void _EnumBuilder.GetTypeInfoCount (out uint pcTInfo)
  54. {
  55. throw new NotImplementedException ();
  56. }
  57. void _EnumBuilder.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. public sealed partial class EnumBuilder : TypeInfo
  64. {
  65. private TypeBuilder _tb;
  66. private FieldBuilder _underlyingField;
  67. private Type _underlyingType;
  68. internal EnumBuilder (ModuleBuilder mb, string name, TypeAttributes visibility, Type underlyingType)
  69. {
  70. _tb = new TypeBuilder (mb, name, (visibility | TypeAttributes.Sealed),
  71. typeof(Enum), null, PackingSize.Unspecified, 0, null);
  72. _underlyingType = underlyingType;
  73. _underlyingField = _tb.DefineField ("value__", underlyingType,
  74. (FieldAttributes.SpecialName | FieldAttributes.Private | FieldAttributes.RTSpecialName));
  75. setup_enum_type (_tb);
  76. }
  77. internal TypeBuilder GetTypeBuilder ()
  78. {
  79. return _tb;
  80. }
  81. internal override Type InternalResolve ()
  82. {
  83. return _tb.InternalResolve ();
  84. }
  85. internal override Type RuntimeResolve () {
  86. return _tb.RuntimeResolve ();
  87. }
  88. public override Assembly Assembly {
  89. get {
  90. return _tb.Assembly;
  91. }
  92. }
  93. public override string AssemblyQualifiedName {
  94. get {
  95. return _tb.AssemblyQualifiedName;
  96. }
  97. }
  98. public override Type BaseType {
  99. get {
  100. return _tb.BaseType;
  101. }
  102. }
  103. public override Type DeclaringType {
  104. get {
  105. return _tb.DeclaringType;
  106. }
  107. }
  108. public override string FullName {
  109. get {
  110. return _tb.FullName;
  111. }
  112. }
  113. public override Guid GUID {
  114. get {
  115. return _tb.GUID;
  116. }
  117. }
  118. public override Module Module {
  119. get {
  120. return _tb.Module;
  121. }
  122. }
  123. public override string Name {
  124. get {
  125. return _tb.Name;
  126. }
  127. }
  128. public override string Namespace {
  129. get {
  130. return _tb.Namespace;
  131. }
  132. }
  133. public override Type ReflectedType {
  134. get {
  135. return _tb.ReflectedType;
  136. }
  137. }
  138. public override RuntimeTypeHandle TypeHandle {
  139. get {
  140. return _tb.TypeHandle;
  141. }
  142. }
  143. public TypeToken TypeToken {
  144. get {
  145. return _tb.TypeToken;
  146. }
  147. }
  148. public FieldBuilder UnderlyingField {
  149. get {
  150. return _underlyingField;
  151. }
  152. }
  153. public override Type UnderlyingSystemType {
  154. get {
  155. return _underlyingType;
  156. }
  157. }
  158. public Type CreateType ()
  159. {
  160. Type res = _tb.CreateType ();
  161. return res;
  162. }
  163. public TypeInfo CreateTypeInfo()
  164. {
  165. return _tb.CreateTypeInfo ();
  166. }
  167. public override Type GetEnumUnderlyingType ()
  168. {
  169. return _underlyingType;
  170. }
  171. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  172. private extern void setup_enum_type (Type t);
  173. public FieldBuilder DefineLiteral (string literalName, object literalValue)
  174. {
  175. Type fieldType = this;
  176. FieldBuilder fieldBuilder = _tb.DefineField (literalName,
  177. fieldType, (FieldAttributes.Literal |
  178. (FieldAttributes.Static | FieldAttributes.Public)));
  179. fieldBuilder.SetConstant (literalValue);
  180. return fieldBuilder;
  181. }
  182. protected override TypeAttributes GetAttributeFlagsImpl ()
  183. {
  184. return _tb.attrs;
  185. }
  186. protected override ConstructorInfo GetConstructorImpl (
  187. BindingFlags bindingAttr, Binder binder, CallingConventions callConvention,
  188. Type[] types, ParameterModifier[] modifiers)
  189. {
  190. return _tb.GetConstructor (bindingAttr, binder, callConvention, types,
  191. modifiers);
  192. }
  193. [ComVisible (true)]
  194. public override ConstructorInfo[] GetConstructors(BindingFlags bindingAttr)
  195. {
  196. return _tb.GetConstructors (bindingAttr);
  197. }
  198. public override object[] GetCustomAttributes(bool inherit)
  199. {
  200. return _tb.GetCustomAttributes (inherit);
  201. }
  202. public override object[] GetCustomAttributes(Type attributeType, bool inherit)
  203. {
  204. return _tb.GetCustomAttributes (attributeType, inherit);
  205. }
  206. public override Type GetElementType()
  207. {
  208. return _tb.GetElementType ();
  209. }
  210. public override EventInfo GetEvent( string name, BindingFlags bindingAttr)
  211. {
  212. return _tb.GetEvent (name, bindingAttr);
  213. }
  214. public override EventInfo[] GetEvents()
  215. {
  216. return _tb.GetEvents ();
  217. }
  218. public override EventInfo[] GetEvents( BindingFlags bindingAttr)
  219. {
  220. return _tb.GetEvents (bindingAttr);
  221. }
  222. public override FieldInfo GetField( string name, BindingFlags bindingAttr)
  223. {
  224. return _tb.GetField (name, bindingAttr);
  225. }
  226. public override FieldInfo[] GetFields( BindingFlags bindingAttr)
  227. {
  228. return _tb.GetFields (bindingAttr);
  229. }
  230. public override Type GetInterface (string name, bool ignoreCase)
  231. {
  232. return _tb.GetInterface (name, ignoreCase);
  233. }
  234. [ComVisible (true)]
  235. public override InterfaceMapping GetInterfaceMap (Type interfaceType)
  236. {
  237. return _tb.GetInterfaceMap (interfaceType);
  238. }
  239. public override Type[] GetInterfaces()
  240. {
  241. return _tb.GetInterfaces ();
  242. }
  243. public override MemberInfo[] GetMember (string name, MemberTypes type, BindingFlags bindingAttr)
  244. {
  245. return _tb.GetMember (name, type, bindingAttr);
  246. }
  247. public override MemberInfo[] GetMembers(BindingFlags bindingAttr)
  248. {
  249. return _tb.GetMembers (bindingAttr);
  250. }
  251. protected override MethodInfo GetMethodImpl (
  252. string name, BindingFlags bindingAttr, Binder binder,
  253. CallingConventions callConvention, Type[] types,
  254. ParameterModifier[] modifiers)
  255. {
  256. if (types == null) {
  257. return _tb.GetMethod (name, bindingAttr);
  258. }
  259. return _tb.GetMethod (name, bindingAttr, binder,
  260. callConvention, types, modifiers);
  261. }
  262. public override MethodInfo[] GetMethods (BindingFlags bindingAttr)
  263. {
  264. return _tb.GetMethods (bindingAttr);
  265. }
  266. public override Type GetNestedType (string name, BindingFlags bindingAttr)
  267. {
  268. return _tb.GetNestedType (name, bindingAttr);
  269. }
  270. public override Type[] GetNestedTypes (BindingFlags bindingAttr)
  271. {
  272. return _tb.GetNestedTypes (bindingAttr);
  273. }
  274. public override PropertyInfo[] GetProperties (BindingFlags bindingAttr)
  275. {
  276. return _tb.GetProperties (bindingAttr);
  277. }
  278. protected override PropertyInfo GetPropertyImpl (
  279. string name, BindingFlags bindingAttr, Binder binder,
  280. Type returnType, Type[] types,
  281. ParameterModifier[] modifiers)
  282. {
  283. throw CreateNotSupportedException ();
  284. }
  285. protected override bool HasElementTypeImpl ()
  286. {
  287. return _tb.HasElementType;
  288. }
  289. public override object InvokeMember (
  290. string name, BindingFlags invokeAttr, Binder binder,
  291. object target, object[] args,
  292. ParameterModifier[] modifiers, CultureInfo culture,
  293. string[] namedParameters)
  294. {
  295. return _tb.InvokeMember (name, invokeAttr, binder, target,
  296. args, modifiers, culture, namedParameters);
  297. }
  298. protected override bool IsArrayImpl()
  299. {
  300. return false;
  301. }
  302. protected override bool IsByRefImpl()
  303. {
  304. return false;
  305. }
  306. protected override bool IsCOMObjectImpl()
  307. {
  308. return false;
  309. }
  310. protected override bool IsPointerImpl()
  311. {
  312. return false;
  313. }
  314. protected override bool IsPrimitiveImpl()
  315. {
  316. return false;
  317. }
  318. protected override bool IsValueTypeImpl()
  319. {
  320. return true;
  321. }
  322. public override bool IsDefined (Type attributeType, bool inherit)
  323. {
  324. return _tb.IsDefined (attributeType, inherit);
  325. }
  326. public override Type MakeArrayType ()
  327. {
  328. return new ArrayType (this, 0);
  329. }
  330. public override Type MakeArrayType (int rank)
  331. {
  332. if (rank < 1)
  333. throw new IndexOutOfRangeException ();
  334. return new ArrayType (this, rank);
  335. }
  336. public override Type MakeByRefType ()
  337. {
  338. return new ByRefType (this);
  339. }
  340. public override Type MakePointerType ()
  341. {
  342. return new PointerType (this);
  343. }
  344. public void SetCustomAttribute (CustomAttributeBuilder customBuilder)
  345. {
  346. _tb.SetCustomAttribute (customBuilder);
  347. }
  348. [ComVisible (true)]
  349. public void SetCustomAttribute (ConstructorInfo con, byte[] binaryAttribute)
  350. {
  351. SetCustomAttribute (new CustomAttributeBuilder (con, binaryAttribute));
  352. }
  353. private Exception CreateNotSupportedException ()
  354. {
  355. return new NotSupportedException ("The invoked member is not supported in a dynamic module.");
  356. }
  357. internal override bool IsUserType {
  358. get {
  359. return false;
  360. }
  361. }
  362. public override bool IsConstructedGenericType {
  363. get { return false; }
  364. }
  365. public override bool IsAssignableFrom (TypeInfo typeInfo)
  366. {
  367. return base.IsAssignableFrom (typeInfo);
  368. }
  369. public override bool IsTypeDefinition => true;
  370. }
  371. }
  372. #endif