Comment faire pour obtenir le type d'info sans utiliser Generics?
Question
J'ai un object obj
qui est passé dans une méthode d'assistance.
public static MyTagGenerateTag<T>(this HtmlHelper htmlHelper, T obj /*, ... */)
{
Type t = typeof(T);
foreach (PropertyInfo prop in t.GetProperties())
{
object propValue = prop.GetValue(obj, null);
string stringValue = propValue.ToString();
dictionary.Add(prop.Name, stringValue);
}
// implement GenerateTag
}
On m'a dit que ce n'est pas une utilisation correcte des médicaments génériques. Quelqu'un peut-il me dire si je peux obtenir le même résultat sans spécifier un type générique? Si oui, comment?
Je serais probablement changer la signature il serait comme:
public static MyTag GenerateTag(this HtmlHelper htmlHelper, object obj /*, ... */)
{
Type t = typeof(obj);
// implement GenerateTag
}
mais Type t = typeof(obj);
est impossible.
Toutes les suggestions?
Merci
Dave
La solution
Type t = obj.GetType();
Bien que je ne pense pas qu'il y ait un problème avec ce que vous avez à l'heure actuelle.
Autres conseils
Qu'en est-il faire juste:
Type t = obj.GetType();
Cela devrait vous donner le TypeInfo.
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow