为什么新任务&LT;对象&gt;((Func键&LT;任务&LT;对象&gt;&GT;)(异步()=&GT; {})当新任务&LT工作;美孚&GT;(......)不? [英] Why does New Task<object>( (Func<Task<object>>)( async ( ) => { } ) work when new Task<Foo>( ... ) does not?
问题描述
为什么新任务&LT;对象&gt;((Func键&LT;任务&LT;对象&gt;&GT;)(异步()=&GT; {})
工作的时候新任务&LT;富&GT;?(...)
不
本作品:
私有静态任务&LT;对象&gt;作品(){
返回新任务&LT;对象&gt;((Func键&LT;任务&LT;对象&gt;&GT;)(异步()=&GT; {
返回新的对象();
}),新CancellationTokenSource().Token);
}
但是,这并不:
私有静态任务&LT;富&GT;犯规(){
返回新任务&LT;富&GT;((Func键&LT;任务&LT;富&GT;&GT;)(异步()=&GT; {
返回新的Foo();
}),新CancellationTokenSource().Token);
}
替换的富的任何东西不是一个对象(或任何其他原始我猜),你会看到你得到同样的错误消息在红色squigglies:的最好重载方法匹配'System.Threading.Task.Task&LT;富&GT;。任务(目标,System.Threading.Tasks.TaskCreationOptions)'有一些无效参数
我甚至看到了重载方法,它是指......,但为什么它认为我想,当我使用调用该方法 Func键&LT;任务&LT;富&GT;&GT;
,但的不的当我使用 Func键&LT;任务&LT;对象&gt;&GT;
?????
这适用对象(或对象),但我的同班同学失败。我甚至与其他类对象(窗口)和失败。
试了一下修改
在响应的第一位不编译我已经能够编译如下主张:
使用系统;
使用System.Collections.Generic;
使用System.Linq的;
使用System.Text;
使用的System.Threading;
使用System.Threading.Tasks;命名空间AsyncFlubbery {
类节目{ 私有静态任务&LT;对象&gt;作品(){
返回新任务&LT;对象&gt;((Func键&LT;任务&LT;对象&gt;&GT;)(异步()=&GT; {
返回新的对象();
}),新CancellationTokenSource().Token);
} 静态无效的主要(字串[] args){
作品()。开始();
Console.WriteLine(preSS ENTER开始......);
到Console.ReadLine();
}
}
}
这是类型转换的一个简单的例子。
它归结为code:
Func键&LT;任务&LT;对象&gt;&GT; FTO = NULL;
FUNC&LT;对象&gt; FO = FTO; //这是一个很好的投FUNC&LT;任务&LT;富&GT;&GT; FTF = NULL;
FUNC&LT;富&GT; FF = FTF; //这是**不**罚带投
由于任务&LT;对象&gt;
从对象
第一投就OK派生
但任务&LT;富&GT;
做的不可以从富
得出这样的演员阵容无效的。
所以,即使第一个code编译为在运行时预计将无法正常工作。 code的两个位都少不了故障。
Why does new Task<object>( (Func<Task<object>>)( async ( ) => { } )
work when new Task<Foo>( ... )
does not?
This works:
private static Task<object> Works( ) {
return new Task<object>( ( Func<Task<object>> )( async ( ) => {
return new object( );
} ), new CancellationTokenSource( ).Token );
}
But this doesn't:
private static Task<Foo> Doesnt( ) {
return new Task<Foo>( (Func<Task<Foo>>)(async ( ) =>{
return new Foo( );
} ), new CancellationTokenSource( ).Token );
}
Replace Foo with anything that is not an object (or any other primitive I'm guessing) and you will see that you get the same error message over the red squigglies : The best overloaded method match for 'System.Threading.Task.Task<Foo>.Task(object, System.Threading.Tasks.TaskCreationOptions)' has some invalid arguments
I even see the overloaded method to which it is referring... and yet why does it think that I am trying to call THAT method when I use Func<Task<Foo>>
but NOT when I use Func<Task<object>>
?????
This works with object (or Object) but FAILS with my class. I even tried it with another class object (Window) and THAT failed.
EDIT
In response to claims that the first bit does not compile I have been able to compile the following :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace AsyncFlubbery {
class Program {
private static Task<object> Works( ) {
return new Task<object>( ( Func<Task<object>> )( async ( ) => {
return new object( );
} ), new CancellationTokenSource( ).Token );
}
static void Main( string[ ] args ) {
Works( ).Start( );
Console.WriteLine( "Press Enter To Begin..." );
Console.ReadLine( );
}
}
}
This is a simple case of type casting.
It boils down to this code:
Func<Task<object>> fto = null;
Func<object> fo = fto; //this is a fine cast
Func<Task<Foo>> ftf = null;
Func<Foo> ff = ftf; //this is **NOT** a fine cast
Since Task<object>
derives from object
the first cast is OK.
But Task<Foo>
does not derive from Foo
so the cast is invalid.
So, even though the first code compiles it won't work as expected at run-time. Both bits of code are ultimately faulty.
这篇关于为什么新任务&LT;对象&gt;((Func键&LT;任务&LT;对象&gt;&GT;)(异步()=&GT; {})当新任务&LT工作;美孚&GT;(......)不?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!