mercredi 18 novembre 2020

How to invoke base-class method of injected object?

(I couldn't come up with a succinct title for this question that didn't drag on.)

I have a base class that has one public method:

public class MyBaseClass
{
    public void MyBaseMethod()
    {
        // Here is where the functionality will go, that is described below.
    }
}

I have another class, derived from my base class, that contains injected objects that also derive from MyBaseClass:

public class MyWorkingClass : MyBaseClass
{
    // All of these objects also derive from MyBaseClass.
    private readonly IClassOne _classOne;
    private readonly IClassTwo _classTwo;
    private readonly IClassThree _classThree;

    public MyWorkingClass(
        IClassOne classOne,
        IClassTwo classTwo,
        IClassThree classThree)
    {
        _classOne = classOne;
        _classTwo = classTwo;
        _classThree = classThree;
    }

    public SomeMethod()
    {
        // Stuff goes here.

        // Call base method here.
        MyBaseMethod();
    }
}

What I want to happen is that the call to MyBaseMethod will collect all of the fields declared in the class. For each field, get its instance and call its own MyBaseMethod.

I can get all of the fields in a class instance:

var fields = GetType()
    .GetFields(BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.DeclaredOnly)
    .ToList();

For each field in the list, I can get its field type, instance and its own declared methods:

foreach (var field in fields)
{
    var fieldType = field.FieldType;

    var fieldInstance = field
        .GetValue(this);

    var methods = fieldType
        .GetMethods();
}

My problem is that my base-class method, MyBaseMethod, is not included in the list of methods. I have also tried GetRuntimeMethods(), but it is not included there either.

If I could get access to the base-class method, then I could invoke it:

var methodInfo = fieldType
    .GetMethod('MyBaseMethod');

methodInfo
    .Invoke(fieldInstance, null);

Question: How can I manage to invoke my base-class method in each field instance, given that its type is listed as being an interface?

Thanks.





Aucun commentaire:

Enregistrer un commentaire