Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I invoke a static method on a .NET object over COM interop?

Is it possible to invoke a static method on a .NET Object, via COM interop?

I know I could write a wrapper class. What if I don't wanna do that?

like image 894
Cheeso Avatar asked Sep 08 '09 19:09

Cheeso


People also ask

Can we invoke a static method on an object?

In the same way that a static variable exists before an object of the class is instantiated, a static method can be called before instantiating an object. So when a method doesn't need to access any stored values, a static method is appropriate.

Can we access static method using object C#?

In C#, static means something which cannot be instantiated. You cannot create an object of a static class and cannot access static members using an object.

Can you invoke a static method from an instance?

A static method in Java (also called class method) is a method that belongs to the class and not the instance. Therefore, you can invoke the method through the class instead of creating an instance first and calling the method on that instance.

Can you call a static method from an instance C#?

In C# it is not legal to access a static method or member variable through an instance, and trying to do so will generate a compiler error (C++ programmers, take note). Some languages distinguish between class methods and other (global) methods that are available outside the context of any class.


1 Answers

No you cannot do this. COM interop communicates via objects, not types.

Work arounds I know of ...

  • The best work around is to create a wrapper method on an instance to do the call on the type. Yes this still requires an instance so it defeats the purpose but it's you're best option.
  • Reverse PInvoke: Still requires you to pass a function pointer down to the C++ layer
like image 107
JaredPar Avatar answered Oct 18 '22 17:10

JaredPar