是否可以将对象传递给动态编译的代码? [英] Is it possible to pass an object into code that is compiled on the fly?

查看:88
本文介绍了是否可以将对象传递给动态编译的代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个编译''模块''的方法,''模块''包含一个从.cs文件中读取的有效C#字符串。我想知道是否可以从包含编译模块的方法的类传递一个对象到创建的程序集,允许最初在.cs文件中的代码使用该对象。



这是我用来编译模块的代码

I have a method which compiles a ''module'', a ''module'' contains a string of valid C# read from a .cs file. I am wondering whether it''s possible to pass an object from the class that contains the method to compile the module, to the assembly that is created allowing for the code initially in the .cs file to use that object.

Here is the code I use to compile the modules

public void CompileModule(string strModuleName)
{
    CompilerParameters objCompilerParameters = new CompilerParameters();
    objCompilerParameters.GenerateExecutable = false;
    objCompilerParameters.GenerateInMemory = true;
    objCompilerParameters.IncludeDebugInformation = false;
    CodeDomProvider objCompiler = CSharpCodeProvider.CreateProvider("CSharp");
    CompilerResults objCompilerResults = objCompiler.CompileAssemblyFromSource(objCompilerParameters, Modules[strModuleName].Source);
    if (objCompilerResults.Errors.HasErrors)
    {
        throw new InvalidOperationException("Syntax error.");
    }
    Assembly objCompiledAssembly = objCompilerResults.CompiledAssembly;
    MethodInfo objMethod = objCompiledAssembly.GetType(Modules[strModuleName].ClassName).GetMethod(Modules[strModuleName].CallMethod);
    Console.WriteLine((string)objMethod.Invoke(null, null));
}





模块只是字典的字典< string,>,模块是包含的类的名称.cs文件的内容。



如果模块源是



Modules is just a dictionary of dictionary<string,>, Module being the name of the class that contains the contents of the .cs file.

If the module source is

public static class Echo
{
    public static string strTest = "Testing";
    public static string EchoTestString()
    {
        return strTest;
    }
}





我如何能够将实例化对象(Fruit类型)传递给汇编,所以我可以使用Fruit对象,但我想要在Echo类中?所以.cs文件基本上可以只是





How would I be able to pass an instantiated object (of type Fruit) to the assembly, so I could use the Fruit object however I wanted inside of the Echo class? So the .cs file could essentially just be

public static class Echo
{
    public static string EchoFruitName()
    {
        return Fruit.Name;
    }
}





水果在我的编译器类中定义并传递给编译的Echo类当然。



有什么想法吗?

提前致谢:)



Fruit being defined in my compiler class and passed across to the compiled Echo class of course.

Any ideas?
Thanks in advance. :)

推荐答案

是的,可能。

添加此行

Yes, its possible.
Add this line
objCompilerParameters.CompilerOptions = "/reference:MyCurrentAssemblyWithTheFruitClass.exe";

< br $>


MyCurrentAssemblyWithTheFruitClass.exe(或.dll),它是包含水果类程序集的文件。我的测试项目是一个控制台应用程序,所以我使用.exe。

希望这是你想要的。



MyCurrentAssemblyWithTheFruitClass.exe (or .dll) which is the file containing the assembly with the fruit class. My test project was a console application, so I used the .exe.
Hope this is what you wanted.


这篇关于是否可以将对象传递给动态编译的代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆