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
This works for anonymous types, ExpandoObject, Nancy.DynamicDictionary or anything else that can be cast to IDictionary.
public static bool PropertyExists(dynamic obj, string name) {
if (obj == null) return false;
if (obj is IDictionary dict) {
return dict.ContainsKey(name);
}
return obj.GetType().GetProperty(name) != null;
}