从完全限定路径(程序集名称?)创建 WebControl
我有一个 ASP.NET 3.5 网页,它将动态创建 WebControls。它将创建的 WebControls 将通过其完全限定路径(即 - System.Web.UI.WebControls.whatever)得知。这样做的原因是因为我允许用户决定网页上将显示哪些控件。当然,还有比这更复杂的事情,但简而言之,就是这样。
简单地说 - 如何通过完全限定的路径在网页上创建 WebControl?
我意识到答案可能最终会使用反射,但我没有使用反射的经验,我不希望犯新手错误,搬起石头砸自己的脚。
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 will 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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试以这种方式调用:
Activator.CreateInstance(Type.GetType("TypeName"));
其中 TypeName 是完全限定名称,包括程序集。就我而言,它看起来是这样的:
要确定您的情况下的全名,请尝试输出
typeof(System.Web.UI.WebControls.Label).FullName
并将其用作模式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:
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其中 name 是完全限定类型的字符串
where name is the string of the fully qualified type