Come ottenere informazioni di tipo senza utilizzare farmaci generici?
Domanda
Ho un object obj
che viene passato in un metodo di supporto.
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
}
Mi è stato detto questo non è un corretto uso dei farmaci generici. Qualcuno può dirmi se posso ottenere lo stesso risultato senza specificare un tipo generico? Se sì, come?
Io probabilmente cambiare la firma in modo sarebbe come:
public static MyTag GenerateTag(this HtmlHelper htmlHelper, object obj /*, ... */)
{
Type t = typeof(obj);
// implement GenerateTag
}
ma Type t = typeof(obj);
è impossibile.
Qualche suggerimento?
Grazie
Dave
Soluzione
Type t = obj.GetType();
Anche se non credo che con quello che hai in questo momento non c'è alcun problema.
Altri suggerimenti
Che dire solo facendo:
Type t = obj.GetType();
Questo dovrebbe dare la TypeInfo.
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow