Classes with the same interface but different types for a property
.net, c#, interface, types
Solution
You could define a generic interface (but it will have to be a property, or, more strictly, it can't be a field):
public interface IHasValue<T> {
T Value { get; }
}
Where `T` is the type, a placeholder, if you will, and you can do:
public class HasStringValue : IHasValue<string> {
public string Value { get; private set; }
}
Problem
I would like to have this 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 with the property 'value' defined in the interface. So I can call something like that : ``` IDifferentTypes someInt = GetSomeInt(); // GetSomeInt() returns a IntegerType object Assert.AreEqual(5, someInt.value); IDifferentTypes someString = GetSomeString(); // GetSomeString() returns a StringType object Assert.AreEqual("ok", someString.value); ``` Problem is that the type of value is different for each implementation, what is the best way to deal with that?