How to Create a Generic Method and Create Instance of The Type
Posted
by DaveDev
on Stack Overflow
See other posts from Stack Overflow
or by DaveDev
Published on 2010-03-15T23:11:13Z
Indexed on
2010/03/15
23:19 UTC
Read the original article
Hit count: 335
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
© Stack Overflow or respective owner