All articles| All Pictures| All Softwares| All Video| Go home page| Write articles| Upload pictures

Reading number is top 10 articles
单击GridView某行获取该行某列内容并显示_[Asp.Net教程]
asp.net缩略图和水印制作-Image,Generation_[Asp.Net教程]
通过web.config设置数据库连接串_[Asp.Net教程]
在PHP的图形函数中显示汉字_php资料_编程技术
Sql,server动态和静态内存分配_[SQL,Server教程]
javascript实现小方框中浏览大图特效_JavaScript技术_编程技术
DataBinder.Eval使用方法总结_[Asp.Net教程]
Sql Server 和 Access 操作数据库结构Sql语句_[SQL Server教程]
visual c++中CColorDialog对话框的制作
ASP.NET中根据XML动态创建使用WEB组件_[Asp.Net教程]
Reading number is top 10 pictures
Wild animals melee moment of life and death2
男人,就要活出棱角
擦地板的大叔太好了
邪恶搞笑内涵图
The household design, do not do bridal chamber a pity
NeedWallpaper7
Beauty Sun Feifei
可爱的小动物
NeedWallpaper1
The sixties of the last century, China is such a kill pig
Download software ranking
Ashlynn Video4
Ashlynn Video3
The cock of the Grosvenor LTD handsome
传奇私服架设教程-chm
I'm come from Beijing1
虚拟机汉化软件
Be there or be square
SQL2000 For 4IN1
Macromedia Dreamweaver 8
电车之狼R
aaa published in(发表于) 2013/12/17 7:47:43 Edit(编辑)
应用技巧:用.net动态创建类的实例_.net资料_编程技术

应用技巧:用.net动态创建类的实例_.net资料_编程技术

应用技巧:用.net动态创建类的实例_.net资料_编程技术-你的首页-uuhomepage.com

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


  大家可以参考http://www.cnblogs.com/ShadowK/archive/2006/11/14/560131.html,费了很多笔墨写了一个比较完整的动态构造类的设计器。其实,在获得程序集这个问题上,可以有更简单的办法,以下是我的做法。


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


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


  Public Shared Function CompileExecutable()Function CompileExecutable(ByVal sourceName As String, ByVal DLLPath As String, ByRef ReturnDLLName As String) As Boolean
  Dim sourceFile As FileInfo = New FileInfo(sourceName)
  Dim provider As CodeDomProvider = Nothing
  Dim compileOk As Boolean = False
  ' 根据原文件的扩展名选择code provider
  If sourceFile.Extension.ToUpper(CultureInfo.InvariantCulture) = ".CS" Then
  provider = New Microsoft.CSharp.CSharpCodeProvider()
  ElseIf sourceFile.Extension.ToUpper(CultureInfo.InvariantCulture) = ".VB" Then
  provider = New Microsoft.VisualBasic.VBCodeProvider()
  Else
  Console.WriteLine("原文件必须包含 .cs 或 .vb 扩展名")
  End If
  If Not provider Is Nothing Then
  ' 构造DLL文件的全路径
  Dim dllName As String = String.Format("{0}\{1}.dll", _
  DLLPath, _
  sourceFile.Name.Replace(".", "_"))
  ReturnDLLName = dllName
  Dim cp As CompilerParameters = New CompilerParameters()
  ' 设置编译控制参数
  cp.GenerateExecutable = False '生成DLL,如果是True则生成exe文件
  cp.OutputAssembly = dllName
  cp.GenerateInMemory = False
  cp.TreatWarningsAsErrors = False
  ' 调用编译方法将原代码文件编译成DLL
  Dim cr As CompilerResults = provider.CompileAssemblyFromFile(cp, _
  sourceName)
  If cr.Errors.Count > 0 Then
  ' 显示编译错误
  Console.WriteLine("编译错误 {0} 编译成 {1}", _
  sourceName, cr.PathToAssembly)
  Dim ce As CompilerError
  For Each ce In cr.Errors
  Console.WriteLine(" {0}", ce.ToString())
  Console.WriteLine()
  Next ce
  Else
  ' 显示编译成功的消息
  Console.WriteLine("原文件 {0} 编译成 {1} 成功完成.", _
  sourceName, cr.PathToAssembly)
  End If
  ' 返回编译结果
  If cr.Errors.Count > 0 Then
  compileOk = False
  Else
  compileOk = True
  End If
  End If
  Return compileOk
  End Function


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


  Dim strSourceFileName As String = Server.MapPath("~/App_Code/Class1.vb") '类文件的全路径
  Dim strDllPath As String = Server.MapPath("~/App_Code") '编译后的DLL文件存放的位置
  Dim strDllName As String = "" 'DLL的全路径(返回值)
  CompileExecutable(strSourceFileName, strDllPath, strDllName) '编译原文件为DLL文件
  Dim a As [Assembly] = [Assembly].LoadFrom(strDllName) '加载DLL
  Dim myType As System.Type = a.GetType("Class1") '获得Class1的Type
  Dim obj As Object = Activator.CreateInstance(myType) '获得Class1的实例


  3.Class1.vb原文件


  Public Class Class1Class Class1
  Public i As Integer
  End Class





添加到del.icio.us 添加到新浪ViVi 添加到百度搜藏 添加到POCO网摘 添加到天天网摘365Key 添加到和讯网摘 添加到天极网摘 添加到黑米书签 添加到QQ书签 添加到雅虎收藏 添加到奇客发现 diigo it 添加到饭否 添加到飞豆订阅 添加到抓虾收藏 添加到鲜果订阅 digg it 貼到funP 添加到有道阅读 Live Favorites 添加到Newsvine 打印本页 用Email发送本页 在Facebook上分享


Disclaimer Privacy Policy About us Site Map

If you have any requirements, please contact webmaster。(如果有什么要求,请联系站长)
Copyright ©2011-
uuhomepage.com, Inc. All rights reserved.