MonoGenericClass.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519
  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. #if !FULL_AOT_RUNTIME
  33. using System.Reflection;
  34. using System.Reflection.Emit;
  35. using System.Collections;
  36. using System.Runtime.CompilerServices;
  37. using System.Globalization;
  38. using System.Runtime.Serialization;
  39. using System.Text;
  40. using System.Runtime.InteropServices;
  41. namespace System.Reflection
  42. {
  43. /*
  44. * MonoGenericClass represents an instantiation of a generic TypeBuilder. MS
  45. * calls this class TypeBuilderInstantiation (a much better name). MS returns
  46. * NotImplementedException for many of the methods but we can't do that as gmcs
  47. * depends on them.
  48. */
  49. [StructLayout (LayoutKind.Sequential)]
  50. sealed class MonoGenericClass :
  51. TypeInfo
  52. {
  53. #region Keep in sync with object-internals.h
  54. #pragma warning disable 649
  55. internal Type generic_type;
  56. Type[] type_arguments;
  57. bool initialized;
  58. #pragma warning restore 649
  59. #endregion
  60. Hashtable fields, ctors, methods;
  61. internal MonoGenericClass ()
  62. {
  63. // this should not be used
  64. throw new InvalidOperationException ();
  65. }
  66. internal MonoGenericClass (Type tb, Type[] args)
  67. {
  68. this.generic_type = tb;
  69. this.type_arguments = args;
  70. /*
  71. This is a temporary hack until we can fix the rest of the runtime
  72. to properly handle this class to be a complete UT.
  73. We must not regisrer this with the runtime after the type is created
  74. otherwise created_type.MakeGenericType will return an instance of MonoGenericClass,
  75. which is very very broken.
  76. */
  77. if (tb is TypeBuilder && !(tb as TypeBuilder).is_created)
  78. register_with_runtime (this);
  79. }
  80. internal override Type InternalResolve ()
  81. {
  82. Type gtd = generic_type.InternalResolve ();
  83. Type[] args = new Type [type_arguments.Length];
  84. for (int i = 0; i < type_arguments.Length; ++i)
  85. args [i] = type_arguments [i].InternalResolve ();
  86. return gtd.MakeGenericType (args);
  87. }
  88. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  89. extern void initialize (FieldInfo[] fields);
  90. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  91. internal static extern void register_with_runtime (Type type);
  92. internal bool IsCreated {
  93. get {
  94. TypeBuilder tb = generic_type as TypeBuilder;
  95. return tb != null ? tb.is_created : true;
  96. }
  97. }
  98. private const BindingFlags flags = BindingFlags.Public | BindingFlags.NonPublic |
  99. BindingFlags.Static | BindingFlags.Instance | BindingFlags.DeclaredOnly;
  100. void initialize ()
  101. {
  102. if (initialized)
  103. return;
  104. MonoGenericClass parent = GetParentType () as MonoGenericClass;
  105. if (parent != null)
  106. parent.initialize ();
  107. initialize (generic_type.GetFields (flags));
  108. initialized = true;
  109. }
  110. Type GetParentType ()
  111. {
  112. return InflateType (generic_type.BaseType);
  113. }
  114. internal Type InflateType (Type type)
  115. {
  116. return InflateType (type, type_arguments, null);
  117. }
  118. internal Type InflateType (Type type, Type[] method_args)
  119. {
  120. return InflateType (type, type_arguments, method_args);
  121. }
  122. internal static Type InflateType (Type type, Type[] type_args, Type[] method_args)
  123. {
  124. if (type == null)
  125. return null;
  126. if (!type.IsGenericParameter && !type.ContainsGenericParameters)
  127. return type;
  128. if (type.IsGenericParameter) {
  129. if (type.DeclaringMethod == null)
  130. return type_args == null ? type : type_args [type.GenericParameterPosition];
  131. return method_args == null ? type : method_args [type.GenericParameterPosition];
  132. }
  133. if (type.IsPointer)
  134. return InflateType (type.GetElementType (), type_args, method_args).MakePointerType ();
  135. if (type.IsByRef)
  136. return InflateType (type.GetElementType (), type_args, method_args).MakeByRefType ();
  137. if (type.IsArray) {
  138. if (type.GetArrayRank () > 1)
  139. return InflateType (type.GetElementType (), type_args, method_args).MakeArrayType (type.GetArrayRank ());
  140. if (type.ToString ().EndsWith ("[*]", StringComparison.Ordinal)) /*FIXME, the reflection API doesn't offer a way around this*/
  141. return InflateType (type.GetElementType (), type_args, method_args).MakeArrayType (1);
  142. return InflateType (type.GetElementType (), type_args, method_args).MakeArrayType ();
  143. }
  144. Type[] args = type.GetGenericArguments ();
  145. for (int i = 0; i < args.Length; ++i)
  146. args [i] = InflateType (args [i], type_args, method_args);
  147. Type gtd = type.IsGenericTypeDefinition ? type : type.GetGenericTypeDefinition ();
  148. return gtd.MakeGenericType (args);
  149. }
  150. public override Type BaseType {
  151. get { return generic_type.BaseType; }
  152. }
  153. public override Type[] GetInterfaces ()
  154. {
  155. throw new NotSupportedException ();
  156. }
  157. protected override bool IsValueTypeImpl ()
  158. {
  159. return generic_type.IsValueType;
  160. }
  161. internal override MethodInfo GetMethod (MethodInfo fromNoninstanciated)
  162. {
  163. initialize ();
  164. if (methods == null)
  165. methods = new Hashtable ();
  166. if (!methods.ContainsKey (fromNoninstanciated))
  167. methods [fromNoninstanciated] = new MethodOnTypeBuilderInst (this, fromNoninstanciated);
  168. return (MethodInfo)methods [fromNoninstanciated];
  169. }
  170. internal override ConstructorInfo GetConstructor (ConstructorInfo fromNoninstanciated)
  171. {
  172. initialize ();
  173. if (ctors == null)
  174. ctors = new Hashtable ();
  175. if (!ctors.ContainsKey (fromNoninstanciated))
  176. ctors [fromNoninstanciated] = new ConstructorOnTypeBuilderInst (this, fromNoninstanciated);
  177. return (ConstructorInfo)ctors [fromNoninstanciated];
  178. }
  179. internal override FieldInfo GetField (FieldInfo fromNoninstanciated)
  180. {
  181. initialize ();
  182. if (fields == null)
  183. fields = new Hashtable ();
  184. if (!fields.ContainsKey (fromNoninstanciated))
  185. fields [fromNoninstanciated] = new FieldOnTypeBuilderInst (this, fromNoninstanciated);
  186. return (FieldInfo)fields [fromNoninstanciated];
  187. }
  188. public override MethodInfo[] GetMethods (BindingFlags bf)
  189. {
  190. throw new NotSupportedException ();
  191. }
  192. public override ConstructorInfo[] GetConstructors (BindingFlags bf)
  193. {
  194. throw new NotSupportedException ();
  195. }
  196. public override FieldInfo[] GetFields (BindingFlags bf)
  197. {
  198. throw new NotSupportedException ();
  199. }
  200. public override PropertyInfo[] GetProperties (BindingFlags bf)
  201. {
  202. throw new NotSupportedException ();
  203. }
  204. public override EventInfo[] GetEvents (BindingFlags bf)
  205. {
  206. throw new NotSupportedException ();
  207. }
  208. public override Type[] GetNestedTypes (BindingFlags bf)
  209. {
  210. throw new NotSupportedException ();
  211. }
  212. public override bool IsAssignableFrom (Type c)
  213. {
  214. throw new NotSupportedException ();
  215. }
  216. public override Type UnderlyingSystemType {
  217. get { return this; }
  218. }
  219. public override Assembly Assembly {
  220. get { return generic_type.Assembly; }
  221. }
  222. public override Module Module {
  223. get { return generic_type.Module; }
  224. }
  225. public override string Name {
  226. get { return generic_type.Name; }
  227. }
  228. public override string Namespace {
  229. get { return generic_type.Namespace; }
  230. }
  231. public override string FullName {
  232. get { return format_name (true, false); }
  233. }
  234. public override string AssemblyQualifiedName {
  235. get { return format_name (true, true); }
  236. }
  237. public override Guid GUID {
  238. get { throw new NotSupportedException (); }
  239. }
  240. string format_name (bool full_name, bool assembly_qualified)
  241. {
  242. StringBuilder sb = new StringBuilder (generic_type.FullName);
  243. sb.Append ("[");
  244. for (int i = 0; i < type_arguments.Length; ++i) {
  245. if (i > 0)
  246. sb.Append (",");
  247. string name;
  248. if (full_name) {
  249. string assemblyName = type_arguments [i].Assembly.FullName;
  250. name = type_arguments [i].FullName;
  251. if (name != null && assemblyName != null)
  252. name = name + ", " + assemblyName;
  253. } else {
  254. name = type_arguments [i].ToString ();
  255. }
  256. if (name == null) {
  257. return null;
  258. }
  259. if (full_name)
  260. sb.Append ("[");
  261. sb.Append (name);
  262. if (full_name)
  263. sb.Append ("]");
  264. }
  265. sb.Append ("]");
  266. if (assembly_qualified) {
  267. sb.Append (", ");
  268. sb.Append (generic_type.Assembly.FullName);
  269. }
  270. return sb.ToString ();
  271. }
  272. public override string ToString ()
  273. {
  274. return format_name (false, false);
  275. }
  276. public override Type GetGenericTypeDefinition ()
  277. {
  278. return generic_type;
  279. }
  280. public override Type[] GetGenericArguments ()
  281. {
  282. Type[] ret = new Type [type_arguments.Length];
  283. type_arguments.CopyTo (ret, 0);
  284. return ret;
  285. }
  286. public override bool ContainsGenericParameters {
  287. get {
  288. foreach (Type t in type_arguments) {
  289. if (t.ContainsGenericParameters)
  290. return true;
  291. }
  292. return false;
  293. }
  294. }
  295. public override bool IsGenericTypeDefinition {
  296. get { return false; }
  297. }
  298. public override bool IsGenericType {
  299. get { return true; }
  300. }
  301. public override Type DeclaringType {
  302. get { return generic_type.DeclaringType; }
  303. }
  304. public override RuntimeTypeHandle TypeHandle {
  305. get {
  306. throw new NotSupportedException ();
  307. }
  308. }
  309. public override Type MakeArrayType ()
  310. {
  311. return new ArrayType (this, 0);
  312. }
  313. public override Type MakeArrayType (int rank)
  314. {
  315. if (rank < 1)
  316. throw new IndexOutOfRangeException ();
  317. return new ArrayType (this, rank);
  318. }
  319. public override Type MakeByRefType ()
  320. {
  321. return new ByRefType (this);
  322. }
  323. public override Type MakePointerType ()
  324. {
  325. return new PointerType (this);
  326. }
  327. public override Type GetElementType ()
  328. {
  329. throw new NotSupportedException ();
  330. }
  331. protected override bool HasElementTypeImpl ()
  332. {
  333. return false;
  334. }
  335. protected override bool IsCOMObjectImpl ()
  336. {
  337. return false;
  338. }
  339. protected override bool IsPrimitiveImpl ()
  340. {
  341. return false;
  342. }
  343. protected override bool IsArrayImpl ()
  344. {
  345. return false;
  346. }
  347. protected override bool IsByRefImpl ()
  348. {
  349. return false;
  350. }
  351. protected override bool IsPointerImpl ()
  352. {
  353. return false;
  354. }
  355. protected override TypeAttributes GetAttributeFlagsImpl ()
  356. {
  357. return generic_type.Attributes;
  358. }
  359. //stuff that throws
  360. public override Type GetInterface (string name, bool ignoreCase)
  361. {
  362. throw new NotSupportedException ();
  363. }
  364. public override EventInfo GetEvent (string name, BindingFlags bindingAttr)
  365. {
  366. throw new NotSupportedException ();
  367. }
  368. public override FieldInfo GetField( string name, BindingFlags bindingAttr)
  369. {
  370. throw new NotSupportedException ();
  371. }
  372. public override MemberInfo[] GetMembers (BindingFlags bindingAttr)
  373. {
  374. throw new NotSupportedException ();
  375. }
  376. public override Type GetNestedType (string name, BindingFlags bindingAttr)
  377. {
  378. throw new NotSupportedException ();
  379. }
  380. public override object InvokeMember (string name, BindingFlags invokeAttr,
  381. Binder binder, object target, object[] args,
  382. ParameterModifier[] modifiers,
  383. CultureInfo culture, string[] namedParameters)
  384. {
  385. throw new NotSupportedException ();
  386. }
  387. protected override MethodInfo GetMethodImpl (string name, BindingFlags bindingAttr, Binder binder,
  388. CallingConventions callConvention, Type[] types,
  389. ParameterModifier[] modifiers)
  390. {
  391. throw new NotSupportedException ();
  392. }
  393. protected override PropertyInfo GetPropertyImpl (string name, BindingFlags bindingAttr, Binder binder,
  394. Type returnType, Type[] types, ParameterModifier[] modifiers)
  395. {
  396. throw new NotSupportedException ();
  397. }
  398. protected override ConstructorInfo GetConstructorImpl (BindingFlags bindingAttr,
  399. Binder binder,
  400. CallingConventions callConvention,
  401. Type[] types,
  402. ParameterModifier[] modifiers)
  403. {
  404. throw new NotSupportedException ();
  405. }
  406. //MemberInfo
  407. public override bool IsDefined (Type attributeType, bool inherit)
  408. {
  409. throw new NotSupportedException ();
  410. }
  411. public override object [] GetCustomAttributes (bool inherit)
  412. {
  413. if (IsCreated)
  414. return generic_type.GetCustomAttributes (inherit);
  415. throw new NotSupportedException ();
  416. }
  417. public override object [] GetCustomAttributes (Type attributeType, bool inherit)
  418. {
  419. if (IsCreated)
  420. return generic_type.GetCustomAttributes (attributeType, inherit);
  421. throw new NotSupportedException ();
  422. }
  423. internal override bool IsUserType {
  424. get {
  425. foreach (var t in type_arguments) {
  426. if (t.IsUserType)
  427. return true;
  428. }
  429. return false;
  430. }
  431. }
  432. }
  433. }
  434. #endif