简体   繁体   English

C# - 在派生的上下文中调用派生的基本方法

[英]C# - Call a derived base method in the derived context

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 我的indexFacade保持类型

IndexFacadeBase<Base>

I'm aware of the polimorphism here and why this happens. 我知道这里的polimorphism和为什么会发生这种情况。

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 . T具有Base的类型。

The model has the type of Derived . 模型具有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. 在此先感谢所有的帮助。

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. 派生类的Index方法被调用。

如果您的IndexFacadeBase<Base>更改为IndexFacadeBase<T> where T:Base它可能会起作用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM