I'm trying to make a dynamic proxy as a wrapper for an existing library, the goal is to handle all operations (properties access, members access, method calls, ...) with an existing object through a dynamic dispatch. I might need that for cross-cutting concerns, like better error handling, logging or access control for this object.
I came up with creating a dynamic wrapper for an existing object that implements
IDynamicMetaObjectProvider interface, however parsing all the Expressions by
implementing my own DynamicMetaObject seems to be cumbersome!
The other solution is to inherit from the DynamicObject class to do the heavy lifting for
me, but again there are dozen of virtual methods, which I don't exactly know how to
override! I guess I only know what TrySetMember, TryGetMember and TryInvokeMember
methods do or how to implement them, but there are lots of other methods that I don't know
how to use!
public class DynamicProxy : DynamicObject
{
private object Value;
public DynamicProxy(object value)
{
this.Value = value;
}
public override bool TryGetMember(GetMemberBinder binder, out object result)
{
result = null; // get proxy object value using reflection
return true;
}
public override bool TrySetMember(SetMemberBinder binder, object value)
{
// set proxy object value using reflection
return true;
}
public override bool TryInvokeMember(InvokeMemberBinder binder, object[] args, out object result)
{
result = null; // call proxy object method using reflection
return true;
}
public override bool TryInvoke(InvokeBinder binder, object[] args, out object result)
{
return base.TryInvoke(binder, args, out result);
}
public override bool TryDeleteIndex(DeleteIndexBinder binder, object[] indexes)
{
// What to do here?
}
public override bool TryDeleteMember(DeleteMemberBinder binder)
{
// What to do here?
}
public override bool TryUnaryOperation(UnaryOperationBinder binder, out object result)
{
// What to do here?
}
// ... Other virtual methods of DynamicObject
}
So my question is: Is there any open source library that fully covers DynamicObject or at
the very least fully implements IDynamicMetaObjectProvider interface? Can somebody point
out an overview of DynamicObject virtual methods?