.NET Reflection: How to call an interface method without instantiating?

I have a situation where I have to call an interface method using reflection, like this

object x = null; MethodInfo method = interfaceExists.GetMethod("ShutDown"); method.Invoke(x, new object[] { 4 }) 

As you can see, I am not creating an instance of the object! And, as I can assume, I get an exception

The non-static method requires a targeted

And the question is, can I call the interface method using reflection without creating an instance of the interface, and if YES, how can I do this?

+7
reflection c #
source share
6 answers

If you are absolutely sure that the interface method will not affect the state of the object (and this is generally a very bad assumption), you can create an instance without calling the constructor by calling FormatterServices.GetUnitializedObject . Personally, I would highly recommend against this, as any number of bad things can happen when you call an interface method on an uninitialized type.

+12
source share

If this is an instance method, you need an instance with which to call the method. Therefore, the "instance" method.

Instance methods may depend on instance variables that reflection does not know about, therefore it cannot guarantee that the instance method does not change the state of an instance of the type in which it is defined.

This is why you will receive FxCop warnings (to paraphrase here) "Make this method static, because it will not change the state of the class."

+10
source share

If the method is non-static, you need to instantiate the object in order to use it. Since interfaces cannot have static methods, you need to instantiate an object with an interface on it to execute the method.

+4
source share

Otherwise, you will need to call the non-stationary interface method, which will be implemented on the object. If the implementation of the method does not exist, then no real method can be called.

+2
source share

An interface has no implementation, so you cannot call its method without an instance of an object that implements this interface.

+1
source share

Is it possible to call an interface method without instantiating? Not. Interfaces are, for example, members; static class members are not associated with interfaces.

You may be able to get what you want by providing a static implementation of the interface,

 public class MyImplementation : IMyInterface { public static readonly Instance = new MyImplementation(); private MyImplementation() { } } // ...then your code might look like: MethodInfo method = typeof(IMyInterface).GetMethod("ShutDown"); method.Invoke(MyImplementation.Instance, new object[] { 4 }) 

Or you can make an extension method:

 public static class MyExtensions { public static void ShutDown(this IMyInterface obj, ...) { ... } } // ...then your code might look like: object x = null; MethodInfo method = typeof(MyExtensions).GetMethod("ShutDown"); method.Invoke(x as IMyInterface, new object[] { 4 }); 
+1
source share

All Articles