为什么 RelayCommand.Execute 采用对象而不是 T?
这没有做任何事情,只是导致需要进行不必要的转换(或者更确切地说,导致我拉下代码库并自己进行更改)。这样做有理由吗?
参考资料:
编辑 这是一个例子:
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:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
因为 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.
您的错误是由于 lambda 无法作为
对象
传递。相反尝试:Your error is due to the lambda not being able to be passed as an
object
. Instead try: