So ImpromtuInterface
uses the DLR, basically when you call ActLike() it emits and caches a proxy for that interface, and wraps it around your object.
public class Proxy:IMyInterface {
dynamic target;
public int Foo(){
return (int)target.Foo()
}
}
Since it's a dynamic invocation, you don't actually have the method on your target if it's and IDynamicMetaObjectProvider the most popular to customize being System.Dynamic.DynamicObject
.
public class RoughDynamicAdapter:DynamicObject{
public override bool TryInvokeMember(InvokeMemberBinder binder,
Object[] args,
out Object result){
if(binder.Name == "Foo"){
result = /* do your own logic */
return true;
}
result = null;
return false;
}
}
But this is a lot of work, because it you have to handle the non modified calls as much as the modified calls.
There are several prefab DynamicObject
s in ImpromptuInterface
that I've moved to a separate library Dynamitey.
One in particular, BaseForwarder
sounds like what you want, because rather than handing all the logic, forwarding the message to a target object is already implemented as the base functionality.
public class DynamicAdapter:Dynamitey.DynamicObjects.BaseForwarder {
public DynamicAdapter(object target):base(target){
}
public override bool TryInvokeMember(InvokeMemberBinder binder,
Object[] args,
out Object result){
var newName = binder.Name;
if(newName == "Foo"){
result = Dynamic.InvokeMember(CallTarget, "Bar", args)
return true;
}
//else pass them method on as it was called
return base.TryInvokeMember(binder, args, out result)
}
}
Then to use it would be new DynamicAdapter(myObject).ActLike<IMyInterface>()