如何在不重新编译的情况下从CSharpCodeProvider访问已编译的程序集

时间:2015-01-29 15:03:28

标签: c# csharpcodeprovider compileassemblyfromsource

我有Microsoft.CSharp.CSharpCodeProvider CompileAssemblyFromSource工作。 它肯定会创造并回归我所需要的。

我知道编译后的代码会在程序集中结束,然后才会删除,直到app域被删除为止。

如何重用已编译的被调用者,而不必每次都调用CompileAssemblyFromSource(myParams,myScript)?

如果我设置myParams.OutputAssembly =' MyAssembly&#34 ;; 我似乎无法实例化它的实例。

如果代码在内存中,我如何检查它是否存在以及如何在不调用CompileAssemblyFromSource()的情况下重新加载它?

1 个答案:

答案 0 :(得分:0)

找到了可能的解决方案。至少在我的测试应用程序中工作。 不使用OutputAssembly设置时,.NET将使用随机名称创建程序集。

我从返回的代码对象中获取了名称。 objectReturned.GetType()。Module.ScopeName。

保存该名称,所以当我回来时,如果脚本没有改变,我可以使用最后编译的版本。

请记住,每次编译时,它都会在程序集中创建一个新条目。这可以在AppDomain.CurrentDomain.GetAssemblies()中找到。

/// <summary>
/// 
/// </summary>
/// <param name="scriptString">My string i want to compile</param>
/// <param name="lastCompiledName">The random name .NET gave to my last in MEMORY compile
/// objectReturned.GetType().Module.ScopeName.Replace(".dll", "") to get the name.</param>
/// <param name="doForceCompile">If i want to override the one in MEMORY</param>
/// <param name="errors"></param>
/// <returns></returns>
public IScriptBuilder CompileCodeDomCSharp(String scriptString, string lastCompiledName, bool doForceCompile, out List<string> errors)
{

    var providerOptions = new Dictionary<string, string>();
    providerOptions.Add("CompilerVersion", "v4.0");
    var provider = new Microsoft.CSharp.CSharpCodeProvider(providerOptions);


    var cp = new CompilerParameters(new[] { "System.Core.dll" }) {
        GenerateExecutable = false,
        GenerateInMemory = true,
        TreatWarningsAsErrors = false,
        WarningLevel = 3,
        CompilerOptions = "/optimize",
        IncludeDebugInformation = false
    };
    cp.ReferencedAssemblies.Add(typeof(IScriptBuilder).Assembly.Location);
    // will write file to  C:\newprojects\NewPayroll\Utilities\CSharpScriptingTest\CSharpScriptWriter\CSharpScriptWriter\bin\Debug  -- causes issues for me.
    // cp.OutputAssembly = "MyScriptRunner";        

    errors = new List<string>();

    try
    {

        var assemblies = AppDomain.CurrentDomain.GetAssemblies();
        Type type;

        // use actual name if OutputAssembly is set
        // if (doForceCompile || x.All(a => a.FullName != "MyScriptRunner, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null"))   
        if (doForceCompile || assemblies.All(a => a.FullName != lastCompiledName + ", Version=0.0.0.0, Culture=neutral, PublicKeyToken=null"))
        {
            var results = provider.CompileAssemblyFromSource(cp, scriptString);
            type = results.CompiledAssembly.GetType("MyScriptRunner");
        }
        else
        {
            // use actual name if OutputAssembly is set
            // var myScriptRunner = x.FirstOrDefault(a => a.FullName == "MyScriptRunner, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null");   
            var myScriptRunner = assemblies.FirstOrDefault(a => a.FullName == lastCompiledName + ", Version=0.0.0.0, Culture=neutral, PublicKeyToken=null");
            var myScriptRunnerObject = myScriptRunner.CreateObject("*");
            type = myScriptRunnerObject.GetType();
        }


        dynamic obj = Activator.CreateInstance(type);

        return (IScriptBuilder)obj;

    }
    catch (Exception ex)
    {
        // Not returning or handling error an my test app.
        return null;
    }

}