/// <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 an IAdsLargeInteger to a DateTime /// </summary> /// <param name="largeInteger">IAdsLargeInteger</param> /// <returns>DateTime?</returns> public static DateTime?ToDateTime(this IAdsLargeInteger largeInteger) { var dateLong = largeInteger.ToLong(); if (!dateLong.HasValue) { return(null); } return(DateTime.FromFileTimeUtc(dateLong.Value)); }
/// <summary> /// Convert an IAdsLargeInteger to a long /// </summary> /// <param name="largeInteger">IAdsLargeInteger</param> /// <returns>long?</returns> public static long?ToLong(this IAdsLargeInteger largeInteger) { long dateLong = (largeInteger.HighPart << 32) + largeInteger.LowPart; if (dateLong <= 0 || dateLong == 9223372036854775807) { return(null); } return(dateLong); }
/// <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(), });