I’m doing some importing of data from AD/LDAP, and having some issues with the binary attributes returned as byte[] in the ResultPropertyValueCollection object.
The simplest example is the objectGUID attribute. I need it in a proper GUID object, not a byte[]. I can easily say new Guid(myByteArray), but that seems clumsy if there are other binary fields (Im guessing images and such) that are not GUIDs.
Is there any clean and tidy way of getting the unserialized type from a byte[] without just attempting to create different objects from it? Or better: Can I make LDAP/AD tell me what kind of object it is? I’d like this because I want a generic import that can handle all attributes and map them correctly into my own system.
…This might be sortof an edge case, but if it is possible to do this in a generic way, that would be perfect. If not, I’ll probably just stick to try-catching a new Guid(myByteArray) for now.
You should check that the byte array is exactly 16 bytes long. Other than that, a GUID is plainly just an array of 16 bytes, so you can’t validate it against any other metric.
As for accidentally deserializing another datatypes byte array, unfortunately this information is usually not stored with the serialized byte array. The caller is expected to already know what the byte array is, and do the casting themselves.