Hi,
I have a pretty basic issue when I am trying expose a C# interface for
COM interoperability. Here is the interface definition:
[ComVisible(true)]
public interface IClass:IClassBase
{
int IClassMethod();
}
and the super interface looks like:
public interface IClassBase
{
string IClassBaseMethod();
}
The implementing class:
[ComVisible(true)]
public class Class1: IClass
{
public int IClassMethod()
{
return 6;
}
public string IClassBaseMethod()
{
return "6String";
}
}
I build the assembly and then I add as reference using the the tlb file
for this assembly in Excel VBA. There I am trying to write code as:
Dim Iclass As Iclass
Set Iclass = New class1
MsgBox Iclass.IClassMethod 'this call is ok
Msgbox Iclass.IClassBaseMethod 'This fails.
The second call to IClassBaseMethod fails saying the method not
available on interface. I expected .NET framework to handle exposing of
super interface methods automatically. What is wrong with the above
code. How can force the base interface methods to be exposed.
Thanks in advance for taking your time to answer the question.
Robert Jordan - 07 Oct 2005 02:39 GMT
Hi,
> I build the assembly and then I add as reference using the the tlb file
> for this assembly in Excel VBA. There I am trying to write code as:
[quoted text clipped - 8 lines]
> super interface methods automatically. What is wrong with the above
> code. How can force the base interface methods to be exposed.
Both interfaces are exposed. This is the default behavior.
Did you use some other interop attributes you omited before
posting the code? Like
[ClassInterface(ClassInterfaceType.None)] on your class?
Rob
manohar.shankar@gmail.com - 07 Oct 2005 03:07 GMT
No,
I just checked my code and made sure there is no
[ClassInterface(ClassInterfaceType.None)] on the class "Class1", I can
only define it on the class and not on interface anyway.
I tried that approach for the class class1 but even then the IClass has
only the method exposed from within itself and not the one from base
interface. Please try building this code into a simple assembly
project, you should see the same behaviour. Btw I forgot to mention the
version of .net in original post.
Its Visual Studio.NET 2005 beta 2 (.net framework 2.0 beta).
thanks for the quick response.
Robert Jordan - 07 Oct 2005 03:20 GMT
> I just checked my code and made sure there is no
> [ClassInterface(ClassInterfaceType.None)] on the class "Class1", I can
[quoted text clipped - 7 lines]
>
> Its Visual Studio.NET 2005 beta 2 (.net framework 2.0 beta).
Sorry, I don't have access to this version right now.
With 1.1 it's working.
Rob
Zdenek Drlik - 07 Oct 2005 11:18 GMT
> Hi,
>
[quoted text clipped - 43 lines]
>
> Thanks in advance for taking your time to answer the question.
It is the way how tlbexp.exe works, the same as in framework 1.1.
The most straightforward approach is to explicitly re-list the inherited
members of each base interface in the derived type:
public interface IClassBase
{
string IClassBaseMethod()
}
public interface IClass : IClassBase {
int IClassMethod();
new string IClassBaseMethod();
}
public class Class1 : IClass {
// ...
}
and it should work correctly.
Zdenek D.
manohar.shankar@gmail.com - 07 Oct 2005 19:32 GMT
I thought about this approach but it becomes tedious if lot of
interfaces derive from base interface and beats the purpose of having
inheritance right?
Anyway if thats the only way we can do it then I will do it that way.
thanks for the response