当前位置: 首页 > 图文教程 > 网络编程 > ASP.NET > 用.NET动态创建类的实例讲解

ASP.NET
asp.net GridView控件中模板列CheckBox全选、反选、取消
asp.net GridView 删除时弹出确认对话框(包括内容提示)
asp.net DropDownList 三级联动下拉菜单实现代码
asp DataTable添加列和行的三种方法
Asp.net 页面调用javascript变量的值
asp.net 长文章通过设定的行数分页
asp.net 定时间点执行任务的简易解决办法
asp.net 页面延时五秒,跳转到另外的页面
asp.net 动态输出透明gif图片
asp.net DataList与Repeater用法区别
asp.net Javascript获取CheckBoxList的value
asp.net程序在调式和发布之间图片路径问题的解决方法
asp.net下生成英文字符数字验证码的代码
asp.net 页面版文本框智能提示JSCode (升级版)
ASP.NET URL伪静态重写实现方法
ASP.NET 2.0 中Forms安全认证
asp.net 动态添加多个用户控件
asp.net Repeater显示父子表数据,无闪烁
asp.net 无法获取的内部内容,因为该内容不是文本 的解决方法
asp.net GridView排序简单实现

ASP.NET 中的 用.NET动态创建类的实例讲解


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-08-14   浏览: 52 ::
收藏到网摘: n/a

看了网上很多关于DotNet动态创建类的实例的文章,我这里想总结一下,其实方法很简单,就是用“Activator.CreateInstance”。但是这个方法需要待创建的类的Type作为参数,为了获得该参数,可以利用[Assembly].GetType方法,这个方法只需要待创建的类的名称(名称字符串)就可以了,最后的问题就是要获得这个类所在的程序集。如何获得待创建的类所在程序集,那么就解决了这个问题。

利用Microsoft.VisualBasic.VBCodeProvider(),如果是C#可以用CSharpCodeProvider(),将类文件编译成为DLL文件,然后利用[Assembly].LoadFrom("DLL 的绝对路径")加载该DLL。这样我们可以避免在那些创建DLL和Type的复杂代码。我告诉我的项目组成员这个例子后,强调要打开思路,Simple is perfect,凡事都尽量找简便的方法来实现,客户永远不会为我们那些复杂的代码多花一分钱。

1.执行编译任务的方法:

以下为引用的内容:  
 PublicSharedFunctionCompileExecutable()FunctionCompileExecutable(ByValsourceNameAsString,ByValDLLPathAsString,ByRefReturnDLLNameAsString)AsBoolean
  DimsourceFileAsFileInfo=NewFileInfo(sourceName)
  DimproviderAsCodeDomProvider=Nothing
  DimcompileOkAsBoolean=False
  '根据原文件的扩展名选择codeprovider
  IfsourceFile.Extension.ToUpper(CultureInfo.InvariantCulture)=".CS"Then
  provider=NewMicrosoft.CSharp.CSharpCodeProvider()
  ElseIfsourceFile.Extension.ToUpper(CultureInfo.InvariantCulture)=".VB"Then
  provider=NewMicrosoft.VisualBasic.VBCodeProvider()
  Else
  Console.WriteLine("原文件必须包含.cs或.vb扩展名")
  EndIf
  IfNotproviderIsNothingThen
  '构造DLL文件的全路径
  DimdllNameAsString=String.Format("{0}{1}.dll",_
  DLLPath,_
  sourceFile.Name.Replace(".","_"))
  ReturnDLLName=dllName
  DimcpAsCompilerParameters=NewCompilerParameters()
  '设置编译控制参数
  cp.GenerateExecutable=False'生成DLL,如果是True则生成exe文件
  cp.OutputAssembly=dllName
  cp.GenerateInMemory=False
  cp.TreatWarningsAsErrors=False
  '调用编译方法将原代码文件编译成DLL
  DimcrAsCompilerResults=provider.CompileAssemblyFromFile(cp,_
  sourceName)
  Ifcr.Errors.Count>0Then
  '显示编译错误
  Console.WriteLine("编译错误{0}编译成{1}",_
  sourceName,cr.PathToAssembly)
  DimceAsCompilerError
  ForEachceIncr.Errors
  Console.WriteLine("{0}",ce.ToString())
  Console.WriteLine()
  Nextce
  Else
  '显示编译成功的消息
  Console.WriteLine("原文件{0}编译成{1}成功完成.",_
  sourceName,cr.PathToAssembly)
  EndIf
  '返回编译结果
  Ifcr.Errors.Count>0Then
  compileOk=False
  Else
  compileOk=True
  EndIf
  EndIf
  ReturncompileOk
  EndFunction

2.编译DLL,并动态创建类的实例。(这里类的原文件是Class1.vb文件,放在WebSite的App_Code文件夹中了,实际使用时可以放在任意物理位置。)

以下为引用的内容:  
 DimstrSourceFileNameAsString=Server.MapPath("~/App_Code/Class1.vb")'类文件的全路径
  DimstrDllPathAsString=Server.MapPath("~/App_Code")'编译后的DLL文件存放的位置
  DimstrDllNameAsString=""'DLL的全路径(返回值)
  CompileExecutable(strSourceFileName,strDllPath,strDllName)'编译原文件为DLL文件
  DimaAs[Assembly]=[Assembly].LoadFrom(strDllName)'加载DLL
  DimmyTypeAsSystem.Type=a.GetType("Class1")'获得Class1的Type
  DimobjAsObject=Activator.CreateInstance(myType)'获得Class1的实例

3.Class1.vb原文件

以下为引用的内容:

       PublicClassClass1ClassClass1
  PubliciAsInteger
  EndClass