How to Create a Generic Method and Create Instance of The Type
- by DaveDev
Hi Guys
I want to create a helper method that I can imagine has a signature similar to this:
public static MyHtmlTag GenerateTag<T>(this HtmlHelper htmlHelper, object obj)
{
// how do I create an instance of MyAnchor?
// this returns MyAnchor, which has a MyHtmlTag base
}
When I invoke the method, I want to specify a type of MyHtmlTag, such as MyAnchor, e.g.:
<%= Html.GenerateTag<MyAnchor>(obj) %>
or
<%= Html.GenerateTag<MySpan>(obj) %>
Can someone show me how to create this method?
Also, what's involved in creating an instance of the type I specified? Activator.CreateInstance()?
Thanks
Dave