Created
December 21, 2017 18:30
-
-
Save oguzhaneren/68039a5fdd0995faeae7b037bf8bcf4a to your computer and use it in GitHub Desktop.
.Net Object Proxy
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| void Main() | |
| { | |
| var fooInstance = new FooImpl(); | |
| var proxy = DispatchProxy.Create<IFoo, FooProxy>(); | |
| dynamic p = proxy; | |
| p.SetTarget(fooInstance); | |
| var s = proxy.Bar(123); | |
| Console.WriteLine(s); | |
| } | |
| interface IFoo | |
| { | |
| string Bar(int boo); | |
| } | |
| class FooImpl : IFoo | |
| { | |
| public string Bar(int boo) | |
| { | |
| return $"Value {boo} was passed"; | |
| } | |
| } | |
| class FooProxy : DispatchProxy | |
| { | |
| private object target; | |
| public void SetTarget(object target) | |
| { | |
| this.target = target; | |
| } | |
| public FooProxy() | |
| { | |
| } | |
| protected override object Invoke(MethodInfo targetMethod, object[] args) | |
| { | |
| args[0]=987; | |
| return targetMethod.Invoke(target, args); | |
| } | |
| } |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
need install "System.Reflection.DispatchProxy" NuGet package