简体   繁体   English

在 C# 中,为什么我不能从其接口签名将基接口类型作为其返回类型的 function 返回派生接口类型?

[英]In C#, why can't I return a derived interface type from a function whose interface signature has the base interface type as its return type?

I'm receiving the compile error: Error CS0535 'COITemplateWriter' does not implement interface member 'iTemplateWriter.Write(iTemplateModel)' TemplateService For the following code:我收到编译错误:错误 CS0535 'COITemplateWriter' 没有实现接口成员 'iTemplateWriter.Write(iTemplateModel)' TemplateService 对于以下代码:

    public COITemplate Write(COITemplateModel model)
    {
        throw new NotImplementedException();
    }

The interface signature for the above method is as follows:上述方法的接口签名如下:

public interface iTemplateWriter
{
    public iTemplate Write(iTemplateModel model);
}

Type COITemplate implements the iTemplate interface and type COITemplateModel implements the iTemplateModel interface, so why does this code fail? COITemplate 类型实现了 iTemplate 接口,COITemplateModel 类型实现了 iTemplateModel 接口,那么为什么这段代码会失败呢? Wouldn't it stand to reason that if the interface requires the method to return anything that implements iTemplate and takes as a parameter anything that implements iTemplateModel that this should compile?如果接口要求方法返回任何实现 iTemplate 的东西并将任何实现 iTemplateModel 的东西作为参数,这难道不合理吗?

You are confusing what you are allowed to do during run-time with what the compiler needs to know at compile-time.您将在运行时允许执行的操作与编译器在编译时需要知道的操作混淆了。

This is what generics are for in C#这就是 generics 在 C# 中的用途

It looks like you want an interface iTemplateWriter where you want to be able to have this method Write that will take in any iTemplateModel and return any iTemplate看起来您想要一个接口iTemplateWriter ,您希望能够在其中拥有此方法Write将接受任何iTemplateModel并返回任何iTemplate

You can do this two ways:您可以通过以下两种方式执行此操作:

Method 1- You can define at the interface level what type of iTemplateModel and what type of iTemplate the implementer of the interface will use:方法 1- 您可以在接口级别定义什么类型的iTemplateModel以及接口的实现者将使用什么类型的iTemplate

    public interface iTemplateWriter<TTemplate, TTemplateModel> 
        where TTemplate : iTemplate
        where TTemplateModel : iTemplateModel
    {
         TTemplate Write(TTemplateModel model);
    }

doing this will allow you to define a COITemplateWritter that uses a COITemplate and a COITemplateModel :这样做将允许您定义一个使用COITemplateWritterCOITemplateCOITemplateModel

public class COITemplateWritter : iTemplateWriter<COITemplate, COITemplateModel>
{
    public COITemplate Write(COITemplateModel model)
    {
        throw new System.NotImplementedException();
    }
}

Use method 1 if you know at compile time what class types the implementing class needs to use如果您在编译时知道 class 类型,则使用方法 1 实现 class 需要使用

If you need your implementing class to handle all types at run time and you do not know the type at compile-time, then use method 2:如果您需要实现 class 在运行时处理所有类型,并且您在编译时不知道类型,则使用方法 2:

Method 2- You can define this at the method level of what type of iTemplate and what type of iTemplateModel the method will use.方法 2- 您可以在方法级别定义该方法将使用什么类型的iTemplate和什么类型的iTemplateModel This will require all implementing classes to be able to return whatever type is passed in at runtime, which allows for more flexibility but is less structured at compile time.这将要求所有实现类能够返回在运行时传入的任何类型,这允许更大的灵活性但在编译时结构较少。

public interface iTemplateWriter
{
    TTemplate Write<TTemplate, TTemplateModel>(TTemplateModel model)
        where TTemplate : iTemplate
        where TTemplateModel : iTemplateModel;
}

public class COITemplateWritter : iTemplateWriter
{
    public TTemplate Write<TTemplate, TTemplateModel>(TTemplateModel model)
        where TTemplate : iTemplate
        where TTemplateModel : iTemplateModel
    {
        
        throw new System.NotImplementedException();
    }
}

Without seeing the rest of what COITemplate is, assuming it is not an interface, you should just need to have the function return the interface type itself.没有看到 COITemplate 的 rest 是什么,假设它不是接口,您应该只需要让 function 返回接口类型本身。

public iTemplateWriter Write(COITemplateModel model)
{
   // if the "COITemplateModel" class is an iTemplateWriter you could just return that back.
   return model;

   // OR if some other object you are working with is that interface.
   return (iTemplateWrite)OfWhateverClassYouAreTrying;

}

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

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