How To Get Type Info Without Using Generics?

Posted by DaveDev on Stack Overflow See other posts from Stack Overflow or by DaveDev
Published on 2010-03-15T21:13:03Z Indexed on 2010/03/15 21:19 UTC
Read the original article Hit count: 280

Filed under:
|
|

Hi Guys

I have an object obj that is passed into a helper method.

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
}

I've been told this is not a correct use of generics. Can somebody tell me if I can achieve the same result without specifying a generic type? If so, how?

I would probably change the signature so it'd be like:

public static MyTag GenerateTag(this HtmlHelper htmlHelper, object obj /*, ... */)
{
    Type t = typeof(obj);
    // implement GenerateTag
}

but Type t = typeof(obj); is impossible.

Any suggestions?

Thanks

Dave

© Stack Overflow or respective owner

Related posts about c#

Related posts about typeinfo