C# - Call a derived base method in the derived context

143 Views Asked by At

Tried to search the web but found nothing so far so here my question:

I want to index model-information via attributes on the different members. To do this i created a function in a base class that gathers all needed information when called. This method is derived into the different models so that they can all be indexed.

Base()
{
    public virtual void Index() {...}
}

In the base class I'm calling a generic method that gives me acces to the indexing server for the specific model that I want to save there

using (var indexFacade = IndexFacadeFactory.GetIndexFacade(configuration, this))
{
    indexFacade.Execute(this, operation);
}

The issue I'm currently having is that when calling the factory it retrieves the information for the base-class.

What I want to acomplish is something like this:

Derived : Base
{
   [IndexingKey]
   long Id { get; set; }

   [IndexingField]
   string SomeValue { get; set; }
}

var derived = new Derived();
derived.Index();

My indexFacade holds the type of

IndexFacadeBase<Base>

I'm aware of the polimorphism here and why this happens.

My question is: How can i call

derived.Index();

so that the context from which it is called is not from the base-class without overwriting it?

Further information:

The method that is called looks like this:

public static IndexFacadeBase<T> GetIndexFacade<T>(IndexInfo.IndexConfiguration config, T model)
        {
            IndexFacadeBase<T> retVal;
            .....
            return retVal;
        }

The T has the type of Base.

The model has the type of Derived.

Maybe that clears up some of the Problems.

I get back:

  IndexFacadeBase<Base>

I would need back:

   IndexFacadeBase<Derived>

Thanks in advance for all the help.

2

There are 2 best solutions below

1
On

I may not be fully understanding your question, but aren't you simply looking to override the method in the derived class?

class Base
{
    public virtual void Index() { }
}

class Derived : Base
{
    public override void Index() { } // here is the override.
    long Id { get; set; }
    string SomeValue { get; set; }
}

Then when you do this:

var derived = new Derived();
derived.Index();

The derived class' Index method is called.

2
On

Maybe it would work if your IndexFacadeBase<Base> was changed to IndexFacadeBase<T> where T:Base.