Get properties from derived class in base class

c#, oop, reflection

Solution

If both classes are in the same assembly, you can try this:

Assembly
    .GetAssembly(typeof(BaseClass))
    .GetTypes()
    .Where(t => t.IsSubclassOf(typeof(BaseClass))
    .SelectMany(t => t.GetProperties());

This will give you all the properties of all the subclasses of `BaseClass`.

Problem

How do I get properties from derived class in base class? Base class: ``` public abstract class BaseModel { protected static readonly Dictionary<string, Func<BaseModel, object>> _propertyGetters = typeof(BaseModel).GetProperties().Where(p => _getValidations(p).Length != 0).ToDictionary(p => p.Name, p => _getValueGetter(p)); } ``` Derived classes: ``` public class ServerItem : BaseModel, IDataErrorInfo { [Required(ErrorMessage = "Field name is required.")] public string Name { get; set; } } public class OtherServerItem : BaseModel, IDataErrorInfo { [Required(ErrorMessage = "Field name is required.")] public string OtherName { get; set; } [Required(ErrorMessage = "Field SomethingThatIsOnlyHereis required.")] public string SomethingThatIsOnlyHere{ get; set; } } ``` In this example - can I get the "Name" property from ServerItem class while in BaseModel class? EDIT: I'm trying to implement model validation, as described here: http://weblogs.asp.net/marianor/archive/2009/04/17/wpf-validation-with-attributes-and-idataerrorinfo-interface-in-mvvm.aspx I figured that if I create some base model with (almost) all of the validation magic in it, and then extend that model, it will be okay...

Original source