Is there a way to perform a chained null check in a dynamic/expando?

后端 未结 2 1776
栀梦
栀梦 2021-01-14 19:09

C# has the usefull Null Conditional Operator. Well explained in this answer too.

I was wondering if it is possible to do a similar check like this when my object is

2条回答
  •  旧时难觅i
    2021-01-14 19:42

    If you want to support this in a more natural way you can inherit from DynamicObject and provide a custom implementation:

    class MyExpando : DynamicObject
        {
            private readonly Dictionary _dictionary = new Dictionary();
    
            public override bool TryGetMember(GetMemberBinder binder, out object result)
            {
                var name = binder.Name.ToLower();
                result = _dictionary.ContainsKey(name) ? _dictionary[name] : null;
                return true;
            }
    
            public override bool TrySetMember(SetMemberBinder binder, object value)
            {
                _dictionary[binder.Name.ToLower()] = value;
                return true;
            }
        }
    

    Testing:

     private static void Main(string[] args)
            {
                dynamic foo = new MyExpando();
                if (foo.Boo?.Lol ?? true)
                {
                    Console.WriteLine("It works!");
                }
                Console.ReadLine();
            }
    

    The output will be "It works!". Since Boo does not exist we get a null reference so that the Null Conditional Operator can work.

    What we do here is to return a null reference to the output parameter of TryGetMember every time a property is not found and we always return true.

提交回复
热议问题