D
D
Denis Bredun2020-07-21 18:39:44
C++ / C#
Denis Bredun, 2020-07-21 18:39:44

Why do we need a binder or a binder object in general?

There is a code:

using System.Dynamic;
 
class PersonObject : DynamicObject
{
    Dictionary<string, object> members = new Dictionary<string, object>();
 
    // установка свойства
    public override bool TrySetMember(SetMemberBinder binder, object value)
    {
        members[binder.Name] = value;
        return true;
    }
    // получение свойства
    public override bool TryGetMember(GetMemberBinder binder, out object result)
    {
        result = null;
        if (members.ContainsKey(binder.Name))
        {
            result = members[binder.Name];
            return true;
        }
        return false;
    }
    // вызов метода
    public override bool TryInvokeMember(InvokeMemberBinder binder, object[] args, out object result)
    {
        dynamic method = members[binder.Name];
        result = method((int)args[0]);
        return result != null;
    }
}

Here the binder is used to refer to properties and methods by name. So the question is: besides this, is it intended for something else? If yes, then why? Where else can binders be used, besides working with dynamic objects?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
F
freeExec, 2020-07-21
@Luffy1

No.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question