Question

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?

Was it helpful?

Solution

You don't have to override all methods of DynamicObject, just TryGetMember and TrySetMember is enough in most cases. DynamicObject have default implementations for all methods, and you can implement only methods you want to change

DynamicObject is a class that implements IDynamicMetaObjectProvider, so use it if your wrapper doesn't have to have any other base class

Implementing IDynamicMetaObjectProvider would be a bit boring, but if you however will decide to do that, take a look how it is implemented in DynamicObject with ILSpy

OTHER TIPS

I have an opensource framework ImpromptuInterface (in nuget), that has an abstract class ImpromptuForwarder that is a DynamicObject subclass that's base implementation just forwards the calls using the dlr for properties, methods and events and indexers to a Target.

It also has a lot of other fun dlr proxying stuff too, including static interface typing to dynamic implementations, curring methods, late bound type static method proxy.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top