C# 泛型抽象类

C# 泛型抽象类,c#,generics,C#,Generics,我有以下代码,这是很好的 namespace GenericAbstract { public interface INotifModel { string Data { get; set; } } public interface INotif<T> where T: INotifModel { T Model { get; set; } } public interface INotifPr

我有以下代码,这是很好的

namespace GenericAbstract
{
    public interface INotifModel
    {
        string Data { get; set; }
    }

    public interface INotif<T> where T: INotifModel
    {
       T Model { get; set; }
    }

    public interface INotifProcessor<in T> where T : INotif<INotifModel>
    {
        void Yell(T notif);
    }

    public class HelloWorldModel : INotifModel
    {
        public string Data { get; set; }

        public HelloWorldModel()
        {
            Data = "Hello world!";
        }
    }

    public class HelloWorldNotif : INotif<HelloWorldModel>
    {
        public HelloWorldModel Model { get; set; }

        public HelloWorldNotif()
        {
            Model = new HelloWorldModel();
        }
    }

    public class HelloWorldProcessor<T> : INotifProcessor<T> where T : INotif<INotifModel>
    {
        public void Yell(T notif)
        {
            throw new NotImplementedException();
        }
    }
}
命名空间通用摘要
{
公共接口模型
{
字符串数据{get;set;}
}
公共接口INotif,其中T:INotifModel
{
T模型{get;set;}
}
公共接口INotif处理器,其中T:INotif
{
无效喊叫(T notif);
}
公共类HelloWorldModel:INotifModel
{
公共字符串数据{get;set;}
公共HelloWorldModel()
{
Data=“你好,世界!”;
}
}
公共类HelloWorldNotif:INotif
{
公共HelloWorldModel模型{get;set;}
公共HelloWorldNotif()
{
Model=新的HelloWorldModel();
}
}
公共类HelloWorldProcessor:INotifProcessor,其中T:INotif
{
公共无效呼叫(T notif)
{
抛出新的NotImplementedException();
}
}
}
如您所见,有3个接口,每个接口都已实现

但是,我希望处理器的实现方式如下:

public class HelloWorldProcessor : INotifProcessor<HelloWorldNotif<HelloWorldModel>>
{
    public void Yell(HelloWorldNotif<HelloWorldModel> notif)
    {
        throw new NotImplementedException();
    }
}
公共类HelloWorldProcessor:INotifProcessor
{
公共无效呼叫(HelloWorldNotif notif)
{
抛出新的NotImplementedException();
}
}
但我得到了以下错误:

非泛型类型“HelloWorldNotif”不能与类型参数一起使用

我希望HelloWorldProcessor仅为
HelloWorldNotif
实现
InotifyProcessor


无法找出我做错了什么。

正如其他人所说和/或暗示的那样,如果没有完全指定,您已经得到了HelloWorld。因此,要翻译这个:

我希望HelloWorldProcessor仅为 HelloWorldNotif

对C#,我想你的意思是:

public class HelloWorldProcessor : INotifProcessor<HelloWorldNotif>
{
    public void Yell(HelloWorldNotif notif)
    {
        throw new NotImplementedException();
    }
}
公共类HelloWorldProcessor:INotifProcessor
{
公共无效呼叫(HelloWorldNotif notif)
{
抛出新的NotImplementedException();
}
}

要使其起作用,您首先必须制作
INotif
co变体。这意味着接口的
Model
属性必须是只读的(在一个实现中,它仍然可以有一个公共
set
)。然后,为了修复立即出现的错误,不要将
放在
HelloWorldNotif
之后,因为它已经是
INotif


不太清楚你在问什么
HelloWorldNotif
已经实现了
INotif
(并且不是泛型),那么为什么您希望它采用另一种泛型类型的
HelloWorldModel
?错误消息很清楚
HelloWorldNotif
不是泛型,所以不能使用
HelloWorldNotif
@DavidG我希望HelloWorldProcessor只为HelloWorldNotif实现INotifProcessor…是的,你已经说过了,但是就像我(和艾米)说的,
HelloWorldNotif
不是泛型。@DavidG,我知道它不是泛型。但如何创建一个实现INotifProcessor和“yells”HelloworldNotif的处理器呢“?虽然这很可能是OP的目的,并且它处理即时错误,但这也不会编译,因为
HelloWOrldNotif
不是
INotif
,它是
INotif
,并且
INotif
都是协同变量,这样才能工作。”。
public interface INotifModel
{
    string Data { get; set; }
}

public interface INotif<out T> where T : INotifModel
{
    T Model { get; }
}

public interface INotifProcessor<in T> where T : INotif<INotifModel>
{
    void Yell(T notif);
}

public class HelloWorldModel : INotifModel
{
    public string Data { get; set; }

    public HelloWorldModel()
    {
        Data = "Hello world!";
    }
}

public class HelloWorldNotif : INotif<HelloWorldModel>
{
    public HelloWorldModel Model { get; set; }

    public HelloWorldNotif()
    {
        Model = new HelloWorldModel();
    }
}

public class HelloWorldProcessor<T> : INotifProcessor<T> where T : INotif<INotifModel>
{
    public void Yell(T notif)
    {
        throw new NotImplementedException();
    }
}

public class HelloWorldProcessor : INotifProcessor<HelloWorldNotif>
{
    public void Yell(HelloWorldNotif notif)
    {
        throw new NotImplementedException();
    }
}
Console.WriteLine(notif.Model.Data);