Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/306.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/design-patterns/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
.Net与Java';什么是动态代理?_Java_.net_Dynamic_Proxy - Fatal编程技术网

.Net与Java';什么是动态代理?

.Net与Java';什么是动态代理?,java,.net,dynamic,proxy,Java,.net,Dynamic,Proxy,在java中,可以使用动态代理动态实现接口,如下所示: public static <T> T createProxy(InvocationHandler invocationHandler, Class<T> anInterface) { if (!anInterface.isInterface()) { throw new IllegalArgumentException("Supplied interface must be an inter

在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);
}
publicstatict createProxy(调用处理程序调用处理程序,类接口){
如果(!anInterface.isInterface()){
抛出新的IllegalArgumentException(“提供的接口必须是接口!”);
}
return(T)Proxy.newProxyInstance(interface.getClassLoader(),新类[]{anInterface},invocationHandler);
}
.Net中是否有等效项?

没有直接等效项,但有关一些解决方法,请参阅:

背景:动态代理 动态地生成一个类 符合特定的运行时 接口,将所有调用代理到 单一的“通用”方法

早些时候,Stellsmi问是否 可以在.NET中执行此操作(这是一个 Java的标准部分)。看来 这是我第二次谈论它 我想在同样多的日子里,它是值得的 博客


有几个库在.NET中实现了这一点,有基准。

是。您可以从抽象中派生出来。

也可以查看一下(AOP framework for.NET)-它可以做类似的事情,但需要在编译时完成。您可能更喜欢它支持的方法。

最广泛使用的方法是,它也被几个(或至少1个)模拟框架使用。请记住,在dotnet中,方法(以及像属性这样的糖化方法)在默认情况下不是虚拟的,因此,如果在类设计中没有预料到这一点,那么这可能会带来一些麻烦。

你错了。RealProxies用于完全不同的目的(.NET远程处理)。此外,您不能将它们用于非MBR类型。RealProxy用于远程处理,但它们不必仅用于此目的。您完全可以使用它们在同一进程中拦截调用。此外,虽然它们仅限于类的MarshalByRefObject,但问题是关于接口的,RealProxy支持所有接口(因为它们总是vtable调度的)。是的。有一组框架提供此功能。不止一个模拟框架。有关使用动态代理的部分项目列表,请参见此处