public bool TryConvertToScalar(object value, ITemplatePropertyValueFactory propertyValueFactory, out ScalarValue result) { var type = value.GetType(); #if USE_REFLECTION_40 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 USE_REFLECTION_40 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); }
static IEnumerable <TemplateProperty> GetProperties(object value, ITemplatePropertyValueFactory recursive) { var properties = #if USE_REFLECTION_40 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 USE_REFLECTION_40 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))); } }
public bool TryConvertToScalar(object value, ITemplatePropertyValueFactory propertyValueFactory, out ScalarValue result) { var type = value.GetType(); #if USE_REFLECTION_40 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 USE_REFLECTION_40 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, ITemplatePropertyValueFactory propertyValueFactory, out TemplatePropertyValue result) { if (value == null) { throw new ArgumentNullException("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, ITemplatePropertyValueFactory recursive) { var properties = #if USE_REFLECTION_40 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 USE_REFLECTION_40 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)); } }