2
0

MonoGenericClass.cs 28 KB

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