C# 在不同程序集中定义重写方法
C# Define override method in different assembly
我有一个我构建的 class 库,它包含许多 class 继承自单个基础 class 的库。
在基础 class 上定义了一个称为 Process 的虚拟方法,它基本上会将 class 结构的内容处理到应用程序中。
应用程序的不同组件(服务器)之间有消息传递,它们正在传递和接收基本 class 类型的消息,然后调用 Process()
我被卡住的地方是我需要能够覆盖应用程序其他部分中的 Process() 方法,即在不同的程序集中而不从中派生另一种类型。
根据我的阅读,这似乎是不可能的,所以看看使用接口是否可以解决问题?
编辑:
我在问题中没有提到的是基础 class 的每个派生 class 需要有不同的 Process() 实现,具体取决于 [=33] 所需的功能=].
例如:
class First : BaseClass
{
override void Process()
{
//do something
}
}
class Second : BaseClass
{
override void Process()
{
//do something else
}
}
重写过程方法中的代码需要在不同的程序集中,因为该代码将根据我不想传播的 class 引用其他外部源在整个应用程序中。
澄清一下,我确实可以访问并且可以根据需要更改库 class。
我一直在做一些进一步的阅读,使用委托似乎可以解决这个问题。有什么想法吗?
接口,特别是 策略模式 可以解决您的问题。
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
可以在任何地方定义,包括不同的程序集,它只需要实现公共接口。
我有一个我构建的 class 库,它包含许多 class 继承自单个基础 class 的库。
在基础 class 上定义了一个称为 Process 的虚拟方法,它基本上会将 class 结构的内容处理到应用程序中。
应用程序的不同组件(服务器)之间有消息传递,它们正在传递和接收基本 class 类型的消息,然后调用 Process()
我被卡住的地方是我需要能够覆盖应用程序其他部分中的 Process() 方法,即在不同的程序集中而不从中派生另一种类型。
根据我的阅读,这似乎是不可能的,所以看看使用接口是否可以解决问题?
编辑:
我在问题中没有提到的是基础 class 的每个派生 class 需要有不同的 Process() 实现,具体取决于 [=33] 所需的功能=].
例如:
class First : BaseClass
{
override void Process()
{
//do something
}
}
class Second : BaseClass
{
override void Process()
{
//do something else
}
}
重写过程方法中的代码需要在不同的程序集中,因为该代码将根据我不想传播的 class 引用其他外部源在整个应用程序中。
澄清一下,我确实可以访问并且可以根据需要更改库 class。
我一直在做一些进一步的阅读,使用委托似乎可以解决这个问题。有什么想法吗?
接口,特别是 策略模式 可以解决您的问题。
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
可以在任何地方定义,包括不同的程序集,它只需要实现公共接口。