为什么 RelayCommand.Execute 采用对象而不是 T?

发布于 2024-11-14 20:54:18 字数 987 浏览 4 评论 0原文

这没有做任何事情,只是导致需要进行不必要的转换(或者更确切地说,导致我拉下代码库并自己进行更改)。这样做有理由吗?

参考资料:

Codeplex 上的来源

博客发布来源

编辑 这是一个例子:

DoCommand = new RelayCommand<AsyncCallback>((callBack) =>
{
    Console.WriteLine("In the Action<AsyncCallback>");
    SomeAsyncFunction((async_result) =>
    {
        Console.WriteLine("In the AsyncCallback");
        callBack.Invoke(new MyAsyncResult(true));
    });
});

DoCommand.Execute((iasyncresult) => Console.WriteLine(iasyncresult.IsCompleted));
//Where MyAsyncResult is a class implement IAsyncResult that sets IsCompleted in the constructor
// This will cause the "cannot cast lambda as object" error

This hasn't done anything but cause the need for what would otherwise be unnecessary casting (or rather, caused me to pull down the codebase and make the change myself). Is there a reason for doing this?

References:

Source on Codeplex

Blog posting with source

Edit
Here's an example:

DoCommand = new RelayCommand<AsyncCallback>((callBack) =>
{
    Console.WriteLine("In the Action<AsyncCallback>");
    SomeAsyncFunction((async_result) =>
    {
        Console.WriteLine("In the AsyncCallback");
        callBack.Invoke(new MyAsyncResult(true));
    });
});

DoCommand.Execute((iasyncresult) => Console.WriteLine(iasyncresult.IsCompleted));
//Where MyAsyncResult is a class implement IAsyncResult that sets IsCompleted in the constructor
// This will cause the "cannot cast lambda as object" error

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

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

发布评论

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

评论(2

べ映画 2024-11-21 20:54:18

因为 ICommand 不是通用的。 ICommand 的通用实现必须从接口进行强制转换、处理无效强制转换并将强制转换实例转发给通用方法。

Because ICommand is not generic. A generic implementation of ICommand must cast from the interface, handle invalid casts, and forward the cast instance to the generic methods.

李白 2024-11-21 20:54:18

您的错误是由于 lambda 无法作为对象传递。相反尝试:

AsyncCallback callback = (iasyncresult) => Console.WriteLine(iasyncresult.IsCompleted);
DoCommand.Execute(callback);

Your error is due to the lambda not being able to be passed as an object. Instead try:

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