A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from http://stackoverflow.com/questions/12634250/possible-to-get-chained-value-of-dynamicobject below:

c# - Possible to get chained value of DynamicObject?

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

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


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4