评估代码和AppDomains [英] Eval code and AppDomains

查看:75
本文介绍了评估代码和AppDomains的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些复杂的逻辑,只需简单地构建成一个字符串。

我需要一种方法来Eval这个字符串并返回一个布尔结果。

这个代码可以很好地实现这个目标。


我的问题是当

方法运行完毕时动态创建的程序集会发生什么? GC是否会处理它?<​​br />
或者在ASP.Net流程被回收之前它是否卡在RAM中?


此代码执行频繁(可能是4个)每次交易的时间)我和b $ b担心我可能会占用内存而不是在我的

ASP.Net应用程序中释放它。

我不想让像这样的代码关闭网络服务器只是因为我没有正确清理它。


我快速看了一下使用任务管理器但是当代码运行时,没有创建新的

进程。是因为它在ASP.Net进程中运行吗?


我看过几篇帖子提到创建一个单独的appdomain

处理这种类型问题。

是否有一些完整的示例代码显示如何执行此操作

而不会意外地将动态程序集重新加载到ASP.Net中

流程?


提前感谢您的帮助。


-

Joe Fallon


======================================== ========== ==========================

====

我的网页调用动态汇编代码如下:其中sbCode是String格式的

复杂布尔逻辑。


Dim objEval作为New EvalProvider

Dim objResult As Object = objEval.Eval(sbCode.ToString)


============== ==================================== ============== ============

====

动态汇编代码:

====== == ========================================== ======== ==================

====

进口Microsoft.VisualBasic

进口系统

进口System.Text

进口System.CodeDom.Compiler

进口System.Reflection

进口系统.IO


命名空间myNamespace


'''''< summary>

'''' ''想想这个:你的应用程序有很多业务逻辑,有些需要复杂的逻辑代码串,而这需要复杂的逻辑代码串。

'''''可能会改变时间满足某些商业条件或

元数据。如果你能够根据某些存储的某些

从你的数据库中删除最多的b / b'''''当前的代码串,那不是很好吗?程序输入参数,并确保它运行

''''''''''''''''''''''''''''''''''''''''''''事实上,返回的

代码字符串甚至可以根据sproc本身的一些输入参数动态创建。

'''''< / summary>

''''''

''''''<评论>

'''''' http:// www。 eggheadcafe.com/articles/20030908.asp

'''''< / remarks>


Public Class EvalProvider


公共函数Eval(ByVal vbCode As String)As Object

Dim c As VBCodeProvider = New VBCodeProvider

Dim icc As ICodeCompiler = c。 CreateCompiler()

Dim cp As CompilerParameters = New CompilerParameters


''注意:此列表与sb.Append中的Imports列表非常匹配
以下!!

cp.ReferencedAssemblies.Add(" system.dll")

cp.Referen cedAssemblies.Add(" system.data.dll")

cp.ReferencedAssemblies.Add(" system.xml.dll")


cp.GenerateInMemory = True


Dim sb As StringBuilder = New StringBuilder("")

sb.Append(" Imports System" &安培; vbCrLf)

sb.Append(" Imports System.Data"& vbCrLf)

sb.Append(" Imports System.Xml"& vbCrLf)

sb.Append(" Namespace myNamespace"& vbCrLf)

sb.Append(" Class myDynamicLib"& vbCrLf)

sb.Append(" public function EvalCode()as Object"& vbCrLf)

sb.Append(vbCode& vbCrLf)

sb.Append("结束功能& vbCrLf)

sb.Append(" End Class& vbCrLf)

sb.Append(" End Namespace"& vbCrLf )


''调试你的eval字符串取消注释这一行

''Debug.WriteLine(sb.ToString())

>
Dim cr As CompilerResults = icc.CompileAssemblyFromSource(cp,

sb.ToString())

Dim a As System.Reflection.Assembly = cr.CompiledAssembly

Dim o As Object

Dim mi As MethodInfo

o = a.CreateInstance(" myNamespace.myDynamicLi b")

Dim t As Type = o.GetType()

mi = t.GetMethod(" EvalCode")

Dim s作为对象

s = mi.Invoke(o,Nothing)

返回s


结束功能

$>
结束班级


结束命名空间

==================== ============================== ==================== ======

====

I have some complex logic which is fairly simply to build up into a string.
I needed a way to Eval this string and return a Boolean result.
This code works fine to achieve that goal.

My question is what happens to the dynamically created assembly when the
method is done running? Does GC take care of it?
Or is it stuck in RAM until the ASP.Net process is recycled?

This code executes pretty frequently (maybe 4 times per transaction) and I
am concerned that I could be eating up RAM and not releasing it in my
ASP.Net application.
I would hate to have code like this bring down the web server just because I
didn''t clean it up correctly.

I took a quick look with Task Manager but when the code ran there was no new
process created. Is that because it is running inside the ASP.Net process?

I have seen a couple of posts that mention creating a separate appdomain to
handle this type of issue.
Is there some complete sample code somewhere that shows how to do this
without accidentally re-loading the dynamic assembly into the ASP.Net
process?

Thanks in advance for any help.

--
Joe Fallon

================================================== ==========================
====
My web page calls the dynamic assembly code like this: where sbCode is the
complex Boolean logic in String format.

Dim objEval As New EvalProvider
Dim objResult As Object = objEval.Eval(sbCode.ToString)

================================================== ==========================
====
Dynamic Assembly code:
================================================== ==========================
====
Imports Microsoft.VisualBasic
Imports System
Imports System.Text
Imports System.CodeDom.Compiler
Imports System.Reflection
Imports System.IO

Namespace myNamespace

'''''' <summary>
'''''' Think about this: Your application does a lot of business logic, some
of which requires complicated logical strings of code
'''''' that may change over time to meet certain business conditions or
metadata. Wouldn''t it be great if you could pull the most
'''''' current string of code to be run out of your database based on certain
stored procedure input parameters, and be sure it''s run
'''''' and you get back the desired result? In fact, the returned string of
code may even be dynamically created based on some of the
'''''' input parameters from the sproc itself.
'''''' </summary>
''''''
'''''' <remarks>
'''''' http://www.eggheadcafe.com/articles/20030908.asp
'''''' </remarks>

Public Class EvalProvider

Public Function Eval(ByVal vbCode As String) As Object
Dim c As VBCodeProvider = New VBCodeProvider
Dim icc As ICodeCompiler = c.CreateCompiler()
Dim cp As CompilerParameters = New CompilerParameters

''Note: this list much match the list of Imports in the sb.Append
below!!
cp.ReferencedAssemblies.Add("system.dll")
cp.ReferencedAssemblies.Add("system.data.dll")
cp.ReferencedAssemblies.Add("system.xml.dll")

cp.CompilerOptions = "/t:library"
cp.GenerateInMemory = True

Dim sb As StringBuilder = New StringBuilder("")
sb.Append("Imports System" & vbCrLf)
sb.Append("Imports System.Data" & vbCrLf)
sb.Append("Imports System.Xml" & vbCrLf)
sb.Append("Namespace myNamespace " & vbCrLf)
sb.Append("Class myDynamicLib " & vbCrLf)
sb.Append("public function EvalCode() as Object " & vbCrLf)
sb.Append(vbCode & vbCrLf)
sb.Append("End Function " & vbCrLf)
sb.Append("End Class " & vbCrLf)
sb.Append("End Namespace" & vbCrLf)

''to debug your eval string uncomment this line
''Debug.WriteLine(sb.ToString())

Dim cr As CompilerResults = icc.CompileAssemblyFromSource(cp,
sb.ToString())
Dim a As System.Reflection.Assembly = cr.CompiledAssembly
Dim o As Object
Dim mi As MethodInfo
o = a.CreateInstance("myNamespace.myDynamicLib ")
Dim t As Type = o.GetType()
mi = t.GetMethod("EvalCode")
Dim s As Object
s = mi.Invoke(o, Nothing)
Return s

End Function

End Class

End Namespace
================================================== ==========================
====

推荐答案

你好Joe:


加载一个程序集后就不能删除了,遗憾的是GC不能在那里帮助


代码运行在与asp.net应用程序相同的过程中,所以你不会看到

a新进程出现。


唯一真正摆脱它的方法汇编和回收内存是指你使用

a新的appdomain。

这里有好文章: http://www.we st-wind.com/presentatio...ynamicCode.htm


给大量用户。跨越应用程序域并生成这些程序集如果你需要每次请求4次,
可以咀嚼CPU和内存。


-

Scott
http://www.OdeToCode.com/ blogs / scott /
Hi Joe:

Once an assembly is loaded it can''t be removed, unfortunately the GC can''t
help there.
The code runs inside the same process as you asp.net app so you won''t see
a new process appear.

The only true way to get rid of the assembly and reclaim memory is to use
a new appdomain as you pointed out.
There is good article here: http://www.west-wind.com/presentatio...ynamicCode.htm

I''d also have some concern with performance if you need to scale this up
to a large number of users. Crossing app domains and generating these assemblies
can chew up CPU and memory if you need to do this 4 times per request.

--
Scott
http://www.OdeToCode.com/blogs/scott/
我有一些复杂的逻辑,相当简单地构建成一个
字符串。
我需要一种方法来Eval这个字符串并返回一个布尔结果。
这段代码可以很好地实现这个目标。
我的问题是当
方法运行完毕时动态创建的程序集会发生什么? GC是否会处理它?<​​br />或者在ASP.Net流程被回收之前它是否卡在RAM中?
此代码执行频繁(每次事务可能4次)
我是我担心我可能会占用RAM而不是在我的ASP.Net应用程序中释放它。
我不想让这样的代码关闭web服务器
因为我
没有正确清理它。
我快速查看了任务管理器但是当代码运行时没有创建新进程。是因为它在ASP.Net进程中运行吗?

我看过几篇帖子提到创建一个单独的
appdomain来处理这种类型问题。
是否有一些完整的示例代码显示如何执行此操作
而不会意外地将动态程序集重新加载到ASP.Net
进程中?
提前感谢任何帮助。

====================================== ============ ====================
======
====
我的网页调用动态汇编代码如下:其中sbCode是String格式的复杂布尔逻辑。
Dim objEval As New EvalProvider
Dim objResult As Object = objEval.Eval(sbCode.ToString)
==================================== ============== ====================
======
== ==
动态汇编代码:
==================================== ============== ====================
======
== ==
进口Microsoft.V isualBasic
进口系统
Imports System.Text
Imports System.CodeDom.Compiler
Imports System.Reflection
Imports System.IO
Namespace myNamespace
'''''< summary>
''''''想想看:你的应用程序有很多业务逻辑,
其中一些需要复杂的逻辑代码串
'''''可能会随着时间的推移而变化,以满足特定的业务条件或元数据。如果您能够根据某些存储过程输入参数从数据库中删除最多的当前代码串,那不是很好吗?并确保它已经运行
'''''你得到了预期的结果?实际上,返回的
代码串甚至可以根据sproc本身的一些输入参数动态创建。
''' '''< / summary>
'''''
''''''<备注>
'''''' http://www.eggheadcafe.com/articles/20030908.asp
''' '''< / remarks>
Public Class EvalProvider

Public Function Eval(ByVal vbCode As String)As Object
Dim c As VBCodeProvider = New VBCodeProvider
Dim icc作为ICodeCompiler = c.CreateCompiler()
Dim cp As CompilerParameters = New CompilerParameters
''注意:此列表与sb.Append
下面的Imports列表非常匹配!!
cp.ReferencedAssemblies.Add(" system.dll")
cp.ReferencedAssemblies.Add(" system.data.dll")
cp.ReferencedAssemblies.Add(" sy stem.xml.dll")
cp.CompilerOptions =" / t:library"
cp.GenerateInMemory = True
Dim sb As StringBuilder = New StringBuilder("")
sb.Append(" Imports System" &安培; vbCrLf)
sb.Append(" Imports System.Data"& vbCrLf)
sb.Append(" Imports System.Xml"& vbCrLf)
sb.Append("命名空间myNamespace"& vbCrLf)
sb.Append(" Class myDynamicLib"& vbCrLf)
sb.Append(" public function EvalCode()as Object"& vbCrLf)< sb.Append(vbCode& vbCrLf)
sb.Append(" End Function& vbCrLf)
sb.Append(" End Class& vbCrLf)
sb.Append(" End Namespace"& vbCrLf)
''调试你的eval字符串取消注释这一行
''Debug.WriteLine(sb.ToString())
昏暗cr作为CompilerResults = icc.CompileAssemblyFromSource(cp,
sb.ToString())
Dim a As System.Reflection.Assembly = cr.CompiledAssembly
Dim o As Object
Dim mi As MethodInfo
o = a.CreateInstance(" myNamespace.myDynamicLib")
Dim t As Type = o.GetType()
mi = t.GetMethod(" EvalCode")<昏暗s作为对象
s = mi.Invoke(o,Nothing)
返回结束功能

结束类

结束命名空间< br => =============================================== === ====================
====== ====
I have some complex logic which is fairly simply to build up into a
string.
I needed a way to Eval this string and return a Boolean result.
This code works fine to achieve that goal.
My question is what happens to the dynamically created assembly when
the
method is done running? Does GC take care of it?
Or is it stuck in RAM until the ASP.Net process is recycled?
This code executes pretty frequently (maybe 4 times per transaction)
and I
am concerned that I could be eating up RAM and not releasing it in my
ASP.Net application.
I would hate to have code like this bring down the web server just
because I
didn''t clean it up correctly.
I took a quick look with Task Manager but when the code ran there was
no new process created. Is that because it is running inside the
ASP.Net process?

I have seen a couple of posts that mention creating a separate
appdomain to
handle this type of issue.
Is there some complete sample code somewhere that shows how to do this
without accidentally re-loading the dynamic assembly into the ASP.Net
process?
Thanks in advance for any help.

================================================== ====================
======
====
My web page calls the dynamic assembly code like this: where sbCode is
the
complex Boolean logic in String format.
Dim objEval As New EvalProvider
Dim objResult As Object = objEval.Eval(sbCode.ToString)
================================================== ====================
======
====
Dynamic Assembly code:
================================================== ====================
======
====
Imports Microsoft.VisualBasic
Imports System
Imports System.Text
Imports System.CodeDom.Compiler
Imports System.Reflection
Imports System.IO
Namespace myNamespace

'''''' <summary>
'''''' Think about this: Your application does a lot of business logic,
some
of which requires complicated logical strings of code
'''''' that may change over time to meet certain business conditions or
metadata. Wouldn''t it be great if you could pull the most
'''''' current string of code to be run out of your database based on
certain
stored procedure input parameters, and be sure it''s run
'''''' and you get back the desired result? In fact, the returned
string of
code may even be dynamically created based on some of the
'''''' input parameters from the sproc itself.
'''''' </summary>
''''''
'''''' <remarks>
'''''' http://www.eggheadcafe.com/articles/20030908.asp
'''''' </remarks>
Public Class EvalProvider

Public Function Eval(ByVal vbCode As String) As Object
Dim c As VBCodeProvider = New VBCodeProvider
Dim icc As ICodeCompiler = c.CreateCompiler()
Dim cp As CompilerParameters = New CompilerParameters
''Note: this list much match the list of Imports in the sb.Append
below!!
cp.ReferencedAssemblies.Add("system.dll")
cp.ReferencedAssemblies.Add("system.data.dll")
cp.ReferencedAssemblies.Add("system.xml.dll")
cp.CompilerOptions = "/t:library"
cp.GenerateInMemory = True
Dim sb As StringBuilder = New StringBuilder("")
sb.Append("Imports System" & vbCrLf)
sb.Append("Imports System.Data" & vbCrLf)
sb.Append("Imports System.Xml" & vbCrLf)
sb.Append("Namespace myNamespace " & vbCrLf)
sb.Append("Class myDynamicLib " & vbCrLf)
sb.Append("public function EvalCode() as Object " & vbCrLf)
sb.Append(vbCode & vbCrLf)
sb.Append("End Function " & vbCrLf)
sb.Append("End Class " & vbCrLf)
sb.Append("End Namespace" & vbCrLf)
''to debug your eval string uncomment this line
''Debug.WriteLine(sb.ToString())
Dim cr As CompilerResults = icc.CompileAssemblyFromSource(cp,
sb.ToString())
Dim a As System.Reflection.Assembly = cr.CompiledAssembly
Dim o As Object
Dim mi As MethodInfo
o = a.CreateInstance("myNamespace.myDynamicLib ")
Dim t As Type = o.GetType()
mi = t.GetMethod("EvalCode")
Dim s As Object
s = mi.Invoke(o, Nothing)
Return s
End Function

End Class

End Namespace
================================================== ====================
====== ====



Joe,


如果不深入或尝试,我会在

组件中设置您使用的类。


Cor
Joe,

Without looking to deep or trying, would I set the class you use in a
component.

Cor


Scott,

每次交易4次,这是大型网站的单页。它是
不是每一次打击。


我想我看过那篇文章 - 我会仔细阅读。我在其他领域使用Rick的




你知道Eval a String的任何其他方法来获得布尔结果吗? />
比如下所示动态生成程序集?


你知道Eval是否是.Net版本2.0中的内置函数

框架?


谢谢。

-

Joe Fallon


" Scott Allen" < SC *** @ nospam.OdeToCode.com>在消息中写道

news:59 ********************* @ msnews.microsoft.com。 ..
Scott,
That was 4 times per transaction which is a single page in a large site. It
is not every hit.

I think I have seen that article - I will read it closely. I use Rick''s
stuff in other areas.

Do you know of any other way to Eval a String to get a Boolean result other
than dynamically generating the assembly as shown below?

Do you know if Eval will be a built-in function in ver 2.0 of the .Net
framework?

Thanks.
--
Joe Fallon

"Scott Allen" <sc***@nospam.OdeToCode.com> wrote in message
news:59*********************@msnews.microsoft.com. ..
嗨Joe:

一旦装配了装配就无法移除,遗憾的是GC无法帮助那里。代码在与asp.net应用程序相同的进程中运行,因此你不会看到
出现一个新进程。

摆脱程序集的唯一真正方法回收内存就是使用你指出的新appdomain。
这里有好文章:
http://www.west-wind.com/presentatio...ynamicCode.htm
我也有如果您需要将此扩展到大量用户,请关注性能。如果你需要每次请求执行4次,那么跨越应用程序域并生成这些
程序集可以咀嚼CPU和内存。

-
Scott
http://www.OdeToCode.com/blogs/scott/
Hi Joe:

Once an assembly is loaded it can''t be removed, unfortunately the GC can''t
help there.
The code runs inside the same process as you asp.net app so you won''t see
a new process appear.

The only true way to get rid of the assembly and reclaim memory is to use
a new appdomain as you pointed out.
There is good article here: http://www.west-wind.com/presentatio...ynamicCode.htm
I''d also have some concern with performance if you need to scale this up
to a large number of users. Crossing app domains and generating these assemblies can chew up CPU and memory if you need to do this 4 times per request.

--
Scott
http://www.OdeToCode.com/blogs/scott/
我有一些复杂的逻辑,相当简单地构建成一个
字符串。
我需要一种方法来Eval这个字符串并返回一个布尔结果。
这段代码可以很好地实现这个目标。
我的问题是当
方法运行完毕后动态创建的程序集会发生什么? GC是否会处理它?<​​br />或者在ASP.Net流程被回收之前它是否卡在RAM中?
此代码执行频繁(每次事务可能4次)
我是我担心我可能会占用RAM而不是在我的ASP.Net应用程序中释放它。
我不想让这样的代码关闭web服务器
因为我
没有正确清理它。
我快速查看了任务管理器但是当代码运行时没有创建新进程。是因为它在ASP.Net进程中运行吗?

我看过几篇帖子提到创建一个单独的
appdomain来处理这种类型问题。
是否有一些完整的示例代码显示如何执行此操作
而不会意外地将动态程序集重新加载到ASP.Net
进程中?
提前感谢任何帮助。

====================================== ============ ====================
======
====
我的网页调用动态汇编代码如下:其中sbCode是String格式的复杂布尔逻辑。
Dim objEval As New EvalProvider
Dim objResult As Object = objEval.Eval(sbCode.ToString)
==================================== ============== ====================
======
== ==
动态汇编代码:
==================================== ============== ====================
======
== ==
进口Microsoft.V isualBasic
进口系统
Imports System.Text
Imports System.CodeDom.Compiler
Imports System.Reflection
Imports System.IO
Namespace myNamespace
'''''< summary>
''''''想想看:你的应用程序有很多业务逻辑,
其中一些需要复杂的逻辑代码串
'''''可能会随着时间的推移而变化,以满足特定的业务条件或元数据。如果您能够根据某些存储过程输入参数从数据库中删除最多的当前代码串,那不是很好吗?并确保它已经运行
'''''你得到了预期的结果?实际上,返回的
代码串甚至可以根据sproc本身的一些输入参数动态创建。
''' '''< / summary>
'''''
''''''<备注>
'''''' http://www.eggheadcafe.com/articles/20030908.asp
''' '''< / remarks>
Public Class EvalProvider

Public Function Eval(ByVal vbCode As String)As Object
Dim c As VBCodeProvider = New VBCodeProvider
Dim icc作为ICodeCompiler = c.CreateCompiler()
Dim cp As CompilerParameters = New CompilerParameters
''注意:此列表与sb.Append
下面的Imports列表非常匹配!!
cp.ReferencedAssemblies.Add(" system.dll")
cp.ReferencedAssemblies.Add(" system.data.dll")
cp.ReferencedAssemblies.Add(" sy stem.xml.dll")
cp.CompilerOptions =" / t:library"
cp.GenerateInMemory = True
Dim sb As StringBuilder = New StringBuilder("")
sb.Append(" Imports System" &安培; vbCrLf)
sb.Append(" Imports System.Data"& vbCrLf)
sb.Append(" Imports System.Xml"& vbCrLf)
sb.Append("命名空间myNamespace"& vbCrLf)
sb.Append(" Class myDynamicLib"& vbCrLf)
sb.Append(" public function EvalCode()as Object"& vbCrLf)< sb.Append(vbCode& vbCrLf)
sb.Append(" End Function& vbCrLf)
sb.Append(" End Class& vbCrLf)
sb.Append(" End Namespace"& vbCrLf)
''调试你的eval字符串取消注释这一行
''Debug.WriteLine(sb.ToString())
昏暗cr作为CompilerResults = icc.CompileAssemblyFromSource(cp,
sb.ToString())
Dim a As System.Reflection.Assembly = cr.CompiledAssembly
Dim o As Object
Dim mi As MethodInfo
o = a.CreateInstance(" myNamespace.myDynamicLib")
Dim t As Type = o.GetType()
mi = t.GetMethod(" EvalCode")<昏暗s作为对象
s = mi.Invoke(o,Nothing)
返回结束功能

结束类

结束命名空间< br => =============================================== === ====================
====== ====
I have some complex logic which is fairly simply to build up into a
string.
I needed a way to Eval this string and return a Boolean result.
This code works fine to achieve that goal.
My question is what happens to the dynamically created assembly when
the
method is done running? Does GC take care of it?
Or is it stuck in RAM until the ASP.Net process is recycled?
This code executes pretty frequently (maybe 4 times per transaction)
and I
am concerned that I could be eating up RAM and not releasing it in my
ASP.Net application.
I would hate to have code like this bring down the web server just
because I
didn''t clean it up correctly.
I took a quick look with Task Manager but when the code ran there was
no new process created. Is that because it is running inside the
ASP.Net process?

I have seen a couple of posts that mention creating a separate
appdomain to
handle this type of issue.
Is there some complete sample code somewhere that shows how to do this
without accidentally re-loading the dynamic assembly into the ASP.Net
process?
Thanks in advance for any help.

================================================== ====================
======
====
My web page calls the dynamic assembly code like this: where sbCode is
the
complex Boolean logic in String format.
Dim objEval As New EvalProvider
Dim objResult As Object = objEval.Eval(sbCode.ToString)
================================================== ====================
======
====
Dynamic Assembly code:
================================================== ====================
======
====
Imports Microsoft.VisualBasic
Imports System
Imports System.Text
Imports System.CodeDom.Compiler
Imports System.Reflection
Imports System.IO
Namespace myNamespace

'''''' <summary>
'''''' Think about this: Your application does a lot of business logic,
some
of which requires complicated logical strings of code
'''''' that may change over time to meet certain business conditions or
metadata. Wouldn''t it be great if you could pull the most
'''''' current string of code to be run out of your database based on
certain
stored procedure input parameters, and be sure it''s run
'''''' and you get back the desired result? In fact, the returned
string of
code may even be dynamically created based on some of the
'''''' input parameters from the sproc itself.
'''''' </summary>
''''''
'''''' <remarks>
'''''' http://www.eggheadcafe.com/articles/20030908.asp
'''''' </remarks>
Public Class EvalProvider

Public Function Eval(ByVal vbCode As String) As Object
Dim c As VBCodeProvider = New VBCodeProvider
Dim icc As ICodeCompiler = c.CreateCompiler()
Dim cp As CompilerParameters = New CompilerParameters
''Note: this list much match the list of Imports in the sb.Append
below!!
cp.ReferencedAssemblies.Add("system.dll")
cp.ReferencedAssemblies.Add("system.data.dll")
cp.ReferencedAssemblies.Add("system.xml.dll")
cp.CompilerOptions = "/t:library"
cp.GenerateInMemory = True
Dim sb As StringBuilder = New StringBuilder("")
sb.Append("Imports System" & vbCrLf)
sb.Append("Imports System.Data" & vbCrLf)
sb.Append("Imports System.Xml" & vbCrLf)
sb.Append("Namespace myNamespace " & vbCrLf)
sb.Append("Class myDynamicLib " & vbCrLf)
sb.Append("public function EvalCode() as Object " & vbCrLf)
sb.Append(vbCode & vbCrLf)
sb.Append("End Function " & vbCrLf)
sb.Append("End Class " & vbCrLf)
sb.Append("End Namespace" & vbCrLf)
''to debug your eval string uncomment this line
''Debug.WriteLine(sb.ToString())
Dim cr As CompilerResults = icc.CompileAssemblyFromSource(cp,
sb.ToString())
Dim a As System.Reflection.Assembly = cr.CompiledAssembly
Dim o As Object
Dim mi As MethodInfo
o = a.CreateInstance("myNamespace.myDynamicLib ")
Dim t As Type = o.GetType()
mi = t.GetMethod("EvalCode")
Dim s As Object
s = mi.Invoke(o, Nothing)
Return s
End Function

End Class

End Namespace
================================================== ====================
====== ====




这篇关于评估代码和AppDomains的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆