Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Possible to get chained value of DynamicObject?

Tags:

c#

dynamic

Creating a class that implements DynamicObject

public class Test : DynamicObject
{
    public override bool TryGetMember(GetMemberBinder binder, out object result)
    {
        if (binder.Name == ("Posts"))
        {
            result = "property accessed was 'Posts'";
            return true;   
        }

        return base.TryGetMember(binder, out result);
    }
}

I can call

dynamic test = new Test();
var result = test.Posts;

And the value of result is "dynamic test = new Test(); var result = test.Posts;"

That's fine.

What I'm wondering is, when TryGetMember is invoked is it possible to get the chained value.

So if I called:

dynamic test = new Test();
var result = test.Posts.Load(123);

I can then do something like:

if (binder.Name == ("Posts"))
{
    if (... == "Load")
        result = this.Load<Post>(... 123);
    return true;   
}

Is something like that possible? I can't figure out a way to do it.

So far I have:

class Program
{
    static void Main(string[] args)
    {
        dynamic test = new Test();
        dynamic result = test.Posts.Load(123);

        Console.WriteLine(result.Name);

        dynamic result2 = test.Posts.Load(909);

        Console.WriteLine(result2.Name);

        Console.ReadKey();
    }
}

public class Test : DynamicObject
{
    public override bool TryGetMember(GetMemberBinder binder, out object result)
    {
        if (binder.Name == ("Posts"))
        {
            result = new ChainBuilder(this, "Post");
            return true;   
        }

        return base.TryGetMember(binder, out result);
    }

    public T Load<T>(int id) where T : Post, new()
    {
        if (id == 123)
            return new T {Id = 123, Name = "Bananas"};

        return new T {Id = 0, Name = "Others"};
    }

    private class ChainBuilder : DynamicObject
    {
        public dynamic OriginalObject { get; set; }
        public string PropertyInvoked { get; set; }

        public ChainBuilder(DynamicObject originalObject, string propertyInvoked)
        {
            OriginalObject = originalObject;
            PropertyInvoked = propertyInvoked;
        }

        public override bool TryInvokeMember(InvokeMemberBinder binder, object[] args, out object result)
        {
            if (binder.Name == "Load")
            {
                result = OriginalObject.Load<Post>((int)args[0]);
                return true;
            }

            return base.TryInvokeMember(binder, args, out result);
        }
    }
}

public class Post
{
    public int Id { get; set; }
    public string Name { get; set; }
}

Which is thanks to Bartosz.

But looks like it's basically what Marc has supplied.

Give's me a good starting point! I'll leave this open for now for any other suggestions.

This question has resulted in

  • https://gist.github.com/3798206
  • https://github.com/phillip-haydon/Raven.DynamicSession

Not a real project, just prototyping but achieved what we wanted.

like image 821
Phill Avatar asked Oct 06 '22 12:10

Phill


1 Answers

Each step of the evaluation is separate; it does not evaluate .Posts.Load(123) - it evaluates .Posts, and then separately evaluates .Load(123), so no: you can't do this in one step. The trick is to compose the values yourself, for example:

using System;
using System.Dynamic;
using System.Text;
static class Program {
    static void Main() {
        dynamic test = new Test();
        var result = test.Posts.Foo.Bar(123, "abc");
        Console.WriteLine(result);
    }
}
public class Test : DynamicObject
{
    public override bool TryGetMember(GetMemberBinder binder,
        out object result)
    {
        result = new MemberAccessWrapper("member accessed was " + binder.Name);
        return true;
    }
    private class MemberAccessWrapper : DynamicObject
    {
        private readonly string message;
        public override bool TryInvoke(InvokeBinder binder, object[] args,
            out object result)
        {
            StringBuilder builder = new StringBuilder(message).Append("(");
            for(int i = 0 ; i < args.Length ; i++) {
                if(i!=0)builder.Append(", ");
                if (args[i] == null) {
                    builder.Append("null");
                } else if (args[i] is string) {
                    builder.Append("@\"").Append(((string)args[i])
                         .Replace("\"", "\"\"")).Append("\"");
                } else {
                    builder.Append(args[i]);
                }
            }
            builder.Append(")");
            result = new MemberAccessWrapper(builder.ToString());
            return true;
        }
        public MemberAccessWrapper(string message)
        {
            this.message = message;
        }
        public override string ToString()
        {
            return message;
        }
        public override bool TryGetMember(GetMemberBinder binder,
            out object result)
        {
            result = new MemberAccessWrapper(message + "." + binder.Name);
            return true;
        }
    }
}
like image 128
Marc Gravell Avatar answered Oct 10 '22 03:10

Marc Gravell