MonoGenericInst.cs 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  1. //
  2. // System.MonoType
  3. //
  4. // Sean MacIsaac ([email protected])
  5. // Paolo Molaro ([email protected])
  6. // Patrik Torstensson ([email protected])
  7. //
  8. // (C) 2001 Ximian, Inc.
  9. //
  10. using System.Reflection;
  11. using System.Collections;
  12. using System.Runtime.CompilerServices;
  13. using System.Globalization;
  14. using System.Runtime.Serialization;
  15. namespace System.Reflection
  16. {
  17. internal class MonoGenericInst : MonoType
  18. {
  19. private IntPtr klass;
  20. protected MonoGenericInst parent;
  21. protected Type generic_type;
  22. private MonoGenericInst[] interfaces;
  23. private MethodInfo[] methods;
  24. private ConstructorInfo[] ctors;
  25. private FieldInfo[] fields;
  26. [MonoTODO]
  27. internal MonoGenericInst ()
  28. : base (null)
  29. {
  30. // this should not be used
  31. throw new InvalidOperationException ();
  32. }
  33. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  34. private static extern MethodInfo inflate_method (MonoGenericInst declaring, MonoGenericInst reflected, MethodInfo method);
  35. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  36. private static extern ConstructorInfo inflate_ctor (MonoGenericInst declaring, MonoGenericInst reflected, ConstructorInfo ctor);
  37. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  38. private static extern FieldInfo inflate_field (MonoGenericInst declaring, MonoGenericInst reflected, FieldInfo field);
  39. private const BindingFlags flags = BindingFlags.Public | BindingFlags.NonPublic |
  40. BindingFlags.Static | BindingFlags.Instance | BindingFlags.DeclaredOnly;
  41. protected void inflate (MonoGenericInst reflected,
  42. ArrayList mlist, ArrayList clist, ArrayList flist)
  43. {
  44. if (parent != null)
  45. parent.inflate (parent, mlist, clist, flist);
  46. else if (BaseType != null) {
  47. mlist.AddRange (generic_type.BaseType.GetMethods (flags));
  48. clist.AddRange (generic_type.BaseType.GetConstructors (flags));
  49. flist.AddRange (generic_type.BaseType.GetFields (flags));
  50. } else if (interfaces != null) {
  51. foreach (MonoGenericInst iface in interfaces) {
  52. if (iface != null)
  53. iface.inflate (iface, mlist, clist, flist);
  54. }
  55. }
  56. foreach (MethodInfo m in generic_type.GetMethods (flags))
  57. mlist.Add (inflate_method (this, reflected, m));
  58. foreach (ConstructorInfo c in generic_type.GetConstructors (flags))
  59. clist.Add (inflate_ctor (this, reflected, c));
  60. foreach (FieldInfo f in generic_type.GetFields (flags))
  61. flist.Add (inflate_field (this, reflected, f));
  62. }
  63. void initialize ()
  64. {
  65. ArrayList mlist = new ArrayList ();
  66. ArrayList clist = new ArrayList ();
  67. ArrayList flist = new ArrayList ();
  68. inflate (this, mlist, clist, flist);
  69. methods = new MethodInfo [mlist.Count];
  70. mlist.CopyTo (methods, 0);
  71. ctors = new ConstructorInfo [clist.Count];
  72. clist.CopyTo (ctors, 0);
  73. fields = new FieldInfo [flist.Count];
  74. flist.CopyTo (fields, 0);
  75. }
  76. public override Type BaseType {
  77. get { return parent != null ? parent : generic_type.BaseType; }
  78. }
  79. protected override bool IsValueTypeImpl ()
  80. {
  81. if (BaseType == null)
  82. return false;
  83. if (BaseType == typeof (Enum) || BaseType == typeof (ValueType))
  84. return true;
  85. return BaseType.IsSubclassOf (typeof (ValueType));
  86. }
  87. public override MethodInfo[] GetMethods (BindingFlags bindingAttr)
  88. {
  89. if (methods == null)
  90. initialize ();
  91. return GetMethods_impl (bindingAttr);
  92. }
  93. protected MethodInfo[] GetMethods_impl (BindingFlags bindingAttr)
  94. {
  95. ArrayList l = new ArrayList ();
  96. bool match;
  97. MethodAttributes mattrs;
  98. foreach (MethodInfo c in methods) {
  99. match = false;
  100. mattrs = c.Attributes;
  101. if ((mattrs & MethodAttributes.MemberAccessMask) == MethodAttributes.Public) {
  102. if ((bindingAttr & BindingFlags.Public) != 0)
  103. match = true;
  104. } else {
  105. if ((bindingAttr & BindingFlags.NonPublic) != 0)
  106. match = true;
  107. }
  108. if (!match)
  109. continue;
  110. match = false;
  111. if ((mattrs & MethodAttributes.Static) != 0) {
  112. if ((bindingAttr & BindingFlags.Static) != 0)
  113. match = true;
  114. } else {
  115. if ((bindingAttr & BindingFlags.Instance) != 0)
  116. match = true;
  117. }
  118. if (!match)
  119. continue;
  120. l.Add (c);
  121. }
  122. MethodInfo[] result = new MethodInfo [l.Count];
  123. l.CopyTo (result);
  124. return result;
  125. }
  126. public override ConstructorInfo[] GetConstructors (BindingFlags bindingAttr)
  127. {
  128. if (ctors == null)
  129. initialize ();
  130. return GetConstructors_impl (bindingAttr);
  131. }
  132. protected ConstructorInfo[] GetConstructors_impl (BindingFlags bindingAttr)
  133. {
  134. ArrayList l = new ArrayList ();
  135. bool match;
  136. MethodAttributes mattrs;
  137. foreach (ConstructorInfo c in ctors) {
  138. match = false;
  139. mattrs = c.Attributes;
  140. if ((mattrs & MethodAttributes.MemberAccessMask) == MethodAttributes.Public) {
  141. if ((bindingAttr & BindingFlags.Public) != 0)
  142. match = true;
  143. } else {
  144. if ((bindingAttr & BindingFlags.NonPublic) != 0)
  145. match = true;
  146. }
  147. if (!match)
  148. continue;
  149. match = false;
  150. if ((mattrs & MethodAttributes.Static) != 0) {
  151. if ((bindingAttr & BindingFlags.Static) != 0)
  152. match = true;
  153. } else {
  154. if ((bindingAttr & BindingFlags.Instance) != 0)
  155. match = true;
  156. }
  157. if (!match)
  158. continue;
  159. l.Add (c);
  160. }
  161. ConstructorInfo[] result = new ConstructorInfo [l.Count];
  162. l.CopyTo (result);
  163. return result;
  164. }
  165. public override FieldInfo[] GetFields (BindingFlags bindingAttr)
  166. {
  167. if (fields == null)
  168. initialize ();
  169. return GetFields_impl (bindingAttr);
  170. }
  171. protected FieldInfo[] GetFields_impl (BindingFlags bindingAttr)
  172. {
  173. ArrayList l = new ArrayList ();
  174. bool match;
  175. FieldAttributes fattrs;
  176. foreach (FieldInfo c in fields) {
  177. match = false;
  178. fattrs = c.Attributes;
  179. if ((fattrs & FieldAttributes.FieldAccessMask) == FieldAttributes.Public) {
  180. if ((bindingAttr & BindingFlags.Public) != 0)
  181. match = true;
  182. } else {
  183. if ((bindingAttr & BindingFlags.NonPublic) != 0)
  184. match = true;
  185. }
  186. if (!match)
  187. continue;
  188. match = false;
  189. if ((fattrs & FieldAttributes.Static) != 0) {
  190. if ((bindingAttr & BindingFlags.Static) != 0)
  191. match = true;
  192. } else {
  193. if ((bindingAttr & BindingFlags.Instance) != 0)
  194. match = true;
  195. }
  196. if (!match)
  197. continue;
  198. l.Add (c);
  199. }
  200. FieldInfo[] result = new FieldInfo [l.Count];
  201. l.CopyTo (result);
  202. return result;
  203. }
  204. }
  205. internal class MonoInflatedMethod : MonoMethod
  206. {
  207. private readonly MethodInfo declaring;
  208. private readonly MonoGenericInst declaring_type;
  209. private readonly MonoGenericInst reflected_type;
  210. private readonly IntPtr ginst;
  211. public override Type DeclaringType {
  212. get {
  213. return declaring_type != null ? declaring_type : base.DeclaringType;
  214. }
  215. }
  216. public override Type ReflectedType {
  217. get {
  218. return reflected_type != null ? reflected_type : base.ReflectedType;
  219. }
  220. }
  221. public override bool IsDefined (Type attributeType, bool inherit)
  222. {
  223. // FIXME
  224. return false;
  225. }
  226. public override object[] GetCustomAttributes (bool inherit)
  227. {
  228. // FIXME
  229. return new object [0];
  230. }
  231. public override object[] GetCustomAttributes (Type attributeType, bool inherit)
  232. {
  233. // FIXME
  234. return new object [0];
  235. }
  236. }
  237. internal class MonoInflatedCtor : MonoCMethod
  238. {
  239. private readonly ConstructorInfo declaring;
  240. private readonly MonoGenericInst declaring_type;
  241. private readonly MonoGenericInst reflected_type;
  242. private readonly IntPtr ginst;
  243. public override Type DeclaringType {
  244. get {
  245. return declaring_type != null ? declaring_type : base.DeclaringType;
  246. }
  247. }
  248. public override Type ReflectedType {
  249. get {
  250. return reflected_type != null ? reflected_type : base.ReflectedType;
  251. }
  252. }
  253. public override bool IsDefined (Type attributeType, bool inherit)
  254. {
  255. // FIXME
  256. return false;
  257. }
  258. public override object[] GetCustomAttributes (bool inherit)
  259. {
  260. // FIXME
  261. return new object [0];
  262. }
  263. public override object[] GetCustomAttributes (Type attributeType, bool inherit)
  264. {
  265. // FIXME
  266. return new object [0];
  267. }
  268. }
  269. internal class MonoInflatedField : MonoField
  270. {
  271. private readonly IntPtr dhandle;
  272. private readonly MonoGenericInst declaring_type;
  273. private readonly MonoGenericInst reflected_type;
  274. public override Type DeclaringType {
  275. get {
  276. return declaring_type != null ? declaring_type : base.DeclaringType;
  277. }
  278. }
  279. public override Type ReflectedType {
  280. get {
  281. return reflected_type != null ? reflected_type : base.ReflectedType;
  282. }
  283. }
  284. }
  285. internal class MonoGenericParam : MonoType
  286. {
  287. private object refobj;
  288. private int index;
  289. private string name;
  290. private int flags;
  291. private Type[] constraints;
  292. bool initialized;
  293. [MonoTODO]
  294. internal MonoGenericParam ()
  295. : base (null)
  296. {
  297. // this should not be used
  298. throw new InvalidOperationException ();
  299. }
  300. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  301. private extern void initialize ();
  302. public void SetConstraints (Type[] constraints)
  303. {
  304. this.constraints = constraints;
  305. initialize ();
  306. }
  307. public override Type BaseType {
  308. get {
  309. if (!initialized)
  310. throw new InvalidOperationException ();
  311. if ((constraints.Length == 0) || constraints [0].IsInterface)
  312. return null;
  313. else
  314. return constraints [0];
  315. }
  316. }
  317. public override Type[] GetInterfaces ()
  318. {
  319. if (!initialized)
  320. throw new InvalidOperationException ();
  321. if ((constraints.Length == 0) || constraints [0].IsInterface)
  322. return constraints;
  323. else {
  324. Type[] ret = new Type [constraints.Length-1];
  325. Array.Copy (constraints, 1, ret, 0, constraints.Length-1);
  326. return ret;
  327. }
  328. }
  329. }
  330. }