How to check if property exists

c#

Solution

You can try like this:

OBJECT.GetType().GetProperty("PROPERTY") != null

So in your code it would be like:

var a = one DirectoryEntry record;
var pi = a.GetType().GetProperty("employeeNumber");
var value = pi.GetValue(a, null);

EDIT:-

Try this:

bool x = a.Properties.Contains("employeeNumber");

Problem

I'm trying to read properties from `DirectoryEntry`. Unfortunately not all records have `employeeNumber` property so I need to check if it exists. I already tried: ``` a == one DirectoryEntry record a.GetType().GetProperty("employeeNumber")==null //always returns true String.IsNullOrWhiteSpace(a.Properties["employeeNumber"].ToString()) //exception ``` what else can I try?

Original source

Related problems