一个函数功能<>与罗斯林一个函数、功能、罗斯林、LT

由网友(。你自信的时候真的美多了)分享简介:灵感这个和这文章,我试图创造一个充满活力的功能与罗斯林。Inspired by this and this article, I'm trying to create a dynamic function with Roslyn.然而,提到来源是过时的或不完整的,我不能够创造出功能性样本。我的工作至今:Howev...

灵感这个和这文章,我试图创造一个充满活力的功能与罗斯林。

Inspired by this and this article, I'm trying to create a dynamic function with Roslyn.

然而,提到来源是过时的或不完整的,我不能够创造出功能性样本。我的工作至今:

However the mentioned sources are outdated or not complete and I'm not able to create a functional sample. My work so far:

var code = @"Func<int, int> doStuffToInt = i =>
{
   var result = i;
   for (var y = i; y <= i * 2; y++)
   {
      result += y;
   }
   return result;
};";


var se = new ScriptEngine();
var session = se.CreateSession();
session.AddReference(typeof(Program).Assembly);
session.AddReference(typeof(Expression).Assembly);

session.ImportNamespace("System");
session.ImportNamespace("System.Linq");
session.ImportNamespace("System.Linq.Expressions");

var submission = session.CompileSubmission<Func<int, int>>(code);

Func<int, int> myFunc =  submission.Execute();

然而 myFunc的总是空,我不能够确定问题所在。有人可以帮助我,使这个样品运行?

However myFunc is always null and I'm not able to identify where the problem is. Can someone help me out to make this sample run?

推荐答案

免责声明:我还没有实际使用罗斯林在愤怒太大的所有

Disclaimer: I haven't actually used Roslyn in anger much at all.

目前的code声明一个变量,但没有用它做任何事情之后。根据this随机博客帖子,它看起来像你可能只需要一个额外的前pression宣布后:

Currently your code declares a variable, but doesn't do anything with it afterwards. Based on this random blog post, it looks like you possibly just need an extra expression after the declaration:

var code = @"Func<int, int> doStuffToInt = i =>
{
   var result = i;
   for (var y = i; y <= i * 2; y++)
   {
      result += y;
   }
   return result;
};
doStuffToInt"; // This is effectively the return statement for the script...

我不保证它会工作,但不妨一试:)

I don't guarantee it'll work, but give it a try :)

阅读全文

相关推荐

最新文章