标签: roslyn

如何在 Roslyn 脚本环境中访问和编辑全局变量?

我有一个应用程序,我在其中使用 Roslyn 脚本引擎(命名空间Microsoft.CodeAnalysis.Scripting)。

我现在所拥有的是:

public static async Task<object> Execute(string code, CommandEventArgs e)
{
    if (_scriptState == null)
    {
        var options = ScriptOptions.Default;
        options
            .AddReferences(typeof (Type).Assembly,
                typeof (Console).Assembly,
                typeof (IEnumerable<>).Assembly,
                typeof (IQueryable).Assembly)
            .AddImports("System", "System.Numerics", "System.Text", "System.Linq", "System.Collections.Generics",
                "System.Security.Cryptography");
        _scriptState = await CSharpScript.RunAsync(code, options, new MessageGlobal {e = e});
    }
    else
    {
        // TODO: set global e (it's not in this variables list, thus it throws null reference)
        _scriptState.GetVariable("e").Value = e;
        _scriptState = await _scriptState.ContinueWithAsync(code);
    }
    return !string.IsNullOrEmpty(_scriptState.ReturnValue?.ToString()) ? _scriptState.ReturnValue …
Run Code Online (Sandbox Code Playgroud)

c# scripting roslyn

0
推荐指数
1
解决办法
3078
查看次数

来自 Roslyn 语义模型的 GetTypeInfo 无法找到“var”的类型

我有以下代码:

public class ParallelLinqAsSequential
{
    private List<Customer> _orders;

    private void Method()
    {
        var query = (_orders.AsParallel().OrderBy(ord => ord.CustomerID).Select(ord => new
        {
            Date = ord.OrderDate
        })).AsSequential().Take(5);
    }

    private class Customer
    {
        public string CustomerID;
        public DateTime OrderDate { get; set; }
    }
}
Run Code Online (Sandbox Code Playgroud)

我期望在通过名为“query”的变量调用语义模型时,它能够将其推断为具有“DateTime”类型字段的匿名类型的 Enumerable。但它失败并显示错误类型。

在 Visual Studio 中,您可以看到它,如下图所示。

在此处输入图片说明

我用来从 Roslyn 获取此代码的代码是:

public void GetType(SyntaxTree tree)
{
            var Mscorlib = MetadataReference.CreateFromFile(typeof(object).Assembly.Location);
            var compilation = CSharpCompilation.Create("RoslynVar", syntaxTrees: new[] { tree }, references: new[] { Mscorlib });


            VariableDeclarationSyntax variable = ... // get …
Run Code Online (Sandbox Code Playgroud)

c# roslyn

0
推荐指数
1
解决办法
1058
查看次数

使用 Roslyn 获取忽略 lambda 表达式中的返回语句

我正在尝试使用 roslyn 来获取方法的返回语句,为此我正在使用它:

var returns = methods.DescendantNodes().OfType<ReturnStatementSyntax>();
Run Code Online (Sandbox Code Playgroud)

那行得通,它给了我所有的返回语句。但是现在,我想获得所有返回值而忽略 lambda 表达式中的值。

我怎样才能做到这一点?

是否有任何财产表明这一点?

c# return roslyn

0
推荐指数
1
解决办法
223
查看次数

当前上下文中不存在“InitializeComponent”

我正在使用 Roslyn 解析一个非常基本的 WPF 解决方案。我设置了诊断并发现以下错误:

MainWindow.xaml.cs(25,13):错误CS0103:当前上下文中不存在名称“InitializeComponent”

错误 CS5001:程序不包含适合入口点的静态“Main”方法

知道如何解决这个问题吗?

更新

所以我添加了一个 main 方法,现在我收到以下错误:

App.xaml.cs(20,17):错误CS1061:“App”不包含“InitializeComponent”的定义,并且找不到接受“App”类型的第一个参数的扩展方法“InitializeComponent”(您是否缺少使用指令还是程序集引用?)

MainWindow.xaml.cs(25,13):错误CS0103:当前上下文中不存在名称“InitializeComponent”

这是主要方法

public partial class App : Application
{
    [STAThread]
    public static void Main()
    {
        var app = new App();
        app.InitializeComponent();
        app.Run();
    }
}
Run Code Online (Sandbox Code Playgroud)

c# roslyn

0
推荐指数
1
解决办法
2748
查看次数

标签 统计

c# ×4

roslyn ×4

return ×1

scripting ×1