在Java中,可以使用动态代理来动态实现接口,如下所示:
public static <T> T createProxy(InvocationHandler invocationHandler, Class<T> anInterface) {
if (!anInterface.isInterface()) {
throw new IllegalArgumentException("Supplied interface must be an interface!");
}
return (T) Proxy.newProxyInstance(anInterface.getClassLoader(), new Class[]{anInterface}, invocationHandler);
}
.Net中是否有等效项?
最佳答案
有几个库在.NET中实现此功能。 Here's a list of them,带有一个基准。