MonoGenericClass.cs 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051
  1. //
  2. // System.Reflection.MonoGenericClass
  3. //
  4. // Sean MacIsaac ([email protected])
  5. // Paolo Molaro ([email protected])
  6. // Patrik Torstensson ([email protected])
  7. //
  8. // (C) 2001 Ximian, Inc.
  9. //
  10. //
  11. // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
  12. //
  13. // Permission is hereby granted, free of charge, to any person obtaining
  14. // a copy of this software and associated documentation files (the
  15. // "Software"), to deal in the Software without restriction, including
  16. // without limitation the rights to use, copy, modify, merge, publish,
  17. // distribute, sublicense, and/or sell copies of the Software, and to
  18. // permit persons to whom the Software is furnished to do so, subject to
  19. // the following conditions:
  20. //
  21. // The above copyright notice and this permission notice shall be
  22. // included in all copies or substantial portions of the Software.
  23. //
  24. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  25. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  26. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  27. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  28. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  29. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  30. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  31. //
  32. using System.Reflection;
  33. using System.Reflection.Emit;
  34. using System.Collections;
  35. using System.Runtime.CompilerServices;
  36. using System.Globalization;
  37. using System.Runtime.Serialization;
  38. using System.Text;
  39. namespace System.Reflection
  40. {
  41. /*
  42. * MonoGenericClass represents an instantiation of a generic TypeBuilder. MS
  43. * calls this class TypeBuilderInstantiation (a much better name). MS returns
  44. * NotImplementedException for many of the methods but we can't do that as gmcs
  45. * depends on them.
  46. */
  47. internal class MonoGenericClass : Type
  48. {
  49. #region Keep in sync with object-internals.h
  50. #pragma warning disable 649
  51. internal Type generic_type;
  52. Type[] type_arguments;
  53. bool initialized;
  54. #pragma warning restore 649
  55. #endregion
  56. Hashtable fields, ctors, methods;
  57. int event_count;
  58. int is_compiler_context;
  59. internal MonoGenericClass ()
  60. {
  61. // this should not be used
  62. throw new InvalidOperationException ();
  63. }
  64. internal MonoGenericClass (Type tb, Type[] args)
  65. {
  66. this.generic_type = tb;
  67. this.type_arguments = args;
  68. register_with_runtime (this); /*Temporary hack while*/
  69. }
  70. internal override bool IsCompilerContext {
  71. get {
  72. if (is_compiler_context == 0) {
  73. bool is_cc = generic_type.IsCompilerContext;
  74. foreach (Type t in type_arguments)
  75. is_cc |= t.IsCompilerContext;
  76. is_compiler_context = is_cc ? 1 : -1;
  77. }
  78. return is_compiler_context == 1;
  79. }
  80. }
  81. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  82. extern void initialize (MethodInfo[] methods, ConstructorInfo[] ctors, FieldInfo[] fields, PropertyInfo[] properties, EventInfo[] events);
  83. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  84. internal static extern void register_with_runtime (Type type);
  85. EventInfo[] GetEventsFromGTD (BindingFlags flags) {
  86. TypeBuilder tb = generic_type as TypeBuilder;
  87. if (tb == null)
  88. return generic_type.GetEvents (flags);
  89. return tb.GetEvents_internal (flags);
  90. }
  91. ConstructorInfo[] GetConstructorsFromGTD (BindingFlags flags)
  92. {
  93. TypeBuilder tb = generic_type as TypeBuilder;
  94. if (tb == null)
  95. return generic_type.GetConstructors (flags);
  96. return tb.GetConstructorsInternal (flags);
  97. }
  98. /*
  99. MethodInfo[] GetMethodsFromGTD (BindingFlags bf)
  100. {
  101. TypeBuilder tb = generic_type as TypeBuilder;
  102. if (tb == null)
  103. return generic_type.GetMethods (bf);
  104. MethodInfo[] res = new MethodInfo [tb.num_methods];
  105. if (tb.num_methods > 0)
  106. Array.Copy (tb.methods, res, tb.num_methods);
  107. return res;
  108. }
  109. */
  110. FieldInfo[] GetFieldsFromGTD (BindingFlags bf)
  111. {
  112. TypeBuilder tb = generic_type as TypeBuilder;
  113. if (tb == null)
  114. return generic_type.GetFields (bf);
  115. FieldInfo[] res = new FieldInfo [tb.num_fields];
  116. if (tb.num_fields > 0)
  117. Array.Copy (tb.fields, res, tb.num_fields);
  118. return res;
  119. }
  120. /*@hint might not be honored so it required aditional filtering
  121. TODO move filtering into here for the TypeBuilder case and remove the hint ugliness
  122. */
  123. MethodInfo[] GetMethodsFromGTDWithHint (BindingFlags hint)
  124. {
  125. TypeBuilder tb = generic_type as TypeBuilder;
  126. if (tb == null)
  127. return generic_type.GetMethods (hint);
  128. if (tb.num_methods == 0)
  129. return new MethodInfo [0];
  130. MethodInfo[] res = new MethodInfo [tb.num_methods];
  131. Array.Copy (tb.methods, 0, res, 0, tb.num_methods);
  132. return res;
  133. }
  134. /*@hint might not be honored so it required aditional filtering
  135. TODO move filtering into here for the TypeBuilder case and remove the hint ugliness
  136. */
  137. ConstructorInfo[] GetConstructorsFromGTDWithHint (BindingFlags hint)
  138. {
  139. TypeBuilder tb = generic_type as TypeBuilder;
  140. if (tb == null)
  141. return generic_type.GetConstructors (hint);
  142. if (tb.ctors == null)
  143. return new ConstructorInfo [0];
  144. ConstructorInfo[] res = new ConstructorInfo [tb.ctors.Length];
  145. tb.ctors.CopyTo (res, 0);
  146. return res;
  147. }
  148. static Type PeelType (Type t) {
  149. if (t.HasElementType)
  150. return PeelType (t.GetElementType ());
  151. if (t.IsGenericType && !t.IsGenericParameter)
  152. return t.GetGenericTypeDefinition ();
  153. return t;
  154. }
  155. static PropertyInfo[] GetPropertiesInternal (Type type, BindingFlags bf)
  156. {
  157. TypeBuilder tb = type as TypeBuilder;
  158. if (tb != null)
  159. return tb.properties;
  160. return type.GetProperties (bf);
  161. }
  162. Type[] GetInterfacesFromGTD ()
  163. {
  164. TypeBuilder tb = generic_type as TypeBuilder;
  165. if (tb != null)
  166. return tb.interfaces;
  167. return generic_type.GetInterfaces ();
  168. }
  169. internal bool IsCreated {
  170. get {
  171. TypeBuilder tb = generic_type as TypeBuilder;
  172. return tb != null ? tb.is_created : true;
  173. }
  174. }
  175. private const BindingFlags flags = BindingFlags.Public | BindingFlags.NonPublic |
  176. BindingFlags.Static | BindingFlags.Instance | BindingFlags.DeclaredOnly;
  177. void initialize ()
  178. {
  179. if (initialized)
  180. return;
  181. MonoGenericClass parent = GetParentType () as MonoGenericClass;
  182. if (parent != null)
  183. parent.initialize ();
  184. EventInfo[] events = GetEventsFromGTD (flags);
  185. event_count = events.Length;
  186. initialize (generic_type.GetMethods (flags),
  187. GetConstructorsFromGTD (flags),
  188. generic_type.GetFields (flags),
  189. generic_type.GetProperties (flags),
  190. events);
  191. initialized = true;
  192. }
  193. Type GetParentType ()
  194. {
  195. return InflateType (generic_type.BaseType);
  196. }
  197. internal Type InflateType (Type type)
  198. {
  199. return InflateType (type, type_arguments, null);
  200. }
  201. internal Type InflateType (Type type, Type[] method_args)
  202. {
  203. return InflateType (type, type_arguments, method_args);
  204. }
  205. internal static Type InflateType (Type type, Type[] type_args, Type[] method_args)
  206. {
  207. if (type == null)
  208. return null;
  209. if (!type.IsGenericParameter && !type.ContainsGenericParameters)
  210. return type;
  211. if (type.IsGenericParameter) {
  212. if (type.DeclaringMethod == null)
  213. return type_args == null ? type : type_args [type.GenericParameterPosition];
  214. return method_args == null ? type : method_args [type.GenericParameterPosition];
  215. }
  216. if (type.IsPointer)
  217. return InflateType (type.GetElementType (), type_args, method_args).MakePointerType ();
  218. if (type.IsByRef)
  219. return InflateType (type.GetElementType (), type_args, method_args).MakeByRefType ();
  220. if (type.IsArray) {
  221. if (type.GetArrayRank () > 1)
  222. return InflateType (type.GetElementType (), type_args, method_args).MakeArrayType (type.GetArrayRank ());
  223. #if BOOTSTRAP_NET_2_0
  224. if (type.ToString ().EndsWith ("[*]"))
  225. #else
  226. if (type.ToString ().EndsWith ("[*]", StringComparison.Ordinal)) /*FIXME, the reflection API doesn't offer a way around this*/
  227. #endif
  228. return InflateType (type.GetElementType (), type_args, method_args).MakeArrayType (1);
  229. return InflateType (type.GetElementType (), type_args, method_args).MakeArrayType ();
  230. }
  231. Type[] args = type.GetGenericArguments ();
  232. for (int i = 0; i < args.Length; ++i)
  233. args [i] = InflateType (args [i], type_args, method_args);
  234. Type gtd = type.IsGenericTypeDefinition ? type : type.GetGenericTypeDefinition ();
  235. return gtd.MakeGenericType (args);
  236. }
  237. public override Type BaseType {
  238. get {
  239. Type parent = GetParentType ();
  240. return parent != null ? parent : generic_type.BaseType;
  241. }
  242. }
  243. Type[] GetInterfacesInternal ()
  244. {
  245. Type[] ifaces = GetInterfacesFromGTD ();
  246. if (ifaces == null)
  247. return new Type [0];
  248. Type[] res = new Type [ifaces.Length];
  249. for (int i = 0; i < res.Length; ++i)
  250. res [i] = InflateType (ifaces [i]);
  251. return res;
  252. }
  253. public override Type[] GetInterfaces ()
  254. {
  255. if (!IsCompilerContext) {
  256. Console.WriteLine ("--FAIL {0}", this);
  257. Console.WriteLine ("\tgt {0}/{1}/{2}", generic_type, generic_type.IsCompilerContext, generic_type.GetType ());
  258. foreach (Type t in type_arguments)
  259. Console.WriteLine ("\targ {0}/{1}/{2}", t, t.IsCompilerContext, t.GetType ());
  260. throw new NotSupportedException ();
  261. }
  262. return GetInterfacesInternal ();
  263. }
  264. protected override bool IsValueTypeImpl ()
  265. {
  266. return generic_type.IsValueType;
  267. }
  268. internal override MethodInfo GetMethod (MethodInfo fromNoninstanciated)
  269. {
  270. initialize ();
  271. if (methods == null)
  272. methods = new Hashtable ();
  273. if (!methods.ContainsKey (fromNoninstanciated))
  274. methods [fromNoninstanciated] = new MethodOnTypeBuilderInst (this, fromNoninstanciated);
  275. return (MethodInfo)methods [fromNoninstanciated];
  276. }
  277. internal override ConstructorInfo GetConstructor (ConstructorInfo fromNoninstanciated)
  278. {
  279. initialize ();
  280. if (ctors == null)
  281. ctors = new Hashtable ();
  282. if (!ctors.ContainsKey (fromNoninstanciated))
  283. ctors [fromNoninstanciated] = new ConstructorOnTypeBuilderInst (this, fromNoninstanciated);
  284. return (ConstructorInfo)ctors [fromNoninstanciated];
  285. }
  286. internal override FieldInfo GetField (FieldInfo fromNoninstanciated)
  287. {
  288. initialize ();
  289. if (fields == null)
  290. fields = new Hashtable ();
  291. if (!fields.ContainsKey (fromNoninstanciated))
  292. fields [fromNoninstanciated] = new FieldOnTypeBuilderInst (this, fromNoninstanciated);
  293. return (FieldInfo)fields [fromNoninstanciated];
  294. }
  295. public override MethodInfo[] GetMethods (BindingFlags bf)
  296. {
  297. if (!IsCompilerContext)
  298. throw new NotSupportedException ();
  299. ArrayList l = new ArrayList ();
  300. //
  301. // Walk up our class hierarchy and retrieve methods from our
  302. // parent classes.
  303. //
  304. if (!(generic_type is TypeBuilder)) {
  305. foreach (var method in generic_type.GetMethods (bf)) {
  306. var m = method;
  307. if (m.DeclaringType.IsGenericTypeDefinition)
  308. m = TypeBuilder.GetMethod (this, m);
  309. l.Add (m);
  310. }
  311. } else {
  312. Type current_type = this;
  313. do {
  314. MonoGenericClass gi = current_type as MonoGenericClass;
  315. if (gi != null)
  316. l.AddRange (gi.GetMethodsInternal (bf, this));
  317. else if (current_type is TypeBuilder)
  318. l.AddRange (current_type.GetMethods (bf));
  319. else {
  320. // If we encounter a `MonoType', its
  321. // GetMethodsByName() will return all the methods
  322. // from its parent type(s), so we can stop here.
  323. MonoType mt = (MonoType) current_type;
  324. l.AddRange (mt.GetMethodsByName (null, bf, false, this));
  325. break;
  326. }
  327. if ((bf & BindingFlags.DeclaredOnly) != 0)
  328. break;
  329. current_type = current_type.BaseType;
  330. } while (current_type != null);
  331. }
  332. MethodInfo[] result = new MethodInfo [l.Count];
  333. l.CopyTo (result);
  334. return result;
  335. }
  336. MethodInfo[] GetMethodsInternal (BindingFlags bf, MonoGenericClass reftype)
  337. {
  338. if (reftype != this)
  339. bf |= BindingFlags.DeclaredOnly; /*To avoid duplicates*/
  340. MethodInfo[] methods = GetMethodsFromGTDWithHint (bf);
  341. if (methods.Length == 0)
  342. return new MethodInfo [0];
  343. ArrayList l = new ArrayList ();
  344. bool match;
  345. MethodAttributes mattrs;
  346. initialize ();
  347. for (int i = 0; i < methods.Length; ++i) {
  348. MethodInfo c = methods [i];
  349. match = false;
  350. mattrs = c.Attributes;
  351. if ((mattrs & MethodAttributes.MemberAccessMask) == MethodAttributes.Public) {
  352. if ((bf & BindingFlags.Public) != 0)
  353. match = true;
  354. } else {
  355. if ((bf & BindingFlags.NonPublic) != 0)
  356. match = true;
  357. }
  358. if (!match)
  359. continue;
  360. match = false;
  361. if ((mattrs & MethodAttributes.Static) != 0) {
  362. if ((bf & BindingFlags.Static) != 0)
  363. match = true;
  364. } else {
  365. if ((bf & BindingFlags.Instance) != 0)
  366. match = true;
  367. }
  368. if (!match)
  369. continue;
  370. if (c.DeclaringType.IsGenericTypeDefinition)
  371. c = TypeBuilder.GetMethod (this, c);
  372. l.Add (c);
  373. }
  374. MethodInfo[] result = new MethodInfo [l.Count];
  375. l.CopyTo (result);
  376. return result;
  377. }
  378. public override ConstructorInfo[] GetConstructors (BindingFlags bf)
  379. {
  380. if (!IsCompilerContext)
  381. throw new NotSupportedException ();
  382. ArrayList l = new ArrayList ();
  383. Type current_type = this;
  384. do {
  385. MonoGenericClass gi = current_type as MonoGenericClass;
  386. if (gi != null)
  387. l.AddRange (gi.GetConstructorsInternal (bf, this));
  388. else if (current_type is TypeBuilder)
  389. l.AddRange (current_type.GetConstructors (bf));
  390. else {
  391. MonoType mt = (MonoType) current_type;
  392. l.AddRange (mt.GetConstructors_internal (bf, this));
  393. break;
  394. }
  395. if ((bf & BindingFlags.DeclaredOnly) != 0)
  396. break;
  397. current_type = current_type.BaseType;
  398. } while (current_type != null);
  399. ConstructorInfo[] result = new ConstructorInfo [l.Count];
  400. l.CopyTo (result);
  401. return result;
  402. }
  403. ConstructorInfo[] GetConstructorsInternal (BindingFlags bf, MonoGenericClass reftype)
  404. {
  405. ConstructorInfo[] ctors = GetConstructorsFromGTDWithHint (bf);
  406. if (ctors == null || ctors.Length == 0)
  407. return new ConstructorInfo [0];
  408. ArrayList l = new ArrayList ();
  409. bool match;
  410. MethodAttributes mattrs;
  411. initialize ();
  412. for (int i = 0; i < ctors.Length; i++) {
  413. ConstructorInfo c = ctors [i];
  414. match = false;
  415. mattrs = c.Attributes;
  416. if ((mattrs & MethodAttributes.MemberAccessMask) == MethodAttributes.Public) {
  417. if ((bf & BindingFlags.Public) != 0)
  418. match = true;
  419. } else {
  420. if ((bf & BindingFlags.NonPublic) != 0)
  421. match = true;
  422. }
  423. if (!match)
  424. continue;
  425. match = false;
  426. if ((mattrs & MethodAttributes.Static) != 0) {
  427. if ((bf & BindingFlags.Static) != 0)
  428. match = true;
  429. } else {
  430. if ((bf & BindingFlags.Instance) != 0)
  431. match = true;
  432. }
  433. if (!match)
  434. continue;
  435. l.Add (TypeBuilder.GetConstructor (this, c));
  436. }
  437. ConstructorInfo[] result = new ConstructorInfo [l.Count];
  438. l.CopyTo (result);
  439. return result;
  440. }
  441. public override FieldInfo[] GetFields (BindingFlags bf)
  442. {
  443. if (!IsCompilerContext)
  444. throw new NotSupportedException ();
  445. ArrayList l = new ArrayList ();
  446. Type current_type = this;
  447. do {
  448. MonoGenericClass gi = current_type as MonoGenericClass;
  449. if (gi != null)
  450. l.AddRange (gi.GetFieldsInternal (bf, this));
  451. else if (current_type is TypeBuilder)
  452. l.AddRange (current_type.GetFields (bf));
  453. else {
  454. MonoType mt = (MonoType) current_type;
  455. l.AddRange (mt.GetFields_internal (bf, this));
  456. break;
  457. }
  458. if ((bf & BindingFlags.DeclaredOnly) != 0)
  459. break;
  460. current_type = current_type.BaseType;
  461. } while (current_type != null);
  462. FieldInfo[] result = new FieldInfo [l.Count];
  463. l.CopyTo (result);
  464. return result;
  465. }
  466. FieldInfo[] GetFieldsInternal (BindingFlags bf, MonoGenericClass reftype)
  467. {
  468. FieldInfo[] fields = GetFieldsFromGTD (bf);
  469. if (fields.Length == 0)
  470. return new FieldInfo [0];
  471. ArrayList l = new ArrayList ();
  472. bool match;
  473. FieldAttributes fattrs;
  474. initialize ();
  475. for (int i = 0; i < fields.Length; i++) {
  476. FieldInfo c = fields [i];
  477. match = false;
  478. fattrs = c.Attributes;
  479. if ((fattrs & FieldAttributes.FieldAccessMask) == FieldAttributes.Public) {
  480. if ((bf & BindingFlags.Public) != 0)
  481. match = true;
  482. } else {
  483. if ((bf & BindingFlags.NonPublic) != 0)
  484. match = true;
  485. }
  486. if (!match)
  487. continue;
  488. match = false;
  489. if ((fattrs & FieldAttributes.Static) != 0) {
  490. if ((bf & BindingFlags.Static) != 0)
  491. match = true;
  492. } else {
  493. if ((bf & BindingFlags.Instance) != 0)
  494. match = true;
  495. }
  496. if (!match)
  497. continue;
  498. l.Add (TypeBuilder.GetField (this, c));
  499. }
  500. FieldInfo[] result = new FieldInfo [l.Count];
  501. l.CopyTo (result);
  502. return result;
  503. }
  504. public override PropertyInfo[] GetProperties (BindingFlags bf)
  505. {
  506. if (!IsCompilerContext)
  507. throw new NotSupportedException ();
  508. ArrayList l = new ArrayList ();
  509. Type current_type = this;
  510. do {
  511. MonoGenericClass gi = current_type as MonoGenericClass;
  512. if (gi != null)
  513. l.AddRange (gi.GetPropertiesInternal (bf, this));
  514. else if (current_type is TypeBuilder)
  515. l.AddRange (current_type.GetProperties (bf));
  516. else {
  517. MonoType mt = (MonoType) current_type;
  518. l.AddRange (mt.GetPropertiesByName (null, bf, false, this));
  519. break;
  520. }
  521. if ((bf & BindingFlags.DeclaredOnly) != 0)
  522. break;
  523. current_type = current_type.BaseType;
  524. } while (current_type != null);
  525. PropertyInfo[] result = new PropertyInfo [l.Count];
  526. l.CopyTo (result);
  527. return result;
  528. }
  529. PropertyInfo[] GetPropertiesInternal (BindingFlags bf, MonoGenericClass reftype)
  530. {
  531. PropertyInfo[] props = GetPropertiesInternal (generic_type, bf);
  532. if (props == null || props.Length == 0)
  533. return new PropertyInfo [0];
  534. ArrayList l = new ArrayList ();
  535. bool match;
  536. MethodAttributes mattrs;
  537. MethodInfo accessor;
  538. initialize ();
  539. foreach (PropertyInfo pinfo in props) {
  540. match = false;
  541. accessor = pinfo.GetGetMethod (true);
  542. if (accessor == null)
  543. accessor = pinfo.GetSetMethod (true);
  544. if (accessor == null)
  545. continue;
  546. mattrs = accessor.Attributes;
  547. if ((mattrs & MethodAttributes.MemberAccessMask) == MethodAttributes.Public) {
  548. if ((bf & BindingFlags.Public) != 0)
  549. match = true;
  550. } else {
  551. if ((bf & BindingFlags.NonPublic) != 0)
  552. match = true;
  553. }
  554. if (!match)
  555. continue;
  556. match = false;
  557. if ((mattrs & MethodAttributes.Static) != 0) {
  558. if ((bf & BindingFlags.Static) != 0)
  559. match = true;
  560. } else {
  561. if ((bf & BindingFlags.Instance) != 0)
  562. match = true;
  563. }
  564. if (!match)
  565. continue;
  566. l.Add (new PropertyOnTypeBuilderInst (reftype, pinfo));
  567. }
  568. PropertyInfo[] result = new PropertyInfo [l.Count];
  569. l.CopyTo (result);
  570. return result;
  571. }
  572. public override EventInfo[] GetEvents (BindingFlags bf)
  573. {
  574. if (!IsCompilerContext)
  575. throw new NotSupportedException ();
  576. ArrayList l = new ArrayList ();
  577. Type current_type = this;
  578. do {
  579. MonoGenericClass gi = current_type as MonoGenericClass;
  580. if (gi != null)
  581. l.AddRange (gi.GetEventsInternal (bf, this));
  582. else if (current_type is TypeBuilder)
  583. l.AddRange (current_type.GetEvents (bf));
  584. else {
  585. MonoType mt = (MonoType) current_type;
  586. l.AddRange (mt.GetEvents (bf));
  587. break;
  588. }
  589. if ((bf & BindingFlags.DeclaredOnly) != 0)
  590. break;
  591. current_type = current_type.BaseType;
  592. } while (current_type != null);
  593. EventInfo[] result = new EventInfo [l.Count];
  594. l.CopyTo (result);
  595. return result;
  596. }
  597. EventInfo[] GetEventsInternal (BindingFlags bf, MonoGenericClass reftype) {
  598. TypeBuilder tb = generic_type as TypeBuilder;
  599. if (tb == null) {
  600. EventInfo[] res = generic_type.GetEvents (bf);
  601. for (int i = 0; i < res.Length; ++i)
  602. res [i] = new EventOnTypeBuilderInst (this, res [i]);
  603. return res;
  604. }
  605. EventBuilder[] events = tb.events;
  606. if (events == null || events.Length == 0)
  607. return new EventInfo [0];
  608. initialize ();
  609. ArrayList l = new ArrayList ();
  610. bool match;
  611. MethodAttributes mattrs;
  612. MethodInfo accessor;
  613. for (int i = 0; i < event_count; ++i) {
  614. EventBuilder ev = events [i];
  615. match = false;
  616. accessor = ev.add_method;
  617. if (accessor == null)
  618. accessor = ev.remove_method;
  619. if (accessor == null)
  620. continue;
  621. mattrs = accessor.Attributes;
  622. if ((mattrs & MethodAttributes.MemberAccessMask) == MethodAttributes.Public) {
  623. if ((bf & BindingFlags.Public) != 0)
  624. match = true;
  625. } else {
  626. if ((bf & BindingFlags.NonPublic) != 0)
  627. match = true;
  628. }
  629. if (!match)
  630. continue;
  631. match = false;
  632. if ((mattrs & MethodAttributes.Static) != 0) {
  633. if ((bf & BindingFlags.Static) != 0)
  634. match = true;
  635. } else {
  636. if ((bf & BindingFlags.Instance) != 0)
  637. match = true;
  638. }
  639. if (!match)
  640. continue;
  641. l.Add (new EventOnTypeBuilderInst (this, ev));
  642. }
  643. EventInfo[] result = new EventInfo [l.Count];
  644. l.CopyTo (result);
  645. return result;
  646. }
  647. public override Type[] GetNestedTypes (BindingFlags bf)
  648. {
  649. return generic_type.GetNestedTypes (bf);
  650. }
  651. public override bool IsAssignableFrom (Type c)
  652. {
  653. if (c == this)
  654. return true;
  655. Type[] interfaces = GetInterfacesInternal ();
  656. if (c.IsInterface) {
  657. if (interfaces == null)
  658. return false;
  659. foreach (Type t in interfaces)
  660. if (c.IsAssignableFrom (t))
  661. return true;
  662. return false;
  663. }
  664. Type parent = GetParentType ();
  665. if (parent == null)
  666. return c == typeof (object);
  667. else
  668. return c.IsAssignableFrom (parent);
  669. }
  670. public override Type UnderlyingSystemType {
  671. get { return this; }
  672. }
  673. public override Assembly Assembly {
  674. get { return generic_type.Assembly; }
  675. }
  676. public override Module Module {
  677. get { return generic_type.Module; }
  678. }
  679. public override string Name {
  680. get { return generic_type.Name; }
  681. }
  682. public override string Namespace {
  683. get { return generic_type.Namespace; }
  684. }
  685. public override string FullName {
  686. get { return format_name (true, false); }
  687. }
  688. public override string AssemblyQualifiedName {
  689. get { return format_name (true, true); }
  690. }
  691. public override Guid GUID {
  692. get { throw new NotSupportedException (); }
  693. }
  694. string format_name (bool full_name, bool assembly_qualified)
  695. {
  696. StringBuilder sb = new StringBuilder (generic_type.FullName);
  697. bool compiler_ctx = IsCompilerContext;
  698. sb.Append ("[");
  699. for (int i = 0; i < type_arguments.Length; ++i) {
  700. if (i > 0)
  701. sb.Append (",");
  702. string name;
  703. if (full_name) {
  704. string assemblyName = type_arguments [i].Assembly.FullName;
  705. name = type_arguments [i].FullName;
  706. if (name != null && assemblyName != null)
  707. name = name + ", " + assemblyName;
  708. } else {
  709. name = type_arguments [i].ToString ();
  710. }
  711. if (name == null) {
  712. if (compiler_ctx && type_arguments [i].IsGenericParameter)
  713. name = type_arguments [i].Name;
  714. else
  715. return null;
  716. }
  717. if (full_name)
  718. sb.Append ("[");
  719. sb.Append (name);
  720. if (full_name)
  721. sb.Append ("]");
  722. }
  723. sb.Append ("]");
  724. if (assembly_qualified) {
  725. sb.Append (", ");
  726. sb.Append (generic_type.Assembly.FullName);
  727. }
  728. return sb.ToString ();
  729. }
  730. public override string ToString ()
  731. {
  732. return format_name (false, false);
  733. }
  734. public override Type GetGenericTypeDefinition ()
  735. {
  736. return generic_type;
  737. }
  738. public override Type[] GetGenericArguments ()
  739. {
  740. Type[] ret = new Type [type_arguments.Length];
  741. type_arguments.CopyTo (ret, 0);
  742. return ret;
  743. }
  744. public override bool ContainsGenericParameters {
  745. get {
  746. /*FIXME remove this once compound types are not instantiated using MGC*/
  747. if (HasElementType)
  748. return GetElementType ().ContainsGenericParameters;
  749. foreach (Type t in type_arguments) {
  750. if (t.ContainsGenericParameters)
  751. return true;
  752. }
  753. return false;
  754. }
  755. }
  756. public override bool IsGenericTypeDefinition {
  757. get { return false; }
  758. }
  759. public override bool IsGenericType {
  760. get { return !HasElementType; }
  761. }
  762. public override Type DeclaringType {
  763. get { return InflateType (generic_type.DeclaringType); }
  764. }
  765. public override RuntimeTypeHandle TypeHandle {
  766. get {
  767. if (!IsCompilerContext)
  768. throw new NotSupportedException ();
  769. return _impl;
  770. }
  771. }
  772. public override Type MakeArrayType ()
  773. {
  774. return new ArrayType (this, 0);
  775. }
  776. public override Type MakeArrayType (int rank)
  777. {
  778. if (rank < 1)
  779. throw new IndexOutOfRangeException ();
  780. return new ArrayType (this, rank);
  781. }
  782. public override Type MakeByRefType ()
  783. {
  784. return new ByRefType (this);
  785. }
  786. public override Type MakePointerType ()
  787. {
  788. return new PointerType (this);
  789. }
  790. public override Type GetElementType ()
  791. {
  792. throw new NotSupportedException ();
  793. }
  794. protected override bool HasElementTypeImpl ()
  795. {
  796. return false;
  797. }
  798. protected override bool IsCOMObjectImpl ()
  799. {
  800. return false;
  801. }
  802. protected override bool IsPrimitiveImpl ()
  803. {
  804. return false;
  805. }
  806. protected override bool IsArrayImpl ()
  807. {
  808. return false;
  809. }
  810. protected override bool IsByRefImpl ()
  811. {
  812. return false;
  813. }
  814. protected override bool IsPointerImpl ()
  815. {
  816. return false;
  817. }
  818. protected override TypeAttributes GetAttributeFlagsImpl ()
  819. {
  820. return generic_type.Attributes;
  821. }
  822. //stuff that throws
  823. public override Type GetInterface (string name, bool ignoreCase)
  824. {
  825. throw new NotSupportedException ();
  826. }
  827. public override EventInfo GetEvent (string name, BindingFlags bindingAttr)
  828. {
  829. if (!IsCompilerContext)
  830. throw new NotSupportedException ();
  831. foreach (var evt in GetEvents (bindingAttr)) {
  832. if (evt.Name == name)
  833. return evt;
  834. }
  835. return null;
  836. }
  837. public override FieldInfo GetField( string name, BindingFlags bindingAttr)
  838. {
  839. throw new NotSupportedException ();
  840. }
  841. public override MemberInfo[] GetMembers (BindingFlags bindingAttr)
  842. {
  843. throw new NotSupportedException ();
  844. }
  845. public override Type GetNestedType (string name, BindingFlags bindingAttr)
  846. {
  847. throw new NotSupportedException ();
  848. }
  849. public override object InvokeMember (string name, BindingFlags invokeAttr,
  850. Binder binder, object target, object[] args,
  851. ParameterModifier[] modifiers,
  852. CultureInfo culture, string[] namedParameters)
  853. {
  854. throw new NotSupportedException ();
  855. }
  856. protected override MethodInfo GetMethodImpl (string name, BindingFlags bindingAttr, Binder binder,
  857. CallingConventions callConvention, Type[] types,
  858. ParameterModifier[] modifiers)
  859. {
  860. throw new NotSupportedException ();
  861. }
  862. protected override PropertyInfo GetPropertyImpl (string name, BindingFlags bindingAttr, Binder binder,
  863. Type returnType, Type[] types, ParameterModifier[] modifiers)
  864. {
  865. throw new NotSupportedException ();
  866. }
  867. protected override ConstructorInfo GetConstructorImpl (BindingFlags bindingAttr,
  868. Binder binder,
  869. CallingConventions callConvention,
  870. Type[] types,
  871. ParameterModifier[] modifiers)
  872. {
  873. if (!IsCompilerContext)
  874. throw new NotSupportedException ();
  875. return MonoType.GetConstructorImpl (GetConstructors (bindingAttr), bindingAttr, binder, callConvention, types, modifiers);
  876. }
  877. //MemberInfo
  878. public override bool IsDefined (Type attributeType, bool inherit)
  879. {
  880. if (!IsCompilerContext)
  881. throw new NotSupportedException ();
  882. return generic_type.IsDefined (attributeType, inherit);
  883. }
  884. public override object [] GetCustomAttributes (bool inherit)
  885. {
  886. if (!IsCompilerContext)
  887. throw new NotSupportedException ();
  888. return generic_type.GetCustomAttributes (inherit);
  889. }
  890. public override object [] GetCustomAttributes (Type attributeType, bool inherit)
  891. {
  892. if (!IsCompilerContext)
  893. throw new NotSupportedException ();
  894. return generic_type.GetCustomAttributes (attributeType, inherit);
  895. }
  896. }
  897. }