Answer the question
In order to leave comments, you need to log in
How to create a dynamic proxy object based on a class or interface in C#?
Good afternoon,
Java has a feature that allows you to create dynamic proxies based on interfaces or classes. For example, through the JDK mechanism - proxying only the interface, CGLIB, ByteBuddy - proxying classes and interfaces. It looks something like this: https://www.baeldung.com/cglib
Enhancer enhancer = new Enhancer();
enhancer.setSuperclass(PersonService.class);
enhancer.setCallback((MethodInterceptor) (obj, method, args, proxy) -> {
if (method.getDeclaringClass() != Object.class && method.getReturnType() == String.class) {
return "Hello Tom!";
} else {
return proxy.invokeSuper(obj, args);
}
});
PersonService proxy = (PersonService) enhancer.create();
assertEquals("Hello Tom!", proxy.sayHello(null));
int lengthOfName = proxy.lengthOfName("Mary");
assertEquals(4, lengthOfName);
Answer the question
In order to leave comments, you need to log in
There is www.castleproject.org/projects/dynamicproxy
Based on it, everyone makes all sorts of proxies in runtime that intercept calls to something.
Here is the documentation: https://github.com/castleproject/Core/blob/master/...
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question