动网论坛,站长建站首选,国内使用量最多的论坛软件 动网论坛官方技术讨论区 站长工具 申请属于您自己的免费论坛
首页 | 新闻资讯 | 网站运营 | 网络编程 | 数据库 | 服务器 | 网页设计 | 图像媒体 | 网络应用 | 搜索优化 | 资源下载 | 动网主机 | DVBOX
    本站内  互联网 ASP论坛  ASP.Net论坛  PHP论坛
   .Net → 阅读文章

 使用反射实现根据名称动态创建窗体的几种方法

作者:郁郁小蝎 来源:中国站长学院 
阅读 数 460 人次 , 2006-2-15 14:30:00 

 方法1使用activator方法创建实例

Dim str As String

        str = "Form2"  '必须是 命名空间++窗体类名(这里假设为命名空间为空)

        Dim tempAssembly As [Assembly] = [Assembly].GetExecutingAssembly()

        Dim t As Type = tempAssembly.GetType(str)

        Dim args() As Object = Nothing

        Dim o As Object = System.Activator.CreateInstance(t, args)

        CType(o, Form2).Show()

        'Dim frm2 As Form = CType(tempAssembly.CreateInstance(str), Form)

        'frm2.Show()

////////////////方法2使用构造函数的invoke方法创建实例。

        Dim ty() As Type = {} 该构造函数没有参数

        Dim c As ConstructorInfo = t.GetConstructor(ty) 获得没有参数的构造函数

        Dim args1() As Object = Nothing ‘参数为空

        Dim p As Object = c.Invoke(Nothing) ‘创建实例时参数为空

        CType(p, Form2).Show()

方法3 ///////////////////////////////////////使用assembly.createinstance方法创建实例

      Dim str As String

        str = "Form2"  '必须是 命名空间++窗体类名

        Dim tempAssembly As System.Reflection.Assembly = System.Reflection.Assembly.GetExecutingAssembly()

 

        Dim frm2 As Form = CType(tempAssembly.CreateInstance(str), Form)

        frm2.Show()

  
 
 收藏本文  打印本文  论坛讨论  关闭窗口
· 上一篇:正弦函数的绘制的一种方法
· 下一篇:运行时拉伸和移动控件的类
· 从ASP迁移至ASP+ --急不可耐了?转换其他的页面吧
· 如何在asp+ 中使用自定义的pagelet
· 10天学会ASP.net之第七天
· CNET JSP BASIC:建立一个JSP网站
· asp.net高级教程(一)---asp.net , 还是asp+ ?


关于本站 | 联系我们 | 业务合作 | 客户案例 | 诚聘英才 | 广告合作 | 收藏本站
海口动网先锋网络科技有限公司版权所有
Copyright © 2000 - 2006 Cndw.Com
中华人民共和国电信与信息服务业务经营许可证编号 琼 ICP 020077