I have a class that is a map in a field in a database. The class only cares about the name of the field and its sister type .NET. The type can be a string, int, datetime, etc.
class Foo()
{
string Name { get; set; }
Type FooType { get; set; }
}
I have another class that inherits from Foo, which adds a property for the value. Right now, I am storing the value as an object and use the switch statement to enter the value based on the FooType base classes.
class FooWithStuff() : Foo
{
object Value { get; set; }
}
Is there a way to implement this using generics to ensure type safety for values?
Edit: I made the key requirement bold. When declaring a list of Foo, it needs a type. If I did this against user classes, I would create an interface and use this. However, here I use int, string, DateTime, etc. Int is a structure, a string is an object, so Foo <object> does not work for both.
source
share