3

I'm working with some Interop code and need to instantiate an interface.

The equivalent in C# is:

Interop.SomeInterface theInterface;
theInterface = new Interop.SomeInterface();
theInterface.SomeEvent += new SomeEventHandler(...);

How can I accomplish instantiating this interface in F#? Do I need to re-wrap this interop dll into something that F# can make sense of?

ildjarn
  • 62,044
  • 9
  • 127
  • 211
Dave
  • 2,386
  • 1
  • 20
  • 38

1 Answers1

8

It is impossible to instantiate an interface. The C# compiler is pulling a trick by instantiating a class by looking up the implementer of the interface. This is done by the compiler examining the CoClassAttribute.

As far as I know, F# knows nothing about the CoClassAttribute, so you need to change your code so that it instantiates an actual class, not an interface.

You could figure out the class you need by looking at the CoClassAttribute on SomeInterface with Reflector, ILdasm, or any other metadata inspector. If the interop wrapper was generated using tlbimp or visual studio, usually the class will be named SomeInterfaceClass by convention.

Community
  • 1
  • 1
vcsjones
  • 138,677
  • 31
  • 291
  • 286