我不知道你的代码有多干净,所以这只是一个大概的思路(伪代码),但以下是我会采取的方法:
在一个共享库中,向.NET和COM公开每个表单的接口:
public interface ILoginForm
property UserName as String
property Password as String
function DisplayModal as Boolean 'True for login, false for cancel...or expose an enum
end interface
public interface IContactEditor
property FirstName as String
property LastName as String
property EmailAddress as String
function DisplayModal as Boolean 'True for save, false for cancel...or expose an enum
end interface
等等,对于应用程序中的每个表单都要做类似的事情。
接下来,定义一个FormFactory接口:
public interface IFormFactory
function CreateLoginForm as ILoginForm
function CreateContactEditorForm as IContactEditor
end interface
如果您想要缓存表单,那么您可以稍微改变界面以适应该用例。
接下来,在您的VB6 EXE中,您应该实现IFormFactory接口:
Class FormFactory Implements IFormFactory
public function IFormFactory_CreateLoginForm as ILoginForm
'let's say this form is still in VB6
Dim frm As frmLoginPage
Set frm = new frmLoginPage
Set IFormFactory_CreateLoginForm = frm
end function
public function IFormFactory_CreateContactEditorForm as IContactEditor
'let's say this form is in .NET
Dim frm As DotNetLib.ContactEditorDialog
Set frm = new DotNetLib.ContactEditorDialog
Set IFormFactory_CreateContactEditorForm = frm
end function
在您的VB6应用程序中,所有表单创建都必须通过此单例进行:
Dim contactEditor as IContactEditor
Set contactEditor = modSingletons.FormFactory.CreateContactEditorForm()
contactEditor.FirstName = "Joe" 'seed with initial values
contactEditor.LastName = "Blow"
contactEditor.EmailAddress = "bubblegum@something.net"
Dim saved As Boolean
saved = contactEditor.DisplayModal()
if saved then
'read the new values back out and write to DB or whatever
end if
如果您正确地执行此操作,您的主EXE甚至不会意识到表单是.NET还是VB6,您只需在工厂中切换它们即可。
最后,在.NET库中设置相同的内容。创建一个COM公开的单例,VB6 exe可以将IFormFactory实例传递到.NET库中。然后,您的.NET代码可以使用工厂实例来调用应用程序中的任何表单。
或者,您可以在每次调用表单时传递工厂实例(以允许该表单访问任何其他表单),但我不会这样做。原因是因为除了表单创建之外,很可能还有更多的服务需要开始迁移。为您的各种应用程序服务设置一堆接口,并以类似的方式将它们注入到.NET库中,这样您将获得更好的服务。最终所有内容都将在.NET中,但您的代码不需要更改,因为它正在使用接口。