Exemple #1
0
        public static List <BaseEntry> GetValue(object key, Type clazzType)
        {
            var classAttribute =
                (ClassDescribeAttribute)
                Attribute.GetCustomAttribute(clazzType, typeof(ClassDescribeAttribute));
            var sql            = string.Format("SELECT * FROM {0} WHERE {1}=?", classAttribute.Table, classAttribute.Key);
            var odbcParameters = new List <MOdbcParameter>();
            var odbcParameter  = new MOdbcParameter
            {
                Name  = classAttribute.Key,
                OType = classAttribute.MOdbcType,
                Value = key
            };

            odbcParameters.Add(odbcParameter);

            var dataTable = GetDataTable(sql, odbcParameters);

            var productTrees = new List <BaseEntry>();

            foreach (DataRow row in dataTable.Rows)
            {
                var productTree = Activator.CreateInstance(clazzType, true) as BaseEntry;
                foreach (var info in clazzType.GetProperties())
                {
                    var propertyAttribute =
                        info.GetCustomAttributes(typeof(PropertyAttribute), false)
                        .FirstOrDefault() as
                        PropertyAttribute;
                    if (propertyAttribute != null)
                    {
                        var columnName = "";
                        foreach (DataColumn column in dataTable.Columns)
                        {
                            if (info.Name.Equals(column.ColumnName.Replace("U_", "")))
                            {
                                columnName = propertyAttribute.ColumnName;
                            }
                        }
                        if (!string.IsNullOrEmpty(columnName))
                        {
                            var obj = row[columnName];
                            if (dataTable.Columns[columnName].DataType == typeof(decimal))
                            {
                                obj = string.IsNullOrEmpty(obj.ToString()) ? 0.0 : double.Parse(obj.ToString());
                            }

                            info.SetValue(productTree, obj, null);
                        }
                        else
                        {
                            foreach (var type in Assembly.GetTypes())
                            {
                                if (type == propertyAttribute.DataType)
                                {
                                    var productTreeLine = Activator.CreateInstance(type, true) as BaseEntry;
                                    if (productTreeLine != null)
                                    {
                                        info.SetValue(productTree, productTreeLine.GetValue(key).ToArray(), null);
                                    }
                                    break;
                                }
                            }
                        }
                    }
                }
                productTrees.Add(productTree);
            }
            return(productTrees);
        }
Exemple #2
0
        protected new List <object> GetValue(object key)
        {
            var classAttribute =
                (ClassDescribeAttribute)
                Attribute.GetCustomAttribute(typeof(ProductTree), typeof(ClassDescribeAttribute));
            var sql = string.Format("SELECT * FROM {0} WHERE {1}=?", classAttribute.Table, classAttribute.Key);

            var odbcParameters = new List <MOdbcParameter>();
            var odbcParameter  = new MOdbcParameter
            {
                Name  = classAttribute.Key,
                OType = classAttribute.MOdbcType,
                Value = key
            };

            odbcParameters.Add(odbcParameter);

            var dataTable = CommonHelper.GetDataTable(sql, odbcParameters);

            var productTrees = new List <object>();

            foreach (DataRow row in dataTable.Rows)
            {
                var productTree = new ProductTree();
                foreach (var info in typeof(ProductTree).GetProperties())
                {
                    var propertyAttribute =
                        info.GetCustomAttributes(typeof(PropertyAttribute), false)
                        .FirstOrDefault() as
                        PropertyAttribute;
                    if (propertyAttribute != null)
                    {
                        var columnName = "";
                        foreach (DataColumn column in dataTable.Columns)
                        {
                            if (info.Name.Equals(column.ColumnName.Replace("U_", "")))
                            {
                                columnName = propertyAttribute.ColumnName;
                            }
                        }
                        if (!string.IsNullOrEmpty(columnName))
                        {
                            info.SetValue(productTree, row[columnName], null);
                        }
                        else
                        {
                            var assembly = Assembly.GetExecutingAssembly();
                            foreach (var type in assembly.GetTypes())
                            {
                                if (type == propertyAttribute.DataType)
                                {
                                    var productTreeLine = Activator.CreateInstance(type, true) as BaseEntry;
                                    if (productTreeLine != null)
                                    {
                                        info.SetValue(productTree, productTreeLine.GetValue(key).ToArray(), null);
                                    }
                                    break;
                                }
                            }
                        }
                    }
                }
                productTrees.Add(productTree);
            }
            return(productTrees);
        }