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?
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");
Something like this:
a.Properties["employeeNumber"] == null || a.Properties["employeeNumber"].ToString().Length == 0
In your case a.Properties["employeeNumber"] can be null and you get an exception, trying to convert null to string.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With