c# - Can I use generics to change a subclass' method signature? -
i need perform operation on subclass , return result:
public interface ientity { } public abstract class entity : ientity { public abstract ientity dostuff(ientity e1, ientity e2, ientity e3); } public class customer : entity { public override ientity dostuff(ientity e1, ientity e2, ientity e3) { // <-- yuck var customer1 = e1 customer; // <-- yuck var customer2 = e2 customer; // <-- yuck var customer3 = e3 customer; // <-- yuck // stuff return foo; } }
i want avoid subclass' method signature, , associated typecasting.
so prefer this:
public override customer dostuff(customer c1, customer c2, customer c3) { // stuff return foo; }
how use type system accomplish that?
edit: i changed name of method compare
dostuff
, reiterate operation irrelevant. i'm not trying compare. want change types. how do that?
many have said change design, cannot do. have solution, i've included below, , works. if have better way accomplish this, without criticising design (which not point), please let me know. everyone's contribution though.
this works. self-referencing generics make head hurt.
public interface ientity { } public abstract class entity<tentity> : ientity tentity : ientity { public abstract tentity dostuff(tentity e1, tentity e2, tentity e3); } public class customer : entity<customer> { public override customer dostuff(customer c1, customer c2, customer c3) { // stuff return foo; } }
is solution, or there gotchas in there that'll give me problems?
is there better solution?
Comments
Post a Comment