public bool TryConvertToScalar(object value, IMessageTemplatePropertyValueFactory propertyValueFactory, out ScalarValue result) { var type = value.GetType(); #if !REFLECTION_API_EVOLVED if (!type.IsGenericType || type.GetGenericTypeDefinition() != typeof(Nullable <>)) #else if (!type.IsConstructedGenericType || type.GetGenericTypeDefinition() != typeof(Nullable <>)) #endif { result = null; return(false); } #if USE_DYNAMIC var dynamicValue = (dynamic)value; var innerValue = dynamicValue.HasValue ? (object)dynamicValue.Value : null; #elif !REFLECTION_API_EVOLVED var targetType = type.GetGenericArguments()[0]; var innerValue = Convert.ChangeType(value, targetType, null); #else var targetType = type.GenericTypeArguments[0]; var innerValue = Convert.ChangeType(value, targetType); #endif result = propertyValueFactory.CreatePropertyValue(innerValue) as ScalarValue; return(result != null); }
public bool TryDestructure(object value, IMessageTemplatePropertyValueFactory propertyValueFactory, out TemplatePropertyValue result) { if (value == null) { throw new ArgumentNullException(nameof(value)); } if (!_canApply(value.GetType())) { result = null; return(false); } var projected = _projection(value); result = propertyValueFactory.CreatePropertyValue(projected, true); return(true); }
static IEnumerable <TemplateProperty> GetProperties( object value, IMessageTemplatePropertyValueFactory recursive) { var properties = #if !REFLECTION_API_EVOLVED // TODO: stop using LINQ value.GetType().GetProperties().Where(p => p.CanRead && p.GetGetMethod().IsPublic&& !p.GetGetMethod().IsStatic&& (p.Name != "Item" || p.GetIndexParameters().Length == 0)); #else value.GetType().GetPropertiesRecursive(); #endif foreach (var prop in properties) { object propValue; try { #if !REFLECTION_API_EVOLVED // https://blogs.msdn.microsoft.com/dotnet/2012/08/28/evolving-the-reflection-api/ propValue = prop.GetValue(value, null); #else propValue = prop.GetValue(value); #endif } catch (TargetParameterCountException) { SelfLog.WriteLine("The property accessor {0} is a non-default indexer", prop); continue; } catch (TargetInvocationException ex) { SelfLog.WriteLine("The property accessor {0} threw exception {1}", prop, ex); propValue = "The property accessor threw an exception: " + ex.InnerException.GetType().Name; } yield return(new TemplateProperty(prop.Name, recursive.CreatePropertyValue(propValue, true))); } }