In C#, you can check if a property exists on a dynamic anonymous type by using the ExpandoObject or by converting the dynamic object to a dictionary. Here's how you can do it using both approaches:
ExpandoObject:using System.Dynamic; dynamic obj = new ExpandoObject(); obj.Name = "John"; obj.Age = 30; bool hasProperty = ((IDictionary<string, object>)obj).ContainsKey("Name"); if (hasProperty) { // Property "Name" exists } else { // Property "Name" does not exist } using System.Collections.Generic; using System.Dynamic; dynamic obj = new ExpandoObject(); obj.Name = "John"; obj.Age = 30; var dictionary = (IDictionary<string, object>)obj; bool hasProperty = dictionary.ContainsKey("Name"); if (hasProperty) { // Property "Name" exists } else { // Property "Name" does not exist } In both approaches, we cast the dynamic object to IDictionary<string, object> and then check if the property exists by using the ContainsKey method.
Check if a property exists in dynamic object in C#
ExpandoObject to dynamically check property names.dynamic obj = new ExpandoObject(); obj.Name = "John Doe"; bool HasProperty(dynamic obj, string propertyName) { return ((IDictionary<string, object>)obj).ContainsKey(propertyName); } Console.WriteLine(HasProperty(obj, "Name")); // True Console.WriteLine(HasProperty(obj, "Age")); // False Check property existence in anonymous type C#
var anon = new { Name = "John Doe" }; bool HasProperty(object obj, string propertyName) { return obj.GetType().GetProperty(propertyName) != null; } Console.WriteLine(HasProperty(anon, "Name")); // True Console.WriteLine(HasProperty(anon, "Age")); // False Check if a property exists on a dynamic object C#
ExpandoObject.dynamic obj = new { Name = "John Doe" }; bool HasProperty(dynamic obj, string propertyName) { return obj.GetType().GetProperty(propertyName) != null; } Console.WriteLine(HasProperty(obj, "Name")); // True Console.WriteLine(HasProperty(obj, "Age")); // False C# dynamic check if property exists without reflection
ExpandoObject directly might be the solution.dynamic obj = new ExpandoObject(); obj.Name = "John Doe"; bool HasProperty(ExpandoObject obj, string propertyName) { return ((IDictionary<string, object>)obj).ContainsKey(propertyName); } Console.WriteLine(HasProperty(obj, "Name")); // True Console.WriteLine(HasProperty(obj, "Age")); // False C# check if anonymous type has property
var anon = new { Name = "John Doe" }; bool HasProperty(object obj, string propertyName) { return obj.GetType().GetProperty(propertyName) != null; } Console.WriteLine(HasProperty(anon, "Name")); // True Console.WriteLine(HasProperty(anon, "Age")); // False Determine if property exists in ExpandoObject C#
ExpandoObject can be efficiently done by casting to IDictionary<string, object>.dynamic obj = new ExpandoObject(); obj.Name = "John Doe"; bool HasProperty(dynamic obj, string propertyName) { return ((IDictionary<string, object>)obj).ContainsKey(propertyName); } Console.WriteLine(HasProperty(obj, "Name")); // True Console.WriteLine(HasProperty(obj, "Age")); // False How to check if a property exists in a dynamic object at runtime C#
dynamic obj = new ExpandoObject(); obj.Name = "John Doe"; bool HasProperty(dynamic obj, string propertyName) { return ((IDictionary<string, object>)obj).ContainsKey(propertyName); } Console.WriteLine(HasProperty(obj, "Name")); // True Console.WriteLine(HasProperty(obj, "Age")); // False C# check if property exists in anonymous object
var anon = new { Name = "John Doe" }; bool HasProperty(object obj, string propertyName) { return obj.GetType().GetProperty(propertyName) != null; } Console.WriteLine(HasProperty(anon, "Name")); // True Console.WriteLine(HasProperty(anon, "Age")); // False C# dynamic type property check
dynamic obj = new ExpandoObject(); obj.Name = "John Doe"; bool HasProperty(dynamic obj, string propertyName) { return ((IDictionary<string, object>)obj).ContainsKey(propertyName); } Console.WriteLine(HasProperty(obj, "Name")); // True Console.WriteLine(HasProperty(obj, "Age")); // False How to determine if a dynamic object has a property C#
ExpandoObject or reflection.dynamic obj = new ExpandoObject(); obj.Name = "John Doe"; bool HasProperty(dynamic obj, string propertyName) { if (obj is ExpandoObject) { return ((IDictionary<string, object>)obj).ContainsKey(propertyName); } else { return obj.GetType().GetProperty(propertyName) != null; } } Console.WriteLine(HasProperty(obj, "Name")); // True Console.WriteLine(HasProperty(obj, "Age")); // False assign event-delegation database-trigger jmx monaco-editor object-detection-api google-forms ftp4j loadimage thread-safety