mercredi 8 février 2017

Trying to invoke method on child properties

In my C# application I will at times handle objects that inherit from my class BaseDomainModel which has a method public virtual void ValidateModel(). For my example, I'm getting an instance of a CompanyUser object which has at least one property of a type inheriting from BaseDomainModel.

I would like to walk through these properties and invoke the ValidateModel() method on those properties.

Here is what I have so far:

var validatableProperties = testCompanyUser.GetType().GetProperties().Where(p => p.PropertyType.BaseType == typeof(BaseDomainModel));
foreach (var thisProperty in validatableProperties)
{
    var m = thisProperty.PropertyType.GetMethod("ValidateModel", Type.EmptyTypes);
    m.Invoke(thisProperty.GetValue(testCompanyUser), null);
}

I'm currently getting this message:

Non-static method requires a target.

I'm sure I'm missing something simple. What did I do wrong?





Aucun commentaire:

Enregistrer un commentaire