Esempio n. 1
0
        static Action <IDataReader, object> CreateSetValueAction(PropertyInfo info, Type columnType, string columnName)
        {
            Type propType = info.PropertyType;

            Action <IDataReader, object> SetValueAction;

            if (propType.BaseType.Equals(typeof(System.Enum)))
            {
                SetValueAction = (IDataReader dr, object obj) =>
                {
                    if (MappingHelper.IsNumeric(dr[columnName]))
                    {
                        info.SetValue(obj, System.Enum.ToObject(propType, Convert.ToInt32(dr[columnName])), null);
                    }
                    else
                    {
                        info.SetValue(obj, System.Enum.ToObject(propType, dr[columnName]), null);
                    }
                };
            }
            else if (propType.Equals(columnType))
            {
                SetValueAction = (IDataReader dr, object obj) =>
                {
                    info.SetValue(obj, dr[columnName], null);
                };
            }
            else
            {
                //different types
                SetValueAction = (IDataReader dr, object obj) =>
                {
                    info.SetValue(obj, Convert.ChangeType(dr[columnName], propType), null);
                };
            }

            return((IDataReader dr, object obj) =>
            {
                if (Convert.IsDBNull(dr[columnName]))
                {
                    info.SetValue(obj, MappingNull.GetNull(info), null);
                }
                else
                {
                    SetValueAction.Invoke(dr, obj);
                }
            });
        }
Esempio n. 2
0
        //public void RecursiveProperies(Type type)
        //{
        //    foreach (PropertyInfo info in type.GetProperties())
        //    {
        //        if (info.PropertyType.Namespace != "System")
        //        {
        //            _propertyInfoCache.Add(type.FullName, type.GetProperties());
        //        }
        //    }
        //}

        public static void FillParameters <T>(object obj, IDbCommand command)
        {
            List <PropertyInfo> properties = GetPropertyInfo <T>();

            foreach (IDbDataParameter param in command.Parameters)
            {
                //find the right property
                PropertyInfo property = properties.Find(delegate(PropertyInfo p)
                {
                    return(p.Name.ToLower() == param.SourceColumn.ToLower());
                });

                object value = property.GetValue(obj, null);

                if (MappingNull.IsNull(value))
                {
                    param.Value = DBNull.Value;
                }
                else
                {
                    param.Value = value;
                }
            }
        }