0

so I have a string containing a json

dynamic jsonQuery = JObject.Parse(query);

since this object I don't always get all the property's I need to be able to check if a ceratin property exits

if (IsProperty(jsonQuery,"MetaContent"))
{
  // do stuff
}

I tried it with classic reflection but that does not seem to work:

private bool IsProperty(object obj, string propertyName)
{
    return obj.GetType().GetProperty(propertyName) != null;
}

The function always returns false any idea?

Jester
  • 3,069
  • 5
  • 30
  • 44
  • 1
    Or [How to detect if a property exists on an ExpandoObject?](http://stackoverflow.com/questions/2839598/how-to-detect-if-a-property-exists-on-an-expandoobject). – CodeCaster Dec 06 '14 at 13:49
  • 1
    Sorry maybe I am missing something but I don't think that this answers my question fully. I need to be able to check multiple property's so I was looking for a unified function to do it. if the examples that you guys suggested it is always the case that I have to know what I am looking for so I need to do a check for each property in a try/catch block instead of one function – Jester Dec 06 '14 at 14:01
  • 1
    There is no method built into the framework. Put the logic from the duplicate or the second link in a method, and call that method using your object for every property you wish to discover: `if (HasDynamicProperty(jsonQuery, "MetaContent")) { ... }`. You can even make it an extension method. – CodeCaster Dec 06 '14 at 14:02
  • ok got it sorted ty all for help – Jester Dec 06 '14 at 14:16

0 Answers0