سؤال

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. Source using mshtml.

Then I'm trying to compile it using csc.exe, it says:

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

I think it because mshtml is ActiveX library. So The question is how to assemble source usings activeX mshtml.

p.s. Source has no errors and successfully has compiled from VS but can't be compiled by "on the fly" compilation.

هل كانت مفيدة؟

المحلول

I thows FileNotFoundException

That's normal, Microsoft.mshtml.dll is a primary interop assembly. It is not part of the .NET Framework so cannot be located automatically. It also won't be available on the user's machine, PIAs have to be installed.

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. Select it from the References node and set the Isolated property to False, Copy Local to True. Rebuild and verify that you now have Microsoft.mshtml.dll present in your bin\Debug directory.

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 مع الإسناد
لا تنتمي إلى StackOverflow
scroll top