/// <summary> /// Convert a property in the AD to a string /// </summary> /// <param name="directoryEntry">DirectoryEntry</param> /// <param name="propertyName">string</param> /// <returns>string</returns> private static string ToDisplayString(DirectoryEntry directoryEntry, string propertyName) { return(directoryEntry.Properties[propertyName].Value switch { IAdsLargeInteger largeInteger => largeInteger.ToDateTimeOffset()?.ToString() ?? "empty", object[] objects => string.Join(",", objects.Select(o => o.ToString())), byte[] bytes => string.Join(",", bytes), _ => directoryEntry.Properties[propertyName].Value?.ToString(), });
/// <summary> /// Convert the specified property to the target type /// </summary> /// <param name="directoryEntry">DirectoryEntry</param> /// <param name="propertyName">string</param> /// <param name="targetType">Type</param> /// <returns>object</returns> public static object ConvertProperty(this DirectoryEntry directoryEntry, string propertyName, Type targetType = null) { targetType ??= typeof(string); bool isId = propertyName == "uSNChanged" || propertyName == "uSNCreated"; var value = directoryEntry.Properties[propertyName].Value; var valueType = value.GetType(); if (targetType.IsAssignableFrom(valueType)) { return(value); } if (targetType == typeof(string)) { return(value switch { IAdsLargeInteger largeIntegerToString when isId => largeIntegerToString.ToLong()?.ToString() ?? "n.a.", IAdsLargeInteger largeIntegerToString => largeIntegerToString.ToDateTimeOffset()?.ToString() ?? "empty", IADsSecurityDescriptor securityDescriptor => $"{securityDescriptor.Group}-{securityDescriptor.Owner}", object[] objects => string.Join(",", objects.Select(o => o.ToString())), byte[] bytes => string.Join(",", bytes), _ => value.ToString(), });