Lambda 表达式创建不相关的编译错误

发布于 2024-09-28 06:22:13 字数 1580 浏览 1 评论 0原文

每当我将 lambda 表达式(采用以下形式)添加到我的 wpf 项目时,都会收到错误。这些错误与表达式无关,但每次我添加一个错误都会出现。

这是我的最新消息:

using ( LeisureServiceClient client = ServiceFactory.Instance.GetLeisureService() )
{
    client.Execute( ServiceFactory.Instance.ConnectionDetails, new MoveBasketItemsToAccountCommand()
    {
        BasketItemIDs = bisList.ToList().ConvertAll<Guid>( bis => bis.ID )
    } );
}

这对我来说似乎完全有效,这给出了以下编译错误,突出显示 client.Execute(...) 中的 client

Error 43: The type 'System.Windows.DependencyObject' is defined in an assembly that is not referenced. You must add a reference to assembly 'System.Windows, Version=2.0.5.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e'.

这段代码与 DependancyObject 无关。无论如何,.cs 文件中引用了 System.Windows,该文件还包含:

public class PointOfSaleViewModel : DependencyObject

当 lambda 表达式被删除时,它很容易编译。

现在,为了增加混乱......这很好:

ServiceFactory.Instance.ShiftDataRefreshedEvent += ( s, e ) =>
{
    Account = new ObservableCollection<BasketItemSummary>( ServiceFactory.Instance.CurrentContact.Account );
    Basket = new ObservableCollection<BasketItemSummary>( ServiceFactory.Instance.Shift.OpenCurrentContact.Basket );
};

所以,并不是 lambda 表达式本身导致了错误,我不知道为什么这不能编译,并且非常渴望在我的脑海中获得一些输入爆炸。

更新

同事建议的替代语法

BasketItemIDs = bisList.ToList().ConvertAll( delegate( BasketItemSummary basketItem ) { return basketItem.ID; } )

也失败,给出相同的编译错误。

Whenever I add a lambda expression (in the following form) to my wpf project, I get an error. The errors are nothing to do with the expression, but they arrive every time I add one.

here is my latest:

using ( LeisureServiceClient client = ServiceFactory.Instance.GetLeisureService() )
{
    client.Execute( ServiceFactory.Instance.ConnectionDetails, new MoveBasketItemsToAccountCommand()
    {
        BasketItemIDs = bisList.ToList().ConvertAll<Guid>( bis => bis.ID )
    } );
}

This seems perfectly valid to me, this gives the following compile error, highlighting client from client.Execute(...).

Error 43: The type 'System.Windows.DependencyObject' is defined in an assembly that is not referenced. You must add a reference to assembly 'System.Windows, Version=2.0.5.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e'.

this code is nothing to do with a DependancyObject. Regardless, System.Windows is referenced in the .cs file, which also contains:

public class PointOfSaleViewModel : DependencyObject

which is quite happy to compile when the lambda expression is removed.

now, to add confusion... this is fine:

ServiceFactory.Instance.ShiftDataRefreshedEvent += ( s, e ) =>
{
    Account = new ObservableCollection<BasketItemSummary>( ServiceFactory.Instance.CurrentContact.Account );
    Basket = new ObservableCollection<BasketItemSummary>( ServiceFactory.Instance.Shift.OpenCurrentContact.Basket );
};

so, it's not the lambda expression itself that's causing the error, I'm out of ideas as to why this isn't compiling, and pretty keen to get some input before my head explodes.

Update

the alternate syntax suggested by a colleague

BasketItemIDs = bisList.ToList().ConvertAll( delegate( BasketItemSummary basketItem ) { return basketItem.ID; } )

also fails, giving the same compilation error.

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

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

发布评论

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

评论(3

复古式 2024-10-05 06:22:13

在我看来,BasketItemSummary(或其中一个属性)公开了对公共 API 的这种依赖性 - 也许它是该类型的基类。简单地说:按照指示添加缺少的参考。

It sounds to me like BasketItemSummary (or one of the properties) exposes this dependency on the public API - perhaps it is a base-class for the type. Simply: add the missing reference as it instructs.

夜血缘 2024-10-05 06:22:13

我知道这个问题已经很老了,但是万一其他人遇到这个问题:

最近,我遇到了一个与您非常相似的问题。我正在工作的解决方案包括一个 Web 项目,该项目引用了 wpf 项目(用于生成供下载的文档)。在wpf项目中进行一些修改后,web项目将不再编译,并出现以下错误消息:

Error   4   The type 'System.Windows.DependencyObject' is defined in an assembly that is not referenced. You must add a reference to assembly 'WindowsBase, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'.

经过一番调试,我发现错误消息仅指向涉及LINQ操作的行(首先我也认为它与lambdas有关) ,就像你一样)。

最后,问题的根源是我从 http://www.scottlogic.co.uk/blog/colin/2010/03/linq-to-visual-tree/。我从 LinqToVisualTree 中获取了方法并将它们放在“System.Linq”命名空间下。似乎这与我的 Web 项目中的原始 Linq 扩展方法有某种冲突,一旦我将 LinqToVisualTree 方法的命名空间更改为其他名称,问题就消失了。

I know this question is old, but in case someone else faces this problem:

Recently, I faced a problem much like yours. The solution I'm working includes a web project which has reference to a wpf project (for generating documents for downloading). After some modifications in the wpf project, the web project would not compile anymore, with the following error message:

Error   4   The type 'System.Windows.DependencyObject' is defined in an assembly that is not referenced. You must add a reference to assembly 'WindowsBase, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'.

After some debugging, I figured out the error message only pointed to lines which involved LINQ operations (first I also thought it was related to lambdas, as you).

In the end, the root of the problem were some wpf's visual tree helper methods I had taken from http://www.scottlogic.co.uk/blog/colin/2010/03/linq-to-visual-tree/. I had taken the methods from LinqToVisualTree and put them under the "System.Linq" namespace. Seemingly that was somehow conflicting with the original Linq extension methods in my web project, and once I changed the namespace of the LinqToVisualTree methods to something else the problem went away.

风为裳 2024-10-05 06:22:13

您需要在项目中添加对 dll 的引用:右键单击项目,选择“添加引用”。

You need to add a reference to the dll in your project: right click on project, choose add reference.

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