Esempio n. 1
0
        public static object InitializeObject(object objObject, Type objType)
        {
            int intProperty = 0;

            // get properties for type
            ArrayList objProperties = GetPropertyInfo(objType);

            // initialize properties
            for (intProperty = 0; intProperty <= objProperties.Count - 1; intProperty++)
            {
                if (((PropertyInfo)objProperties[intProperty]).CanWrite)
                {
                    ((PropertyInfo)objProperties[intProperty]).SetValue(objObject, Null.SetNull((PropertyInfo)objProperties[intProperty]), null);
                }
            }
            return(objObject);
        }
Esempio n. 2
0
        private static object CreateObject(Type objType, IDataReader dr, ArrayList objProperties, int[] arrOrdinals)
        {
            object objObject   = Activator.CreateInstance(objType);
            int    intProperty = 0;

            // fill object with values from datareader
            for (intProperty = 0; intProperty <= objProperties.Count - 1; intProperty++)
            {
                if (((PropertyInfo)objProperties[intProperty]).CanWrite)
                {
                    if (arrOrdinals[intProperty] != -1)
                    {
                        if (dr.GetValue(arrOrdinals[intProperty]) == System.DBNull.Value)
                        {
                            // translate Null value
                            ((PropertyInfo)objProperties[intProperty]).SetValue(objObject, Null.SetNull((PropertyInfo)objProperties[intProperty]), null);
                        }
                        else
                        {
                            try
                            {
                                Type pType = ((PropertyInfo)objProperties[intProperty]).PropertyType;

                                switch (pType.FullName)
                                {
                                case "System.Enum":
                                    ((PropertyInfo)objProperties[intProperty]).SetValue(objObject, System.Enum.ToObject(pType, dr.GetValue(arrOrdinals[intProperty])), null);
                                    break;

                                case "System.String":
                                    ((PropertyInfo)objProperties[intProperty]).SetValue(objObject, dr.GetString(arrOrdinals[intProperty]), null);
                                    break;

                                case "System.Boolean":
                                    ((PropertyInfo)objProperties[intProperty]).SetValue(objObject, dr.GetBoolean(arrOrdinals[intProperty]), null);
                                    break;

                                case "System.Decimal":
                                    ((PropertyInfo)objProperties[intProperty]).SetValue(objObject, dr.GetDecimal(arrOrdinals[intProperty]), null);
                                    break;

                                case "System.Int16":
                                    ((PropertyInfo)objProperties[intProperty]).SetValue(objObject, dr.GetInt16(arrOrdinals[intProperty]), null);
                                    break;

                                case "System.Int32":
                                    ((PropertyInfo)objProperties[intProperty]).SetValue(objObject, dr.GetInt32(arrOrdinals[intProperty]), null);
                                    break;

                                case "System.Int64":
                                    ((PropertyInfo)objProperties[intProperty]).SetValue(objObject, dr.GetInt64(arrOrdinals[intProperty]), null);
                                    break;

                                case "System.DateTime":
                                    ((PropertyInfo)objProperties[intProperty]).SetValue(objObject, dr.GetDateTime(arrOrdinals[intProperty]), null);
                                    break;

                                case "System.Double":
                                    ((PropertyInfo)objProperties[intProperty]).SetValue(objObject, dr.GetDouble(arrOrdinals[intProperty]), null);
                                    break;

                                default:
                                    // try explicit conversion
                                    ((PropertyInfo)objProperties[intProperty]).SetValue(objObject, Convert.ChangeType(dr.GetValue(arrOrdinals[intProperty]), pType), null);
                                    break;
                                }
                            }
                            catch
                            {
                            }
                        }
                    }
                    else
                    {
                        // property does not exist in datareader
                        ((PropertyInfo)objProperties[intProperty]).SetValue(objObject, Null.SetNull((PropertyInfo)objProperties[intProperty]), null);
                    }
                }
            }


            return(objObject);
        }
Esempio n. 3
0
        private static object CreateObject(Type objType, DataRow dr, ArrayList objProperties)
        {
            object objObject       = Activator.CreateInstance(objType);
            int    intProperty     = 0;
            bool   hasDatarowColum = false;

            // fill object with values from datareader
            for (intProperty = 0; intProperty <= objProperties.Count - 1; intProperty++)
            {
                if (((PropertyInfo)objProperties[intProperty]).CanWrite)
                {
                    //kiem tra xem trong datarow co thuoc tinh nay khong
                    hasDatarowColum = false;
                    try
                    {
                        object obj = dr[((PropertyInfo)objProperties[intProperty]).Name];
                        hasDatarowColum = true;
                    }
                    catch
                    {
                        hasDatarowColum = false;
                    }
                    //ket thuc: kiem tra xem trong datarow co thuoc tinh nay khong

                    if (hasDatarowColum == false)
                    {
                        // translate Null value
                        ((PropertyInfo)objProperties[intProperty]).SetValue(objObject, Null.SetNull((PropertyInfo)objProperties[intProperty]), null);
                    }
                    else
                    {
                        try
                        {
                            // try implicit conversion first
                            ((PropertyInfo)objProperties[intProperty]).SetValue(objObject, dr[((PropertyInfo)objProperties[intProperty]).Name], null);
                        }
                        catch
                        {
                            // data types do not match
                            try
                            {
                                Type pType = ((PropertyInfo)objProperties[intProperty]).PropertyType;
                                //need to handle enumeration conversions differently than other base types
                                if (pType.BaseType.Equals(typeof(System.Enum)))
                                {
                                    ((PropertyInfo)objProperties[intProperty]).SetValue(objObject, System.Enum.ToObject(pType, dr[((PropertyInfo)objProperties[intProperty]).Name]), null);
                                }
                                else
                                {
                                    // try explicit conversion
                                    ((PropertyInfo)objProperties[intProperty]).SetValue(objObject, Convert.ChangeType(dr[((PropertyInfo)objProperties[intProperty]).Name], pType), null);
                                }
                            }
                            catch
                            {
                            }
                            // error assigning a datareader value to a property
                        }
                    }
                }
            }


            return(objObject);
        }
Esempio n. 4
0
        private static object CreateObjectFromDataSet(Type objType, DataRow dr, Hashtable objProperties, Hashtable arrOrdinals)
        {
            object objObject = Activator.CreateInstance(objType);


            // fill object with values from datareader
            string _fieldname = "";
            int    _possition = -1;

            foreach (DictionaryEntry de in arrOrdinals)
            {
                _fieldname = de.Key.ToString();
                _possition = (int)de.Value;

                if (((PropertyInfo)objProperties[_fieldname]).CanWrite)
                {
                    if (dr[_fieldname] == System.DBNull.Value)
                    {
                        // translate Null value
                        ((PropertyInfo)objProperties[_fieldname]).SetValue(objObject, Null.SetNull((PropertyInfo)objProperties[_fieldname]), null);
                    }
                    else
                    {
                        try
                        {
                            Type pType = ((PropertyInfo)objProperties[_fieldname]).PropertyType;

                            #region
                            switch (pType.FullName)
                            {
                            case "System.Enum":
                                ((PropertyInfo)objProperties[_fieldname]).SetValue(objObject, System.Enum.ToObject(pType, dr[_possition]), null);
                                break;

                            case "System.String":
                                ((PropertyInfo)objProperties[_fieldname]).SetValue(objObject, (string)dr[_possition], null);
                                break;

                            case "System.Boolean":
                                ((PropertyInfo)objProperties[_fieldname]).SetValue(objObject, (Boolean)dr[_possition], null);
                                break;

                            case "System.Decimal":
                                ((PropertyInfo)objProperties[_fieldname]).SetValue(objObject, Convert.ToDecimal(dr[_possition].ToString()), null);
                                break;

                            case "System.Int16":
                                ((PropertyInfo)objProperties[_fieldname]).SetValue(objObject, Convert.ToInt16(dr[_possition].ToString()), null);
                                break;

                            case "System.Int32":
                                ((PropertyInfo)objProperties[_fieldname]).SetValue(objObject, Convert.ToInt32(dr[_possition].ToString()), null);
                                break;

                            case "System.Int64":
                                ((PropertyInfo)objProperties[_fieldname]).SetValue(objObject, Convert.ToInt64(dr[_possition].ToString()), null);
                                break;

                            case "System.DateTime":
                                ((PropertyInfo)objProperties[_fieldname]).SetValue(objObject, (DateTime)dr[_possition], null);
                                break;

                            case "System.Double":
                                ((PropertyInfo)objProperties[_fieldname]).SetValue(objObject, (Double)dr[_possition], null);
                                break;

                            default:
                                // try explicit conversion
                                ((PropertyInfo)objProperties[_fieldname]).SetValue(objObject, Convert.ChangeType(dr[_possition], pType), null);
                                break;
                            }
                            #endregion
                        }
                        catch (Exception ex)
                        {
                            throw ex;
                        }
                    }
                }
            }

            return(objObject);
        }