有关动态编译执行后,释放内在资料的问题,分不够再加
以下这段代码是动态执行C#表达试,执行完后,内存资源没有释放,
public object GetValue( string value,string eval)
{
string codeSnippet = "using System; " + "\r\n" +
"using System.Collections;"+
"namespace WrokFlowExpression {" + "\r\n" +
" public class Eval" + "\r\n" +
" {" + "\r\n" +
" public Eval(){} " + "\r\n" +
" public object GetValue()" + "\r\n" +
" {" + "\r\n" +
" " + eval +";"+
" " + "\r\n" +
" try{"+
" return " + value + ";" + " \r\n " +
" }"+
" catch{ \r\n "+
" return false; \r\n }\r\n finally{ \r\n "+
" \r\n }"+
" return false; \r\n "+
" }" + " \r\n" +
" } }";
CodeSnippetCompileUnit unit = new CodeSnippetCompileUnit( codeSnippet );
ICodeCompiler compiler = new CSharpCodeProvider().CreateCompiler();
CompilerParameters para = new CompilerParameters();
para.ReferencedAssemblies.Add( "System.dll" );
para.GenerateInMemory = false;//这里改城true与false没有用
para.GenerateExecutable = false;
para.OutputAssembly = "Eval.dll";//这里输出内在与输出本地程序集也无效
Assembly asm = compiler.CompileAssemblyFromDom( para , unit ).CompiledAssembly;
Type type = asm.GetType( "WrokFlowExpression.Eval" );
MethodInfo mi = type.GetMethod( "GetValue" , BindingFlags.Public | BindingFlags.Instance );
object obj = asm.CreateInstance( "WrokFlowExpression.Eval" );
ParameterInfo[] parame = mi.GetParameters();
object[] ars = new object[parame.Length];
for(int i=0;i<parame.Length;i++)
{
// if(i==0) ars[i] = StartUser;
// if(i==1) ars[i] = CurrentUser;
// if(i==2) ars[i] = Entity;
// if(i>3)
// {
// ars[i] = null;
// }
}
return mi.Invoke(obj , ars );
}