Mono - 编译器即服务,如何创建编译类的实例
我正在使用这个简单的程序:-
var evaluator = new Evaluator(
new CompilerSettings(),
new Report(new ConsoleReportPrinter()));
// Make it reference our own assembly so it can use IFoo
evaluator.ReferenceAssembly(typeof(IFoo).Assembly);
// Feed it some code
evaluator.Compile(
@"
public class Foo : MonoCompilerDemo.IFoo
{
public string Bar(string s) { return s.ToUpper(); }
}");
有没有办法,我可以在主程序中使用已编译类 foo 的实例。编译中存在需要委托的重载,但我无法理解它的用法
I am using this simple program :-
var evaluator = new Evaluator(
new CompilerSettings(),
new Report(new ConsoleReportPrinter()));
// Make it reference our own assembly so it can use IFoo
evaluator.ReferenceAssembly(typeof(IFoo).Assembly);
// Feed it some code
evaluator.Compile(
@"
public class Foo : MonoCompilerDemo.IFoo
{
public string Bar(string s) { return s.ToUpper(); }
}");
Is there a way , I can use the instance of compiled class foo with in the main program. There is overload in compile that takes a delegate, but I am unable to understand its usage
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我一直在寻找类似的解决方案,但没有找到任何东西。直到我做了这样的事情......
给出这个带有简单程序的片段。
script.Bar("hello")
会产生“HELLO”I was looking for a similar solution and had no luck finding anything. Until I did something like this...
Given this snippet with the simple program.
script.Bar("hello")
would produce "HELLO"为什么不直接将此片段添加到您正在执行的代码中来编译您的类呢?
然后
甚至更好的是,只需将“工厂方法”编译到您的代码中:)
然后在“外部”将返回的值转换回
Func
并使用它:Why not just add this fragment to the code you are executing to compile your class?
and then
or even better, just compile a "factory method" into your code :)
and then "outside" you just cast the returned value back into a
Func<IFoo>
and use it: