c# - Generic Abstract Class -
i have following code fine...
namespace genericabstract { public interface inotifmodel { string data { get; set; } } public interface inotif<t> t: inotifmodel { t model { get; set; } } public interface inotifprocessor<in t> 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> t : inotif<inotifmodel> { public void yell(t notif) { throw new notimplementedexception(); } } } as can see there 3 interfaces , each of implemented.
however, processor implemented this:
public class helloworldprocessor : inotifprocessor<helloworldnotif<helloworldmodel>> { public void yell(helloworldnotif<helloworldmodel> notif) { throw new notimplementedexception(); } } but following error:
the non-generic type 'helloworldnotif' cannot used type arguments
i want helloworldprocessor implement inotifprocessor helloworldnotif...
can't figure out doing wrong..
for work first have make inotif<t> co-variant. means model property has read interface (it can still have public set in implementation). fix immediate error don't put <helloworldmodel> after helloworldnotif because it's inotif<helloworldmodel>
public interface inotifmodel { string data { get; set; } } public interface inotif<out t> t : inotifmodel { t model { get; } } public interface inotifprocessor<in t> 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> 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 guess implementation
console.writeline(notif.model.data);
Comments
Post a Comment