Create a Func<> with Roslyn

前端 未结 1 910
粉色の甜心
粉色の甜心 2021-01-20 01:36

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

相关标签:
1条回答
  • 2021-01-20 02:13

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

    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 :)

    0 讨论(0)
提交回复
热议问题