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 ?
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.