private static ObjectPropertyInfos BuildObjectPropertyInfos(object value, Type objectType) { ObjectPropertyInfos propertyInfos; if (ConvertSimpleToString(objectType)) { propertyInfos = ObjectPropertyInfos.SimpleToString; } else { var properties = GetPublicProperties(objectType); if (value is Exception) { // Special handling of Exception (Include Exception-Type as artificial first property) var fastLookup = BuildFastLookup(properties, true); propertyInfos = new ObjectPropertyInfos(properties, fastLookup); } else if (properties.Length == 0) { propertyInfos = ObjectPropertyInfos.SimpleToString; } else { propertyInfos = new ObjectPropertyInfos(properties, null); } } return(propertyInfos); }
private static bool TryExtractExpandoObject(Type objectType, out ObjectPropertyInfos propertyInfos) { foreach (var interfaceType in objectType.GetInterfaces()) { if (IsGenericDictionaryEnumeratorType(interfaceType)) { var dictionaryEnumerator = (IDictionaryEnumerator)Activator.CreateInstance(typeof(DictionaryEnumerator <,>).MakeGenericType(interfaceType.GetGenericArguments())); propertyInfos = new ObjectPropertyInfos(null, new[] { new FastPropertyLookup(string.Empty, TypeCode.Object, (o, p) => dictionaryEnumerator.GetEnumerator(o)) }); return(true); } } propertyInfos = default(ObjectPropertyInfos); return(false); }
public ObjectPropertyList LookupObjectProperties(object value) { Type objectType = value.GetType(); if (_objectTypeCache.TryGetValue(objectType, out var propertyInfos)) { if (!propertyInfos.HasFastLookup) { var fastLookup = BuildFastLookup(propertyInfos.Properties, false); propertyInfos = new ObjectPropertyInfos(propertyInfos.Properties, fastLookup); _objectTypeCache.TryAddValue(objectType, propertyInfos); } return(new ObjectPropertyList(value, propertyInfos.Properties, propertyInfos.FastLookup)); } if (_objectTypeOverride.Count > 0) { // User defined object reflection override lock (_objectTypeOverride) { if (_objectTypeOverride.TryGetValue(objectType, out var objectReflection)) { var objectProperties = objectReflection.Invoke(value); if (objectProperties?.Count > 0) { return(new ObjectPropertyList(objectProperties)); } // object.ToString() since no properties propertyInfos = ObjectPropertyInfos.SimpleToString; return(new ObjectPropertyList(value, propertyInfos.Properties, propertyInfos.FastLookup)); } } } #if DYNAMIC_OBJECT if (value is DynamicObject d) { var dictionary = DynamicObjectToDict(d); return(new ObjectPropertyList(dictionary)); } #endif propertyInfos = BuildObjectPropertyInfos(value, objectType); _objectTypeCache.TryAddValue(objectType, propertyInfos); return(new ObjectPropertyList(value, propertyInfos.Properties, propertyInfos.FastLookup)); }
public bool TryLookupExpandoObject(object value, out ObjectPropertyList objectPropertyList) { if (value is IDictionary <string, object> expando) { objectPropertyList = new ObjectPropertyList(expando); return(true); } #if DYNAMIC_OBJECT if (value is DynamicObject d) { var dictionary = DynamicObjectToDict(d); objectPropertyList = new ObjectPropertyList(dictionary); return(true); } #endif Type objectType = value.GetType(); if (ObjectTypeCache.TryGetValue(objectType, out var propertyInfos)) { if (!propertyInfos.HasFastLookup) { var fastLookup = BuildFastLookup(propertyInfos.Properties, false); propertyInfos = new ObjectPropertyInfos(propertyInfos.Properties, fastLookup); ObjectTypeCache.TryAddValue(objectType, propertyInfos); } objectPropertyList = new ObjectPropertyList(value, propertyInfos.Properties, propertyInfos.FastLookup); return(true); } if (TryExtractExpandoObject(objectType, out propertyInfos)) { ObjectTypeCache.TryAddValue(objectType, propertyInfos); objectPropertyList = new ObjectPropertyList(value, propertyInfos.Properties, propertyInfos.FastLookup); return(true); } objectPropertyList = default(ObjectPropertyList); return(false); }
public bool TryLookupExpandoObject(object value, out ObjectPropertyList objectPropertyList) { if (value is IDictionary <string, object> expando) { objectPropertyList = new ObjectPropertyList(expando); return(true); } #if DYNAMIC_OBJECT if (value is DynamicObject d) { var dictionary = DynamicObjectToDict(d); objectPropertyList = new ObjectPropertyList(dictionary); return(true); } #endif Type objectType = value.GetType(); if (_objectTypeCache.TryGetValue(objectType, out var propertyInfos)) { if (!propertyInfos.HasFastLookup) { var fastLookup = BuildFastLookup(propertyInfos.Properties, false); propertyInfos = new ObjectPropertyInfos(propertyInfos.Properties, fastLookup); _objectTypeCache.TryAddValue(objectType, propertyInfos); } objectPropertyList = new ObjectPropertyList(value, propertyInfos.Properties, propertyInfos.FastLookup); return(true); } if (_objectTypeOverride.Count > 0) { // User defined object reflection override lock (_objectTypeOverride) { if (_objectTypeOverride.TryGetValue(objectType, out var objectReflection)) { var objectProperties = objectReflection.Invoke(value); if (objectProperties?.Count > 0) { objectPropertyList = new ObjectPropertyList(objectProperties); return(true); } // object.ToString() since no properties propertyInfos = ObjectPropertyInfos.SimpleToString; objectPropertyList = new ObjectPropertyList(value, propertyInfos.Properties, propertyInfos.FastLookup); return(true); } } } if (TryExtractExpandoObject(objectType, out propertyInfos)) { _objectTypeCache.TryAddValue(objectType, propertyInfos); objectPropertyList = new ObjectPropertyList(value, propertyInfos.Properties, propertyInfos.FastLookup); return(true); } objectPropertyList = default(ObjectPropertyList); return(false); }