简体   繁体   English

即时编译程序集

[英]Compile Assembly on the fly

I have this code: 我有这个代码:

static void Main(string[] args)
{
    CompilerParameters cp = new CompilerParameters
    {
        GenerateInMemory = true,
        IncludeDebugInformation = false,

    };

    cp.ReferencedAssemblies.AddRange(new string[]{
        "System.dll",
        "System.Data.dll",
        "System.Xml.dll",
        "Microsoft.mshtml.dll",
        "System.Windows.Forms.dll"
    });

    Assembly _assembly = Assembly.GetExecutingAssembly();
    StreamReader _textStreamReader = new StreamReader(_assembly.GetManifestResourceStream("myprog.restext.txt"));

    string src = _textStreamReader.ReadToEnd();
    byte[] code = Convert.FromBase64String(src);

    src = Encoding.UTF8.GetString(code);

    CompilerResults cr = CSharpCodeProvider.CreateProvider("CSharp").
        CompileAssemblyFromSource(cp, src);
    Assembly asm = cr.CompiledAssembly;
    Type typ = asm.GetType("clicker.Program");
    MethodInfo method = typ.GetMethod("DoStart");
    method.Invoke(null, new[] { (object)args });
}

I thows FileNotFoundException becouse CompileAssemblyFromSource returns the same error. 我发布FileNotFoundException CompileAssemblyFromSource返回相同的错误。 Source using mshtml . 来源使用mshtml

Then I'm trying to compile it using csc.exe, it says: 然后我尝试使用csc.exe编译它,它说:

error CS0006. (no Metadata for "Microsoft.mshtml.dll")

I think it because mshtml is ActiveX library. 我认为这是因为mshtml是ActiveX库。 So The question is how to assemble source usings activeX mshtml. 所以问题是如何使用activeX mshtml组装源代码。

ps Source has no errors and successfully has compiled from VS but can't be compiled by "on the fly" compilation. ps源没有错误并成功从VS编译但无法通过“即时”编译进行编译。

I thows FileNotFoundException 我抛出FileNotFoundException

That's normal, Microsoft.mshtml.dll is a primary interop assembly . 这是正常的,Microsoft.mshtml.dll是主要的互操作程序集 It is not part of the .NET Framework so cannot be located automatically. 它不是.NET Framework的一部分,因此无法自动定位。 It also won't be available on the user's machine, PIAs have to be installed. 它也不会在用户的机器上提供,必须安装PIA。

The best way to go about it is to ensure that the assembly is present in your build directory so it will be deployed along with your program and can always be found. 最好的方法是确保程序集存在于您的构建目录中,以便它随程序一起部署并始终可以找到。 Project + Add Reference, select Microsoft.mshtml. Project + Add Reference,选择Microsoft.mshtml。 Select it from the References node and set the Isolated property to False, Copy Local to True. 从References节点中选择它,并将Isolated属性设置为False,将Local Local设置为True。 Rebuild and verify that you now have Microsoft.mshtml.dll present in your bin\\Debug directory. 重建并验证您的bin \\ Debug目录中是否存在Microsoft.mshtml.dll。

And modify your code to pass the full path name to the file. 并修改您的代码以将完整路径名传递给该文件。 Like this: 像这样:

    var referenceAssemblies = new List<string> {
        "System.dll",
        "System.Data.dll",
        "System.Xml.dll",
        "System.Windows.Forms.dll" 
    };
    var homedir = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
    var mshtml = Path.Combine(homedir, "Microsoft.mshtml.dll");
    referenceAssemblies.Add(mshtml);

    cp.ReferencedAssemblies.AddRange(referenceAssemblies.ToArray());

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM