C # Define override method in different assemblies

I have a class library that I created, which consists of several classes that inherit from one base class.

There is a virtual method defined in the base class Process, which will essentially process the contents of the class structure in the application.

There is an exchange of messages between the various components (servers) of the application, and they send and receive messages such as the base class, and then call Process ()

Where am I stuck in that I need to override the Process () method in other parts of the application, that is, in different assemblies, without deriving a different type from it.

From what I read, this seems impossible, so looking if using interfaces solves the problem?

EDIT:

What I did not mention in the question is that each derived class of the base class must have a different implementation of Process () depending on the functionality required by this class.

For example:

class First : BaseClass
{
  override void Process()
  {
    //do something
  }
}

class Second : BaseClass
{
  override void Process()
  {
    //do something else
  }
}

the code in the methods of the redefinition process must be in different assemblies due to the fact that this code will refer to other external sources based on the class to which it belongs, I do not want to be distributed throughout the application.

For clarification only, I have access and, if necessary, you can change the library class.

I am doing some extra reading, and it would seem that using delegates can solve this problem .. thoughts?

+4
source share
1 answer

, , .

public interface IProcessStrategy
{
    void Process();
}

public class ProcessService
{
    public void DoProcess( IProcessStrategy strategy )
    {
        ...whatever
        strategy.Process();
        ...whatever else
    }
}

, , .

: , , , , , , , , - .

public interface IProcessStrategy
{
    void Process();
}

public class BaseClass
{
    private IProcessStrategy _strategy;

    public void Process()
    {
        // this is where the real "override" happens
        if ( _strategy == null )
        {
            // default implementation
            ...
        }
        else
            // overridden
            _strategy.Process();
    }

    public void OverrideWith( IProcessStrategy strategy )
    {
        this._strategy = strategy;
    }

:

 BaseClass f = new BaseClass();
 f.Process(); // uses the default implementation

-

 BaseClass f = new BaseClass();
 IProcessStrategy p = new First();

 // override
 f.OverrideWith( p );
 f.Process(); // overridden with a new implementation

, First , , .

+6

Source: https://habr.com/ru/post/1606358/


All Articles