Can I have two classes implement the same interface in COM?
My classes are as follows
[ComVisible(True)][GUID(...)]
public interface IMyComInterface
{
void DoThis(string[] params);
}
[ClassInterface(ClassInterfaceType.None)]
[GUID(...)]
public class MyComImplementation: MyNetClass,IMyComInterface
{
public void DoThis(string[] params)
{
var netParams=params.ToList();
base.DoThis(netParams);
}
}
This works.
Now I want to add a second ComClass that inherits MyNetBaseClass but uses a filter:
[ClassInterface(ClassInterface.Type.None)
[GUID(...)]
public class MySecondComImplementation:MyNetClass,IMyComInterface
{
public void DoThis(string[] params)
{
var netParams=params.Where(param=>param.Contains("x"));
开发者_如何学Python base.DoThis(netParams.ToList());
}
}
Does this work, or do I have to define a second MyComInterface with an identical DoThis
method? Or in other words: must every class that implements a COM-Visible interface have its own, unique COM-visible interface?
The example you show, is perfectly legal. The 2 classes will each have their own GUID. By registering the assembly, the necessary entries will be written in the registry.
In your VB application, you'll be able to do this. (Sorry, there can be syntax errors in the code below, I don't have enough VB6 knowledge:) ).
Dim a As IMyComInterface
Dim b As IMyComInterface
Set a = New MyComImplementation()
Set b = New MySecondComImplementation()
It is perfectly legal to have multiple objects implementing the same interface. But you register the COM dll by its GUID. When you want to instantiate a COM object, Windows calls the DllGetClassObject
entry point function, passing it the desired class id.
So this function acts like a factory for the COM object. If you write your own version of this function, you can choose which object to create and return.
I must admit I don't know enough about VB6 to know if it is possible to export your own version of DllGetClassObject
to do this.
精彩评论