简体   繁体   English

扩展Task类时,必须在非通用静态类中定义扩展方法

[英]Extension method must be defined in a non-generic static class while extending Task class

I'm trying to extend Task class, but I get two different errors depending how I extend it: first one: 我正在尝试扩展Task类,但是根据如何扩展它,我遇到了两个不同的错误:第一个:

public class ExtTask : Task
{
    public static void DoSomenthing(this Task task)
    {
        //some code
    }
}

Extension method must be defined in a non-generic static class 扩展方法必须在非通用静态类中定义

so I add static: 所以我添加静态:

public static class ExtTask : Task
{
    public static void DoSomenthing(this Task task)
    {
        //some code
    }
}

getting error: 出现错误:

Static class 'DownloadFile.ExtTask' cannot derive from type 'System.Threading.Tasks.Task'. 静态类“ DownloadFile.ExtTask”不能从类型“ System.Threading.Tasks.Task”派生。 Static classes must derive from object. 静态类必须从对象派生。

how can I solve this problem? 我怎么解决这个问题?

You don't need to derive from task if you are trying to add an extension method, change it to 如果尝试添加扩展方法,则无需从任务派生,将其更改为

public static class ExtTask
{
    public static void DoSomenthing(this Task task)
    {
        //some code
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM