none
Qualify classes for COM interop: Interface implementation RRS feed

  • Question

  • http://msdn.microsoft.com/en-us/library/7fcfby2t%28v=VS.80%29.aspx

    The above MSDN library page says that "Classes should implement interfaces explicitly." It refers to class interfaces which seem to be used if the class doesn't "explicitly implement" the interface.

    Now what does that mean? Should the class derive from an interface at all or should it "explicitly implement" (in terms of Visual Studio) the interface?

    Example interface:

    [Guid("...")]
    [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
    public interface IExample
    {
        public void Test();
    }

    Class with implicit interface implementation:

    public class ExampleClass : IExample
    {
        public void Test() { };
    }

    Class with explicit interface implementation:

    public class ExampleClass : IExample
    {
        void IExample.Test() { };
    }

    Tuesday, April 6, 2010 2:11 PM

Answers

  • That documentation sentence isn't very clear. What it's trying to say is that you should create that COM interface ("IExample" in your sample code) and implement it implicitly in your "ExampleClass". The "explicitly" in "Classes should implement interfaces explicitly" is an advice against using the ability of not having an "IExample" interface at all!

    You could just use the ClassInterface attribute with the AutoDual value and have .NET generate the "IExample" interface (it would be generated with the name "_Example") for you, which would allow your "Example" class to not implement any "IExample" interface.

    I'm not sure I made myself clear. I think this is best described here: http://msdn.microsoft.com/en-us/library/4fcadw4a(v=VS.80).aspx


    -- Blog: http://geeklyeverafter.blogspot.com/
    • Marked as answer by LonelyPixel Tuesday, April 6, 2010 2:53 PM
    Tuesday, April 6, 2010 2:31 PM

All replies

  • That documentation sentence isn't very clear. What it's trying to say is that you should create that COM interface ("IExample" in your sample code) and implement it implicitly in your "ExampleClass". The "explicitly" in "Classes should implement interfaces explicitly" is an advice against using the ability of not having an "IExample" interface at all!

    You could just use the ClassInterface attribute with the AutoDual value and have .NET generate the "IExample" interface (it would be generated with the name "_Example") for you, which would allow your "Example" class to not implement any "IExample" interface.

    I'm not sure I made myself clear. I think this is best described here: http://msdn.microsoft.com/en-us/library/4fcadw4a(v=VS.80).aspx


    -- Blog: http://geeklyeverafter.blogspot.com/
    • Marked as answer by LonelyPixel Tuesday, April 6, 2010 2:53 PM
    Tuesday, April 6, 2010 2:31 PM
  • Thank you, that's what I wanted to hear. :-)

    Both examples of classes (implicitly?) implementing an interface on that page still do it wrong, they forgot the "public" modifier...

    But now I can create a base class that implements all the interfaces it ought to with virtual methods, and I can then derive from that class only overriding the methods I need to customise. Whereas explicit interface methods cannot be virtual, my compiler says.

    Tuesday, April 6, 2010 2:56 PM