Created
November 28, 2019 01:23
-
-
Save hendrasyp/a47e76336e008465757f4d976f15cd41 to your computer and use it in GitHub Desktop.
How to Check Property on Dynamic Data type
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// <summary> | |
/// Check any property on dynamic data type whether id exists or not | |
/// </summary> | |
/// <param name="settings">your object</param> | |
/// <param name="name">key to search</param> | |
/// <returns>True or False</returns> | |
/// <example> | |
/// bool isExists = IsPropertyExists(Dictionary<string, dynamic>, string); | |
/// <example> | |
public bool IsPropertyExists(dynamic settings, string name) | |
{ | |
if (settings is System.Dynamic.ExpandoObject) | |
{ | |
return ((IDictionary<string, object>)settings).ContainsKey(name); | |
} | |
return settings.GetType().GetProperty(name) != null; | |
} | |
// Use | |
Dictionary<string, dynamic> myobjects; | |
bool isExists = IsPropertyExists(myobjects, "keytosearch"); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment