c# - Classes with the same interface but different types for a property -
i have kind of design :
public interface idifferenttypes { } public class integertype : idifferenttypes { public int value { get; set; } } public class stringtype : idifferenttypes { public string value { get; set; } } public class datetimetype : idifferenttypes { public datetime value { get; set; } }
but property 'value' defined in interface.
so can call :
idifferenttypes someint = getsomeint(); // getsomeint() returns integertype object assert.areequal(5, someint.value); idifferenttypes somestring = getsomestring(); // getsomestring() returns stringtype object assert.areequal("ok", somestring.value);
problem type of value different each implementation, best way deal that?
you define generic interface (but have property, or, more strictly, can't field):
public interface ihasvalue<t> { t value { get; } }
where t
type, placeholder, if will, , can do:
public class hasstringvalue : ihasvalue<string> { public string value { get; private set; } }
Comments
Post a Comment