views:

278

answers:

6

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

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

As you can see I do not create instance of object! And, as I can supposed, I receive exception

Non-static method requires a target

And Question, Can I call method of interface using reflection without creating instance of interface and if YES, How I can do it ?

+9  A: 

If it is an instance method, you need an instance with which to call the method. Hence "instance" method.

Instance methods can have dependencies on instance variables, which reflection would not know about, so it cannot guarantee that an instance method does not alter the state of the instance of the type in which it is defined.

That's why you'll get those FxCop warnings about (paraphrasing here) "Make this method static as it does not alter the state of the class".

Will
How Can I make method of interface static ?
jitm
You can't have static methods on an interface. You'll either need a static method on a class, if you don't want the class to be used mark it as abstract, and implementing the interface, then derive you're subclasses from it.
Ian
@jitm you cannot. If you **really really** need to do this, consider ditching your interface for a base class. Of course, you cannot override base class static methods, so you may still be screwed. Try asking another question on SO (don't use this one) that details what you are trying to do, and ask if there is a better way.
Will
Yes, I understand this, therefore I asked this question, Thanks a lot for all answers.
jitm
+4  A: 

If the method is non-static, you have to create an object instance to use it. Since interfaces can't have static methods, you need to create an instance of an object with the interface on it to execute the method.

Kevin
+1  A: 

An interface has no implementation so you cant call its method without an instance of a object which implements that interface.

bassfriend
+1  A: 

Your non-static interface method will eventually need to call a method that is implemented on an object. If the implementation of the method does not exist, then no real method can be called.

Robin
+1  A: 

Can you call an interface method without creating an instance? No. Interfaces are for instance members; static class members aren't related to interfaces.

You might 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 could 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 });
Joshua Tacoma
Thanks, I know it.
jitm
+3  A: 

If you're absolutely sure the interface method won't impact object state (and that's generally a very bad assumption), you could create an instance without calling the constructor by calling FormatterServices.GetUnitializedObject. Personally, I would strongly recommend against this, as any number of bad things could happen when you call an interface method on an uninitialized type.

Dan Bryant
+1 for secret ninja techniques.
Will
Thanks, it maybe reasonably for me
jitm