Dynamic build of generics types at runtime

How to create dynamically a type in .Net when it’s only known a string representation of that type, I think it’s an operation known to almost all .Net programmers, but what happened with types with type parameters (or generics) ?

In .Net is possible to create dynamically both generics with a known type, i.e. List<>, and generics with type in turn dynamically built.

Here’s an example:

a) Dynamic generated both type parameter and type

Type collectionType = Type.GetType("System.Collections.Generic.List`1, mscorlib");
if (collectionType == "null")
    throw new InvalidOperationException("Invalid type");
Type genericType = Type.GetType("MyNamespace.MyType, MyAssembly");
Type listWithGeneric = collectionType.MakeGenericType(genericType);
var myList = Activator.CreateInstance(listWithGeneric) as IList;

b) Dynamic generated type parameter with a known type (System.Collection.Generic.List in this case):

Type genericType = Type.GetType("MyNamespace.MyType, MyAssembly");
Type listWithGeneric = typeof(List<>).MakeGenericType(genericType);
var myList = Activator.CreateInstance(listWithGeneric) as IList;
Technorati Tags: ,,

230 thoughts on “Dynamic build of generics types at runtime

  1. Pingback: drugstore online
  2. Pingback: pharmacy canada
  3. Pingback: buy cialis online
  4. Pingback: mazhor4sezon
  5. Pingback: filmfilmfilmes
  6. Pingback: gRh9UPV
  7. Pingback: deiun.flazio.com
  8. Pingback: kertyun.flazio.com
  9. Pingback: canada rx
  10. Pingback: gewrt.usluga.me
  11. Pingback: 9-05-2022
  12. Pingback: kinoteatrzarya.ru
  13. Pingback: TopGun2022
  14. Pingback: Xvideos
  15. Pingback: XVIDEOSCOM Videos
  16. Pingback: hekluy.ucraft.site
  17. Pingback: ivanesva
  18. Pingback: madridbet
  19. Pingback: meritroyalbet
  20. Pingback: meritroyalbet
  21. Pingback: eurocasino
  22. Pingback: canadian drugstore
  23. Pingback: Netflix
  24. Pingback: meritroyalbet
  25. Pingback: FILM
  26. Pingback: designchita.ru
  27. Pingback: YA-krasneyu
  28. Pingback: design-human.ru
  29. Pingback: designmsu.ru
  30. Pingback: vkl-design.ru
  31. Pingback: irida-design.ru
  32. Pingback: projectio
  33. Pingback: psy online
  34. Pingback: uels ukrain
  35. Pingback: do-posle-psihologa
  36. Pingback: DPTPtNqS
  37. Pingback: qQ8KZZE6
  38. Pingback: D6tuzANh
  39. Pingback: SHKALA TONOV
  40. Pingback: chelovek-iz-90-h
  41. Pingback: 3Hk12Bl
  42. Pingback: 3NOZC44
  43. Pingback: 01211
  44. Pingback: tor-lyubov-i-grom
  45. Pingback: film-tor-2022
  46. Pingback: hd-tor-2022
  47. Pingback: hdorg2.ru
  48. Pingback: Psikholog
  49. Pingback: netstate.ru
  50. Pingback: 3construe
  51. Pingback: Link
  52. Pingback: psy
  53. Pingback: bit.ly
  54. Pingback: meritking
  55. Pingback: cleantalkorg2.ru
  56. Pingback: bucha killings
  57. Pingback: War in Ukraine
  58. Pingback: Ukraine
  59. Pingback: site
  60. Pingback: stats
  61. Pingback: Ukraine-war
  62. Pingback: movies
  63. Pingback: gidonline
  64. Pingback: web
  65. Pingback: film.8filmov.ru
  66. Pingback: video
  67. Pingback: filmgoda.ru
  68. Pingback: rodnoe-kino-ru
  69. Pingback: confeitofilm
  70. Pingback: stat.netstate.ru
  71. Pingback: sY5am
  72. Pingback: Dom drakona
  73. Pingback: JGXldbkj
  74. Pingback: aOuSjapt
  75. Pingback: ìûøëåíèå
  76. Pingback: psikholog moskva
  77. Pingback: Dim Drakona 2022
  78. Pingback: TwnE4zl6
  79. Pingback: psy 3CtwvjS
  80. Pingback: lalochesia
  81. Pingback: buy viagra germany
  82. Pingback: canada medication
  83. Pingback: canadadrugs
  84. Pingback: film onlinee
  85. Pingback: canadianpharmacy
  86. Pingback: canadian cialis
  87. Pingback: canadadrugs
  88. Pingback: ivermectin tablets
  89. Pingback: generic stromectol
  90. Pingback: stromectol lice
  91. Pingback: 3qAIwwN
  92. Pingback: stromectol pills
  93. Pingback: video-2
  94. Pingback: sezons.store
  95. Pingback: stromectol generic
  96. Pingback: ivermectina dosis
  97. Pingback: ivermectina dosis
  98. Pingback: psy-news.ru
  99. Pingback: ivermectina
  100. Pingback: 000-1
  101. Pingback: 3SoTS32
  102. Pingback: 3DGofO7
  103. Pingback: canada drugs

Comments are closed.