0

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

我知道编译后的代码最终会出现在程序集中,并且在应用程序域被处置之前不能被删除。

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

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

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

4

1 回答 1

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;
    }

}
于 2015-01-29T19:46:17.883 回答