Get the default value if the dynamic object does not contain a property

When working with dynamic objects in many languages, there is a constructor that allows you to get the value of a property, and if the specified property does not exist, return the default value.

I want to know if there is a similar method / syntax when working with dynamic in .NET. I know that you can add ExpandoObject to the dictionary, but sometimes there is no guarantee that the dynamic object will be Expando.

I think that will have the same effect from the following code

public class SomeClass
{
    public string ValidProperty { get; set; }
}

dynamic t = new SomeClass() { 
    ValidProperty = "someValue"
};

Console.WriteLine(t.Get("ValidProperty", "doesn't exist")); // Prints 'someValue'
Console.WriteLine(t.Get("InvalidProperty", "doesn't exist")); // Prints 'doesn't exist'
+4
source share
3 answers

I want to know if there is a similar method / syntax when working with dynamic in .NET. I know that you can add ExpandoObject to the dictionary, but sometimes there is no guarantee that the dynamic object will be Expando.

In addition, there is no guarantee that this is a compile-time object.

try/catch, . :

 public class MyDynamic: DynamicObject
{
    static int Count = 0;
    public override bool TryGetMember(GetMemberBinder binder, out object result)
    {
        result = Count++;
        if (binder.Name == "Test")
        {
            return Count % 2 == 0;
        }
        return false;
    }
}

,

dynamic d = new MyDynamic();
try { Console.WriteLine(d.Test); } catch (Exception ex) { Console.WriteLine(ex.Message); }
try { Console.WriteLine(d.Test); } catch (Exception ex) { Console.WriteLine(ex.Message); }
try { Console.WriteLine(d.Test); } catch (Exception ex) { Console.WriteLine(ex.Message); }
try { Console.WriteLine(d.Test); } catch (Exception ex) { Console.WriteLine(ex.Message); }

d.Test , . , , , .

+1

ExpandoObjects, . , IDictionary , , . .

    public static T PropertyOrDefault<T>(this ExpandoObject obj, string propertyName)
    {
        var dynamicAsDictionary = (IDictionary<string, object>)obj;

        if (!dynamicAsDictionary.ContainsKey(propertyName))
        {
            return default(T);
        }

        object propertyValue = dynamicAsDictionary[propertyName];

        if (!(propertyValue is T))
        {
            return default(T);
        }

        return (T)propertyValue;
    }

ExpandoObject, .

    public static T PropertyOrDefault<T>(dynamic obj, string propertyName)
    {
        if (obj is ExpandoObject)
        {
            return ((ExpandoObject)obj).PropertyOrDefault<T>(propertyName);
        }

        Type objectType = obj.GetType();
        PropertyInfo p = objectType.GetProperty(propertyName);

        if (p != null)
        {
            object propertyValue = p.GetValue(obj);

            if (propertyValue is T)
            {
                return (T)propertyValue;
            }
        }

        return default(T);
    }
0

, :

public bool HasProperty(Object o, string propertyName)
{
    return o.GetType().GetProperty(propertyName) != null;
}

...

dynamic t = new
              {
            validProperty = "value",
              };

MessageBox.Show(HasProperty(t, "invalidProperty")?"true":"false");
MessageBox.Show(HasProperty(t, "validProperty")  ?"true":"false");
0

Source: https://habr.com/ru/post/1608873/


All Articles