我正在编写一个需要开发用于处理数据的引擎的应用程序,但是根据客户的需要,引擎必须可以由另一个引擎替换。由于每个客户都有非常不同的需求,我想让每个引擎与其他客户分开,因此我们只能使用客户所需的引擎交付应用程序。
所以我的VS解决方案有以下项目:App,Engine_Base,Engine_A,Engine_B App = exe Engine_Base =父类...编译为dll,并通过projec属性添加到App的引用中 Engine_A和Engine_B都是Engine_Base的子类,并且都编译为自己的dll(Engine_A.dll,Engine_B.dll)。它们不会添加到App的引用中,因此它们不会在运行时加载,因为我们不希望将它们都发送给所有客户。
根据客户的配置文件,我们决定加载哪个引擎:
Engine_Base^ engine_for_app;
Assembly^ SampleAssembly;
Type^ engineType;
if (this->M_ENGINE == "A")
{
SampleAssembly = Assembly::LoadFrom("path\\Engine_A.dll");
engineType = SampleAssembly->GetType("Engine_A");
engine_for_app = static_cast<Engine_Base^>(Activator::CreateInstance(engineType, param1, param2));
}
else
{
SampleAssembly = Assembly::LoadFrom("path\\Engine_B.dll");
engineType = SampleAssembly->GetType("Engine_B");
engine_for_app = static_cast<Engine_Base^>(Activator::CreateInstance(engineType, param1, param2, param3, param4));
}
由于只将Engine_Base添加到C ++项目的引用中,我们将Engine__A或Engine_B对象转换为父类型。
然后我们为引擎的线程执行设置事件,因为它们需要很长时间才能运行(要处理大量数据):
engine_for_app->OnComplete += gcnew CompleteEngineProcess(this, &frmMain::ThreadChildComplete);
engine_for_app->OnProgressInit += gcnew ProgressInitEngine(this, &frmMain::ThreadChildProgressInit);
engine_for_app->OnProgressReport += gcnew ProgressReportEngine(this, &frmMain::ThreadChildProgressReport);
Thread^ aThread;
aThread = gcnew Thread(gcnew ThreadStart(engine_for_app, &Engine_Base::Read));
但这给了我一个:
Error 2 error C3754: delegate constructor: member function 'Engine_A::Read' cannot be called on an instance of type 'Engine_Base ^' d:\_activeWork\EDI_TRUNK\src\App\frmMain.cpp 492
我意识到这与继承有关,但我对如何解决这个问题的想法很缺乏。
有没有人有关于如何解决这个问题的想法?
我们的方法是一个正确的解决方案,还是我们应该一直在寻找别的东西并以不同的方式做事?
答案 0 :(得分:0)
编辑:也许确保Engine_A等方法被标记为虚拟?