I have a class like this:
public class ItemField
{
public string FieldName { get; set; }
public object FieldValue
{
get;
set;
}
}
I want to have it so that “FieldValue” can only be of type string, MyItem, or List<MyItem>. I also want it so that when code consumes this class, accessing “FieldValue” returns the right type of object. Is there a way to do this? I’m not too clear on generics, but there seems to be a way to do this using that…
A property can only return one specific type, you can’t make it return different data types depending on what the actual type of the field value is.
You can use generics for the class, but then you need to create the class instance for the type of data that you want to store in it: