У него простой API, но неясно, как обернуть существующий объект в прокси.
Например, имея этот интерфейс:
Код: Выделить всё
interface IFoo
{
string Bar(int boo);
}
Код: Выделить всё
class FooImpl : IFoo
{
public string Bar(int boo)
{
return $"Value {boo} was passed";
}
}
Код: Выделить всё
class Program
{
static void Main(string[] args)
{
var fooInstance = new FooImpl();
var proxy = DispatchProxy.Create();
var s = proxy.Bar(123);
Console.WriteLine(s);
}
}
class FooProxy : DispatchProxy
{
protected override object Invoke(MethodInfo targetMethod, object[] args)
{
return targetMethod.Invoke(/* I need fooInstance here */, args);
}
}
Код: Выделить всё
class FooProxy : DispatchProxy
{
private object target;
public void SetTarget(object target)
{
this.target = target;
}
protected override object Invoke(MethodInfo targetMethod, object[] args)
{
return targetMethod.Invoke(target, args);
}
}
Код: Выделить всё
var fooInstance = new FooImpl();
var proxy = DispatchProxy.Create();
((FooProxy)proxy).SetTarget(fooInstance);
// the rest of code...
Подробнее здесь: https://stackoverflow.com/questions/449 ... patchproxy
Мобильная версия