I have a User
class (there are also several subclasses) that is used throughout a large system. In very certain situations, I need to attach a few extra properties to the class. For several reasons, however, I do not want them at other times.
Creating a subclass does not seem possible since I would not be able to downcast my objects to this derived type. I also can't use a copy constructor to construct these subtype objects, as I don't know if my object has inherited properties from some subclass.
A simplified example of what I need:
class User
{
public string Firstname { get; set; }
}
A property like:
public string FirstLetterOfFirstName { get { return Firstname.Substring(0, 1); } }
How would you give the objects of type User
this kind of property?
I tried deep cloning but I still only get a User object that I still cannot cast to my type with the property. Is this even possible in any way?
I don't want to use methods (extension methods included) as I elsewhere use these properties to extract relevant data from the object.