avril
2007
Dans le code suivant :
Type t = Type.GetType("System.Collections.Generic.List`1[" + test.GetType().ToString() + "]");
t est égal au type d’une liste de mon type anonyme.
Dans le code suivant :
Type t = Type.GetType("System.Collections.Generic.List`1[" + typeof(string).ToString() + "]");
t est égal à typeof(List<string>)
Et dans le code suivant :
Type t = Type.GetType("System.Collections.Generic.List`1[" + typeof(Control).ToString() + "]");
t est égal à null !
Comme la classe Control n’est pas dans mscorlib, il faut renseigner l’assembly :
Type.getType(typeof(Control))
retourne null alors que Type.GetType(typeof(Control).ToString() + ", " + typeof(Control).Assembly.ToString());
retourne typeof(Control)
Cependant, j’ai également essayé ceci :
Type t = Type.GetType("System.Collections.Generic.List`1[" + typeof(Control).ToString() + ", " + typeof(Control).Assembly.ToString() + "]");
mais t est toujours égal à null !
En fait, il me manquait un niveau de crochet :
Type t = Type.GetType("System.Collections.Generic.List`1[[" + typeof(Control).ToString() + ", " + typeof(Control).Assembly.ToString() + "]]");
A noter que bien entendu,
Type t = Type.GetType("System.Collections.Generic.List`1[[" + typeof(String).ToString() + ", " + typeof(String).Assembly.ToString() + "]]");
est toujours valide.
Merci à Niels VAN VLIET pour l’info.