/// <summary> /// Returns ordered dictionary with object fields. /// </summary> /// <param name="obj">object</param> /// <param name="includeBase">Should include fields from object base class, default: false</param> /// <returns>Ordered dictionary with object fields</returns> public static OrderedImmutableDictionary <string, object> Fields(object obj, bool includeBase = false) { var builder = OrderedHashImmutableDictionary <string, object> .Builder(); const BindingFlags options = BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public; AppendFields(obj, obj.GetType(), builder, options, includeBase); return(builder.Build()); }
public void ShouldReturnKeysInPutOrder() { // given var dictionary = OrderedHashImmutableDictionary <string, int> .Builder() .Put("Z", 1) .Put("Y", 2) .Put("X", 3) .Build(); // when var orderedKeys = dictionary.OrderedKeys; // then Check.That(orderedKeys).ContainsExactly("Z", "Y", "X"); }
private static void AppendFields(object obj, Type type, OrderedHashImmutableDictionary <string, object> .OrderedHashImmutableDictionaryBuilder orderedHashImmutableDictionaryBuilder, BindingFlags options, bool includeBase) { if (type != null) { var fields = type.GetFields(options); foreach (var field in fields.Where(f => !f.Name.EndsWith("k__BackingField"))) { var value = field.GetValue(obj); orderedHashImmutableDictionaryBuilder.Put(field.Name, value); } if (includeBase) { AppendFields(obj, type.BaseType, orderedHashImmutableDictionaryBuilder, BindingFlags.NonPublic | BindingFlags.Instance, true); } } }
private static void AppendProperties(object obj, Type type, OrderedHashImmutableDictionary <string, object> .OrderedHashImmutableDictionaryBuilder orderedHashImmutableDictionaryBuilder, BindingFlags options, bool includeBase) { if (type != null) { var properties = type.GetProperties(options); foreach (var property in properties) { var value = property.GetValue(obj); orderedHashImmutableDictionaryBuilder.Put(property.Name, value); } if (includeBase) { AppendProperties(obj, type.BaseType, orderedHashImmutableDictionaryBuilder, BindingFlags.NonPublic | BindingFlags.Instance, true); } } }
private static void AppendFields(object obj, Type type, OrderedHashImmutableDictionary <string, object> .Builder builder, BindingFlags options, bool includeBase) { if (type != null) { var fields = type.GetFields(options); foreach (var field in fields) { var value = field.GetValue(obj); builder.Put(field.Name, value); } if (includeBase) { AppendFields(obj, type.BaseType, builder, BindingFlags.NonPublic | BindingFlags.Instance, true); } } }