public static PocoNode Simple(int columnOrdinal, PropertyInfo property) { var node = new PocoNode(property) { _ColumnOrdinal = columnOrdinal }; return(node); }
public static PocoNode Complex(PropertyInfo property) { var node = new PocoNode(property) { _IsComplex = true, }; return(node); }
public static PocoNode Root(Type type) { var node = new PocoNode(type) { _IsComplex = true, }; return(node); }
public static PocoNode Simple(int columnOrdinal, ParameterInfo parameter) { var node = new PocoNode(parameter.ParameterType) { _ColumnOrdinal = columnOrdinal, Parameter = parameter }; return(node); }
protected override Node CreateSimpleProperty(Node container, string propertyName, int columnOrdinal) { PropertyInfo property = GetProperty(((PocoNode)container).UnderlyingType, propertyName); if (property == null) { return(null); } return(PocoNode.Simple(columnOrdinal, property)); }
protected override Node CreateComplexProperty(Node container, string propertyName) { PropertyInfo property = GetProperty(((PocoNode)container).UnderlyingType, propertyName); if (property == null) { return(null); } return(PocoNode.Complex(property)); }
public override object Create(IDataRecord record, MappingContext context) { if (this.Constructor == null) { return(Activator.CreateInstance(this.Type)); } object[] args = this.ConstructorParameters.Select(m => m.Value.Map(record, context)).ToArray(); if (this.ConstructorParameters.Any(p => ((PocoNode)p.Value).ConvertFunction != null) || args.All(v => v == null)) { return(this.Constructor.Invoke(args)); } try { return(this.Constructor.Invoke(args)); } catch (ArgumentException) { bool convertSet = false; for (int i = 0; i < this.ConstructorParameters.Count; i++) { object value = args[i]; if (value == null) { continue; } PocoNode paramNode = (PocoNode)this.ConstructorParameters.ElementAt(i).Value; if (!paramNode.Type.IsAssignableFrom(value.GetType())) { Func <PocoNode, object, object> convert = GetConversionFunction(value, paramNode); context.Log?.WriteLine($"-- WARNING: Couldn't instantiate {this.UnderlyingType.FullName} with argument '{paramNode.Parameter.Name}' of type {paramNode.Type.FullName} {((value == null) ? "to null" : $"with value of type '{value.GetType().FullName}'")}. Attempting conversion."); paramNode.ConvertFunction = convert; convertSet = true; } } if (convertSet) { return(Create(record, context)); } throw; } }
static Func <PocoNode, object, object> GetConversionFunction(object value, PocoNode node) { if (node.UnderlyingType == typeof(bool)) { if (value.GetType() == typeof(string)) { return(ConvertToBoolean); } } else if (node.UnderlyingType.IsEnum) { if (value.GetType() == typeof(string)) { return(ParseEnum); } return(CastToEnum); } return(ConvertTo); }
protected override Node CreateRootNode() { return(PocoNode.Root(this.type)); }
static object ConvertTo(PocoNode node, object value) { return(Convert.ChangeType(value, node.UnderlyingType, CultureInfo.InvariantCulture)); }
static object ParseEnum(PocoNode node, object value) { return(Enum.Parse(node.UnderlyingType, Convert.ToString(value, CultureInfo.InvariantCulture))); }
static object CastToEnum(PocoNode node, object value) { return(Enum.ToObject(node.UnderlyingType, value)); }
static object ConvertToBoolean(PocoNode node, object value) { return(Convert.ToBoolean(Convert.ToInt64(value, CultureInfo.InvariantCulture))); }
protected override Node CreateParameterNode(ParameterInfo paramInfo) { return(PocoNode.Root(paramInfo)); }
protected override Node CreateParameterNode(int columnOrdinal, ParameterInfo paramInfo) { return(PocoNode.Simple(columnOrdinal, paramInfo)); }