开发者

Accessing class and function after compiling ( CompiledAssembly )

Heres some example code. I successfully figured out how to compile this. I grabbed the location and was able to use visual studios object browser to look through the DLL. I cant figure out how to get a class instance and call a function.

    public static void test()
    {
        JScriptCodeProvider js = new JScriptCodeProvider();
        System.CodeDom.Compiler.CompilerParameters param = new System.CodeDom.Compiler.CompilerParameters();
        var cr = js.CompileAssemblyFromSource开发者_如何学运维(param, new string[] { "package pkg { class b { public function increment(x) { return x+1; } } }" });
        foreach (var e in cr.Errors) { 
            var s = e.ToString(); 
        }
        var asm = cr.CompiledAssembly;
        var module = cr.CompiledAssembly.GetModules();
//or var module = cr.CompiledAssembly.GetModule("JScript Module");
        //...
    }


Hmmm realy late on the answer but this is how you would invoke a method from a CodeDom compiled class

You have to use reflection to create an assembly from your compiler results...(your var cr)

Assembly assembly = cr.CompiledAssembly;

Then you have to create an instance of the class you want

object sourceClass = assembly.CreateInstance("YourNamespace.YourClass");

Then you invoke any method inside the class

var result = sourceClass.GetType().InvokeMember("YourMethod", BindingFlags.InvokeMethod, null, sourceClass, new object[] { *Parameters go here* });

And with that what ever the method you invoked had to returned would now be the value of the "result" var....pretty easy.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜