Implementing IUnknown in C#

Matthew Layton picture Matthew Layton · May 23, 2013 · Viewed 7.2k times · Source

I've been looking for an example of how to implement IUnknown in C#, but haven't found any decent references or solutions to this.

Should it be as simple as...

public interface IUnknown
{
    UInt32 AddRef();
    UInt32 QueryInterface([In] IntPtr riid, [Out] IntPtr ppvObject);
    UInt32 Release();
}

...or is there more to it?

Answer

Sebastian Redl picture Sebastian Redl · May 23, 2013

Why would you want to implement IUnknown? That's a COM interface. If you want to interoperate with COM, use the .Net/COM bridge, which implements IUnknown for you.