为什么不能在立即窗口中计算 lambda 表达式?

发布于 2024-09-05 12:49:53 字数 271 浏览 2 评论 0原文

有什么特别的原因吗?是根本不可能还是只是还没有实施?也许有任何第三方插件允许 lambda 评估?

更新:

我在 codeplex 扩展立即窗口上找到了这个项目。似乎它已经被放弃了一段时间,但这可以作为一个概念的证明。有人知道任何其他即时窗口扩展插件吗?例如,可以在 C# 中运行 for/foreach 语句吗?

Is there any particular reason? Is it not possible at all or is it just not implemented yet? Maybe there are any third-party addins that allow lambda evaluations?

UPDATE:

I've found this project on codeplex Extended Immediate Window. Seems that it has been abandoned for some time, but this can be a proof of a concept. Does anybody know any other immediate window extension addins? The ones that can run for/foreach statements in C# for instance?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

贩梦商人 2024-09-12 12:49:54

Microsoft 的 JaredPar 撰写了几篇博客文章来回答您的问题:第 1 部分第 2 部分。你会在那里找到答案。

JaredPar of Microsoft wrote a couple of blog posts answering your question: part 1 and part 2. You'll find the answers there.

表情可笑 2024-09-12 12:49:54

编写 lambda 时,捕获变量的行为显着改变了底层代码的构造(将变量移动到编译器生成的类的字段中,这些类本身很容易被链式闭包上下文)。

即使不考虑这样做的一般复杂性,它也会有两种选择:

  • 将所有变量值捕获为常量;可行且非常简单,但很容易意味着在立即窗口中执行的结果与在主体中执行的结果非常不同(非常不可取)
  • 重写整个代码(出于概述的原因上面)即时(猜测,不可能)

考虑到“不受欢迎”和“不可能”之间的选择,我猜他们只是选择不实现本质上脆弱的功能,而且写起来非常复杂。

When writing a lambda, the act of capturing variables significantly alters the construction of the underlying code (moving variables into fields of compiler-generated classes, that could very easily themselves be chained closure-contexts).

Not even considering the general complexity of doing this, it would then have two choices:

  • capture all the variable values as constants; feasible and pretty simple, but could easily mean that the result of executing in the immediate window is very different to the result of executing in the main body (very undesirable)
  • rewrite the entire code (for the reasons outlined above) on the fly (at a guess, impossible)

Given a choice between "undesirable" and "impossible", I guess they simply chose not to implement a feature that would be inherently brittle, and very complex to write.

人│生佛魔见 2024-09-12 12:49:54

嗯,我认为这是因为立即窗口只能计算表达式,或者更确切地说,它只能执行调用和赋值。要计算 Lambda 表达式,必须为该 Lambda 创建一个闭包,进行类型检查,然后执行。

我认为归根结底,立即窗口只是一个评估器,而不是一个解释器。

http://msdn.microsoft.com/en-us /library/f177hahy(VS.80).aspx

“立即窗口在设计时用于调试和计算表达式、执行语句、打印变量值等。它允许您输入要计算的表达式或在调试期间由开发语言执行。”

所以实际上,你的问题归结为为什么你不能在立即窗口中定义函数(因为 lambda 只是匿名函数),而我认为的答案是它根本不是为此设计的。

Well, I think it's because the immediate window can only evaluate expressions, or rather it can only do invocations and assignments. To evaluate a Lambda expression a closure would have to be created for that lambda, typchecked and then executed.

I think it comes down to that the Immediate window is just an evaluator and not an interpreter.

http://msdn.microsoft.com/en-us/library/f177hahy(VS.80).aspx

"The Immediate window is used at design time to debug and evaluate expressions, execute statements, print variable values, and so forth. It allows you to enter expressions to be evaluated or executed by the development language during debugging."

So in effect, your question boils down to why you can't define functions in the immediate window (since lambdas are just annonymous functions), and the answer I think is that it simply wasn't designed for that.

站稳脚跟 2024-09-12 12:49:54

如果您仍然需要使用 Visual Studio 2013,您实际上可以使用包管理器控制台窗口在立即窗口中编写循环或 lambda 表达式。就我而言,我在函数顶部添加了一个列表:

    private void RemoveRoleHierarchy()
    {
#if DEBUG
        var departments = _unitOfWork.DepartmentRepository.GetAll().ToList();
        var roleHierarchies = _unitOfWork.RoleHierarchyRepository.GetAll().ToList();
#endif

        try
        {
            //RoleHierarchy
            foreach (SchoolBo.RoleHierarchy item in _listSoRoleHierarchy.Where(r => r.BusinessKeyMatched == false))
                _unitOfWork.RoleHierarchyRepository.Remove(item.Id);

            _unitOfWork.Save();
        }
        catch (Exception e)
        {
            Debug.WriteLine(e.ToString());
            throw;
        }
    }

我的 GetAll() 函数在哪里:

    private DbSet<T> _dbSet;

    public virtual IList<T> GetAll()
    {
        List<T> list;
        IQueryable<T> dbQuery = _dbSet;
        list = dbQuery
            .ToList<T>();

        return list;
    }

在这里我不断收到以下错误,所以我想打印出各个存储库中的所有项目:

InnerException  {"The DELETE statement conflicted with the REFERENCE constraint \"FK_dbo.Department_dbo.RoleHierarchy_OranizationalRoleId\". The conflict occurred in database \"CC_Portal_SchoolObjectModel\", table \"dbo.Department\", column 'OranizationalRoleId'.\r\nThe statement has been terminated."} System.Exception {System.Data.SqlClient.SqlException}

然后,我发现通过在立即窗口中执行以下命令,部门存储库中有多少记录:

_unitOfWork.DepartmentRepository.GetAll().ToList().Count

返回 243。

因此,如果您在包管理器控制台中执行以下命令,它将打印出所有项目:

PM> for($i = 0; $i -lt 243; $i++) { $a = $dte.Debugger.GetExpression("departments[$i].OrgagnizationalRoleId"); Write-Host $a.Value $i }

该想法的作者可以在这里找到: http://ogresoft.blogspot.ca /2013/06/how-to-write-loop-or-lambda-expression.html

If you still need to use Visual Studio 2013, you can actually write a loop, or lambda expression in the immediate window using also the package manager console window. In my case, I added a list at the top of the function:

    private void RemoveRoleHierarchy()
    {
#if DEBUG
        var departments = _unitOfWork.DepartmentRepository.GetAll().ToList();
        var roleHierarchies = _unitOfWork.RoleHierarchyRepository.GetAll().ToList();
#endif

        try
        {
            //RoleHierarchy
            foreach (SchoolBo.RoleHierarchy item in _listSoRoleHierarchy.Where(r => r.BusinessKeyMatched == false))
                _unitOfWork.RoleHierarchyRepository.Remove(item.Id);

            _unitOfWork.Save();
        }
        catch (Exception e)
        {
            Debug.WriteLine(e.ToString());
            throw;
        }
    }

Where my GetAll() function is:

    private DbSet<T> _dbSet;

    public virtual IList<T> GetAll()
    {
        List<T> list;
        IQueryable<T> dbQuery = _dbSet;
        list = dbQuery
            .ToList<T>();

        return list;
    }

Here I kept getting the following error, so I wanted to print out all the items in the various repositories:

InnerException  {"The DELETE statement conflicted with the REFERENCE constraint \"FK_dbo.Department_dbo.RoleHierarchy_OranizationalRoleId\". The conflict occurred in database \"CC_Portal_SchoolObjectModel\", table \"dbo.Department\", column 'OranizationalRoleId'.\r\nThe statement has been terminated."} System.Exception {System.Data.SqlClient.SqlException}

Then, I find out how many records are in the department repository by executing this in the immediate window:

_unitOfWork.DepartmentRepository.GetAll().ToList().Count

Which returned 243.

So, if you execute the following in the package manager console, it prints out all the items:

PM> for($i = 0; $i -lt 243; $i++) { $a = $dte.Debugger.GetExpression("departments[$i].OrgagnizationalRoleId"); Write-Host $a.Value $i }

The author for the idea can be found here: http://ogresoft.blogspot.ca/2013/06/how-to-write-loop-or-lambda-expression.html

A君 2024-09-12 12:49:54

我假设,因为它是惰性求值,所以直接窗口无法事先知道捕获的变量(闭包)应该具有什么值。

I assume, that because it is lazy evaluation, the immediate window cannot know beforehand what values, the captured variables (closure), should have.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文