What is the best way to implement dynamic proxy in C #?

I need to create a dynamic proxy in C #. I want this class to wrap another class and take over the public interface by forwarding calls to these functions:

class MyRootClass { public virtual void Foo() { Console.Out.WriteLine("Foo!"); } } interface ISecondaryInterface { void Bar(); } class Wrapper<T> : ISecondaryInterface where T: MyRootClass { public Wrapper(T otherObj) { } public void Bar() { Console.Out.WriteLine("Bar!"); } } 

This is how I want to use it:

 Wrapper<MyRootClass> wrappedObj = new Wrapper<MyRootClass>(new MyRootClass()); wrappedObj.Bar(); wrappedObj.Foo(); 

for creating:

 Bar! Foo! 

Any ideas?

What is the easiest way to do this?

What is the best way to do this?

Thank you very much.

UPDATE

I tried following the Wernight recommendation and implementing this with C # 4.0 dynamic proxies. Sorry, I'm still stuck. The proxy point should mimic another interface, which (usually usually) is expected. Using DynamicObject requires me to change all clients of this to use "dynamic" instead of "ISecondaryInterface".

Is there a way to get a proxy object, for example, when it wraps A, it advertises (statically?) That it supports interface A; and when he wraps B, he advertises that supports interface B?

UPDATE 2

For instance:

 class MySecretProxy : DynamicObject, ISecondaryInterface { public override void TryInvokeMember(...) { .. } // no declaration of Bar -- let it be handled by TryInvokeMember } 
+4
source share
5 answers

.NET 4 DynamicObject can help you achieve this.

Previously, the .NET environment could use:

  • Aspect #
  • Encase AOP
  • Spring.net
  • Aspect.NET
  • AspectDNG
  • Dynamic proxy
  • Compose *
  • Loom.net
  • Postsharp

Each of these frameworks uses a number of code injection methods both before and after the method. Usually they are divided into 4 categories.

  • MSIL injection . Here we inject the MSIL code into the body of the executed method. (Post sharply)
  • Dynamic runtime injection . Using methods such as reflection, we invoke methods dynamically.
  • Embedding type constructor . In connection with the implementation of the runtime, we create a type based on the type we want to proxy, and then send requests through this type. (Dynamic proxy)
  • Container Injection . Requests go through a container that calls the code before and after the execution of our method.

More details.

I know that the Dynamic Proxy Castle Project is often used (for example, in Moq, to ​​name one large project).


RESPONSE TO UPDATED TOPIC

What you wrote will not compile. Dynamic proxies are the generated runtime code, so you will need to create a specific instance of the class that you proxy in some way. You may be looking for AOP (aspect-oriented programming).

 class MySecretProxy<T> : DynamicObject, T where T : new() { private T _instance = new T(); public override void TryInvokeMember(...) { ... } } MySecretProxy<Bar> bar; 
+6
source

Have you seen the Castle DynamicProxy project? This can provide what you end up trying to achieve. See http://www.castleproject.org/dynamicproxy/index.html

It also opens the source code, so you can even fork it if necessary.

+3
source

You can do this with RealProxy if the target type is an interface or derived from MarshalByRefObject.

+1
source

You can see linfu , which contains a dynamic proxy mechanism.

0
source

I know that proxies used by nhibernate for lazy loading

Castle

Linf

Spring bytecode

0
source

Source: https://habr.com/ru/post/1334259/


All Articles