Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get properties from derived class in base class

Tags:

c#

oop

reflection

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...

like image 962
Marek M. Avatar asked Apr 05 '13 12:04

Marek M.


People also ask

Can base class access derived class properties?

No, you cannot access any derived class members using base class pointer even pointing to a derived class instance. However you can access those values though methods of derived class. Make sure you declare the methods as virtual in your base class.

Can a base class reference to derived class?

A derived class is a class which takes some properties from its base class. It is true that a pointer of one class can point to other class, but classes must be a base and derived class, then it is possible.

What is the connection between the base class and the derived class?

The class whose members are inherited is called the base class, and the class that inherits those members is called the derived class. A derived class can have only one direct base class. However, inheritance is transitive.

What happens if the base class and derived class?

Base class object will call base class functionclass functionIn mathematics, especially in the fields of group theory and representation theory of groups, a class function is a function on a group G that is constant on the conjugacy classes of G. In other words, it is invariant under the conjugation map on G.https://en.wikipedia.org › wiki › Class_functionClass function - Wikipedia and derived class object will call derived class function.


1 Answers

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.

like image 63
pascalhein Avatar answered Sep 19 '22 14:09

pascalhein