我正在编写一个应用程序,该应用程序需要开发用于处理数据的引擎,但该引擎必须可以根据客户的需要替换为另一个引擎。由于每个客户都有非常不同的需求,我想让每个引擎与其他引擎分开,这样我们就可以只交付带有客户所需引擎的应用程序。
所以我的 VS 解决方案有以下项目: App, Engine_Base, Engine_A, Engine_B App = exe Engine_Base = 父类...编译成 dll 并通过项目属性 Engine_A 和 Engine_B 添加到 App 的引用中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
我意识到这与继承有关,但我对如何解决这个问题缺乏了解。
有没有人对如何解决这个问题有想法?
我们的方法是一个好的解决方案还是我们应该一直在寻找其他东西并以不同的方式做事?