Dar*_*ren 6 .net java proxy dynamic
在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);
}
Run Code Online (Sandbox Code Playgroud)
.Net中有等价物吗?
最广泛使用的是Castle Project的动态代理,它也被几个(或至少1个)模拟框架使用.请记住,默认情况下,dotnet中的方法(以及类似于属性的加糖方法)不是虚拟的,因此如果您没有在类设计中预期它,可能会产生一些令人头疼的问题.
归档时间: |
|
查看次数: |
2564 次 |
最近记录: |