views:

73

answers:

2

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

+9  A: 

Type t = obj.GetType();

Although I don't think there's any problem with what you have at the moment.

Lee
+1  A: 

What about just doing:

Type t = obj.GetType();

That should give you the TypeInfo.

Kris