Пример #1
0
        public void GetPropertyTest()
        {
            PropertyClass propertyClass = new PropertyClass();

            PropertyInfo actual = PropertyFinder <PropertyClass> .Find(x => x.GetMe);

            PropertyInfo expected = propertyClass.GetType().GetProperty("GetMe");

            Assert.AreEqual(expected, actual);
        }
Пример #2
0
        /// <summary>
        ///   加载存货信息(存货代码、存货名称)到List泛型
        /// </summary>
        /// <returns> 包含存货信息的List泛型列表 </returns>
        private List <PropertyClass> LoadInven()
        {
            PropertyClass proCla      = new PropertyClass();               //实例化PropertyClass类
            Type          elemnetType = proCla.GetType();                  //获取proCla的Type

            PropertyInfo[] publicProperties = elemnetType.GetProperties(); //得到实例proCla的Type所拥有的所有公共属性
            //得到存货信息,该存货信息为DataTable类型
            DataTable dt =
                db.GetDataSet("select InvenCode,InvenName ,SpecsModel from BSInven", "BSInven").Tables["BSInven"];
            List <PropertyClass> tempProperties = new List <PropertyClass>(); //实例化List<PropertyClass>泛型类

            //逐行读取得到的存货信息
            foreach (DataRow row in dt.Rows)
            {
                //循环数据源dt中的所有列
                for (int i = 0; i < dt.Columns.Count; i++)
                {
                    //循环实例proCla的Type拥有的所有公共属性
                    for (int j = 0; j < publicProperties.Length; j++)
                    {
                        //如果数据源dt中指定的列名称与某个公共属性的名称相同
                        if (dt.Columns[i].ColumnName == publicProperties[j].Name)
                        {
                            if (Convert.IsDBNull(row[i]))                       //如果数据源dt中的当前数据项为null
                            {
                                continue;                                       //则终止本次循环,进入下一次循环
                            }
                            publicProperties[j].SetValue(proCla, row[i], null); //为当前实例proCla中的某个属性赋值,该值为row[i]
                        }
                    }
                }

                tempProperties.Add(proCla);   //向泛型列表tempProperties中添加PropertyClass类型的元素,即PropertyClass类的实例proCla
                proCla = new PropertyClass(); //重新创建PropertyClass类的实例
            }

            return(tempProperties); //返回包含存货信息的List泛型列表
        }