I have an anonymous type object that I receive as a dynamic from a method I would like to check in a property exists on that object.
....
var settings = new
In case someone need to handle a dynamic object come from Json, I has modified Seth Reno answer to handle dynamic object deserialized from NewtonSoft.Json.JObjcet.
public static bool PropertyExists(dynamic obj, string name)
{
if (obj == null) return false;
if (obj is ExpandoObject)
return ((IDictionary)obj).ContainsKey(name);
if (obj is IDictionary dict1)
return dict1.ContainsKey(name);
if (obj is IDictionary dict2)
return dict2.ContainsKey(name);
return obj.GetType().GetProperty(name) != null;
}