[英]Generic Abstract Class
I have the following code which is fine... 我有以下代码很好...
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();
}
}
}
As you can see there are 3 interfaces and each of those is implemented. 如您所见,有3个接口,每个接口都已实现。
However, I would like the processor to be implemented like this: 但是,我希望这样实现处理器:
public class HelloWorldProcessor : INotifProcessor<HelloWorldNotif<HelloWorldModel>>
{
public void Yell(HelloWorldNotif<HelloWorldModel> notif)
{
throw new NotImplementedException();
}
}
But i get the following error: 但我得到以下错误:
The non-generic type 'HelloWorldNotif' cannot be used with type arguments
非泛型类型“ HelloWorldNotif”不能与类型参数一起使用
I want the HelloWorldProcessor to implement INotifProcessor
only for HelloWorldNotif
... 我希望HelloWorldProcessor仅为
HelloWorldNotif
实现INotifProcessor
...
Can't figure out what I am doing wrong.. 无法弄清楚我在做什么错..
As others have said and/or implied out you've already got HelloWorldNotif fully specified. 正如其他人所说和/或暗示的那样,您已经完全指定了HelloWorldNotif。 So to translate this:
因此,将其翻译为:
I want the HelloWorldProcessor to implement INotifProcessor only for HelloWorldNotif
我希望HelloWorldProcessor仅为HelloWorldNotif实现INotifProcessor
To C#, I think you mean: 对于C#,我想你的意思是:
public class HelloWorldProcessor : INotifProcessor<HelloWorldNotif>
{
public void Yell(HelloWorldNotif notif)
{
throw new NotImplementedException();
}
}
For this to work you first have to make INotif<T>
co-variant. 为此,您首先必须使
INotif<T>
协变量。 That means that the Model
property has to be read only for the interface (it can still have a public set
in an implementation). 这意味着
Model
属性仅对于接口是只读的(在实现中它仍然可以具有公共set
)。 Then to fix your immediate error you don't put the <HelloWorldModel>
after HelloWorldNotif
because it's already a INotif<HelloWorldModel>
然后立即解决的错误,你不要把
<HelloWorldModel>
后HelloWorldNotif
,因为它已经是一个INotif<HelloWorldModel>
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();
}
}
Then I guess your implementation would be something like 然后我想你的实现将是这样的
Console.WriteLine(notif.Model.Data);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.