你可以在不创建实例的情况下调用接口方法吗?不是。接口是例如成员;静态类成员与接口无关。
你也许能够得到你想要的东西提供了一个静态接口的实现,
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 })
或者你可以做一个扩展方法:如果你是绝对相信
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 });
想想这样:如果你可以调用它,这个方法实际上会做什么? – Aaronaught 2010-05-07 13:53:50