Exemplo n.º 1
0
        public static PocoNode Simple(int columnOrdinal, PropertyInfo property)
        {
            var node = new PocoNode(property)
            {
                _ColumnOrdinal = columnOrdinal
            };

            return(node);
        }
Exemplo n.º 2
0
        public static PocoNode Complex(PropertyInfo property)
        {
            var node = new PocoNode(property)
            {
                _IsComplex = true,
            };

            return(node);
        }
Exemplo n.º 3
0
        public static PocoNode Root(Type type)
        {
            var node = new PocoNode(type)
            {
                _IsComplex = true,
            };

            return(node);
        }
Exemplo n.º 4
0
        public static PocoNode Simple(int columnOrdinal, ParameterInfo parameter)
        {
            var node = new PocoNode(parameter.ParameterType)
            {
                _ColumnOrdinal = columnOrdinal,
                Parameter      = parameter
            };

            return(node);
        }
Exemplo n.º 5
0
        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));
        }
Exemplo n.º 6
0
        protected override Node CreateComplexProperty(Node container, string propertyName)
        {
            PropertyInfo property = GetProperty(((PocoNode)container).UnderlyingType, propertyName);

            if (property == null)
            {
                return(null);
            }

            return(PocoNode.Complex(property));
        }
Exemplo n.º 7
0
        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;
            }
        }
Exemplo n.º 8
0
        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);
        }
Exemplo n.º 9
0
 protected override Node CreateRootNode()
 {
     return(PocoNode.Root(this.type));
 }
Exemplo n.º 10
0
 static object ConvertTo(PocoNode node, object value)
 {
     return(Convert.ChangeType(value, node.UnderlyingType, CultureInfo.InvariantCulture));
 }
Exemplo n.º 11
0
 static object ParseEnum(PocoNode node, object value)
 {
     return(Enum.Parse(node.UnderlyingType, Convert.ToString(value, CultureInfo.InvariantCulture)));
 }
Exemplo n.º 12
0
 static object CastToEnum(PocoNode node, object value)
 {
     return(Enum.ToObject(node.UnderlyingType, value));
 }
Exemplo n.º 13
0
 static object ConvertToBoolean(PocoNode node, object value)
 {
     return(Convert.ToBoolean(Convert.ToInt64(value, CultureInfo.InvariantCulture)));
 }
Exemplo n.º 14
0
 protected override Node CreateParameterNode(ParameterInfo paramInfo)
 {
     return(PocoNode.Root(paramInfo));
 }
Exemplo n.º 15
0
 protected override Node CreateParameterNode(int columnOrdinal, ParameterInfo paramInfo)
 {
     return(PocoNode.Simple(columnOrdinal, paramInfo));
 }