3

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?

szpic
  • 4,346
  • 15
  • 54
  • 85

2 Answers2

10

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");
Rahul Tripathi
  • 168,305
  • 31
  • 280
  • 331
1

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.

IliaJ
  • 159
  • 3
  • left side always returns false even if there is no `employeeNumber` right side is error because property doesn't have Length property – szpic Apr 15 '14 at 06:55