Skip to content

Instantly share code, notes, and snippets.

@oguzhaneren
Created December 21, 2017 18:30
Show Gist options
  • Save oguzhaneren/68039a5fdd0995faeae7b037bf8bcf4a to your computer and use it in GitHub Desktop.
Save oguzhaneren/68039a5fdd0995faeae7b037bf8bcf4a to your computer and use it in GitHub Desktop.
.Net Object Proxy
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);
}
}
@oguzhaneren
Copy link
Author

need install "System.Reflection.DispatchProxy" NuGet package

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment