Creating WebControls from fully qualified path (assembly name?)
I have a webpage in ASP.NET 3.5 that will be creating WebControls dynamically. The WebControls that it will be creating will be known by their fully qualified path (ie - System.Web.UI.WebControls.whatever). The reason for this is because I am allowing the user to decide what controls will go on the webpage. Of course, there's more complexity than this, but that is it in a nutshell.
Simply put - how do I create a WebControl on a webpage by it's fully qualified path?
I realize that the answer 开发者_StackOverflowwill probably end up using reflection, but I have little experience using reflection and I don't want to shoot myself in the foot by making a newbie mistake.
try to call this way: Activator.CreateInstance(Type.GetType("TypeName"));
where TypeName is fully qualified name, including assembly. in my case it looked this way:
Activator.CreateInstance(Type.GetType("System.Web.UI.WebControls.Label, System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"));
to be sure about full name in your case, try to output typeof(System.Web.UI.WebControls.Label).FullName
and use it as a pattern
object widget = Activator.CreateInstance ( Assembly.GetType ( name ) );
where name is the string of the fully qualified type
精彩评论