为什么新任务<对象>((Func键<任务<对象>>)(异步()=> {})当新任务&LT工作;美孚>(......)不? [英] Why does New Task<object>( (Func<Task<object>>)( async ( ) => { } ) work when new Task<Foo>( ... ) does not?

查看:154
本文介绍了为什么新任务<对象>((Func键<任务<对象>>)(异步()=> {})当新任务&LT工作;美孚>(......)不?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么新任务<对象>((Func键<任务<对象>>)(异步()=> {})工作的时候新任务<富>?(...)

本作品:

 私有静态任务<对象>作品(){
    返回新任务<对象>((Func键<任务<对象>>)(异步()=> {
        返回新的对象();
    }),新CancellationTokenSource().Token);
}

但是,这并不:

 私有静态任务<富>犯规(){
    返回新任务<富>((Func键<任务<富>>)(异步()=> {
        返回新的Foo();
    }),新CancellationTokenSource().Token);
}

替换的的任何东西不是一个对象(或任何其他原始我猜),你会看到你得到同样的错误消息在红色squigglies:的最好重载方法匹配'System.Threading.Task.Task<富>。任务(目标,System.Threading.Tasks.TaskCreationOptions)'有一些无效参数

我甚至看到了重载方法,它是指......,但为什么它认为我想,当我使用调用该方法 Func键<任务<富>> ,但当我使用 Func键<任务<对象>> ?????

这适用对象(或对象),但我的同班同学失败。我甚至与其他类对象(窗口)和失败。

试了一下

修改

在响应的第一位不编译我已经能够编译如下主张:

 使用系统;
使用System.Collections.Generic;
使用System.Linq的;
使用System.Text;
使用的System.Threading;
使用System.Threading.Tasks;命名空间AsyncFlubbery {
    类节目{        私有静态任务<对象>作品(){
            返回新任务<对象>((Func键<任务<对象>>)(异步()=> {
                返回新的对象();
            }),新CancellationTokenSource().Token);
        }        静态无效的主要(字串[] args){
            作品()。开始();
            Console.WriteLine(preSS ENTER开始......);
            到Console.ReadLine();
        }
    }
}


解决方案

这是类型转换的一个简单的例子。

它归结为code:

  Func键<任务<对象>> FTO = NULL;
FUNC<对象> FO = FTO; //这是一个很好的投FUNC<任务<富>> FTF = NULL;
FUNC<富> FF = FTF; //这是**不**罚带投

由于任务<对象> 对象第一投就OK派生

任务<富> 做的不可以得出这样的演员阵容无效的。

所以,即使第一个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屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆