Duck typing / dynamic proxies on existing instances of objects

988 views Asked by At

I have an object handed into our library and passed through various processes. I need to attach some additional information to these objects as they pass through various stages and out the other end - a kind of dynamic decorator pattern, I guess, except adding additional properties rather than changing existing behaviour.

I was hoping to use LinFu or Castle to create a dynamic proxy and implement an additional interface on the object to store this. Components that know about the extended interface could cast and access it - whilst those that are not are oblivious, as the underlying type has not changed.

However, I hadn't appreciated that all these mechanisms assume you have control over the point at which the type is initially created - which I don't.

Does anyone have suggestions on how I could better approach this?

Many thanks

3

There are 3 answers

0
Rick Strahl On BEST ANSWER

This doesn't address your scenario exactly but how about using a DynamicObject implementation that acts as a decorator around your object? It would allow you to access the original object as well as additional properties. Sort of like ExpandoObject but starting out with your own instance data.

Something like this:

public class Expando : DynamicObject
{
    public dynamic Instance;
    Dictionary<string, dynamic> ExtraProperties = new Dictionary<string, dynamic>();

    public Expando(object instance)
    {
        Instance = instance;
    }

    public override bool TryGetMember(GetMemberBinder binder, out object result)
    {
        try
        {
            result = ReflectionUtils.GetProperty(Instance, binder.Name);
            return true;
        }
        catch
        {
            if (ExtraProperties.Keys.Contains(binder.Name))
            {
                result = ExtraProperties[binder.Name];
                return true;
            }
        }

        result = null;
        return false;
    }

    public override bool TrySetMember(SetMemberBinder binder, object value)
    {
        try
        {
            ReflectionUtils.SetProperty(Instance, binder.Name, value);
        }
        catch (Exception ex)
        {
            ExtraProperties[binder.Name] = value;
        }

        return true;
    }

    public override bool TryInvokeMember(InvokeMemberBinder binder, object[] args, out object result)
    {
        try
        {
            result = ReflectionUtils.CallMethod(Instance, binder.Name, args);
            return true;
        }
        catch
        {}

        result = null;
        return false;
    }
}

It doesn't address your strong typing/interface requirement unfortunately though and performance isn't going to be the greatest given the Reflection usage here (from https://github.com/RickStrahl/Westwind.Utilities/blob/master/Westwind.Utilities/Utilities/ReflectionUtils.cs)

0
mcintyre321 On

You want to use the approach from here adding expando properties to a typed object at runtime in c#

That way you won't get memory leaks

0
Robert Levy On

Seems like overkill... just create a new class containing just your "extra" properties. The define a static Dictionary<MainClass,ExtensionsClass>. When your components "in the know" want to look at the extended properties for an object, they just look that object up in the dictionary.