文章详情

  • 游戏榜单
  • 软件榜单
关闭导航
热搜榜
热门下载
热门标签
php爱好者> php文档>ASP.NET:通过反射填充泛型集合List的静态方法_Asp.Net开发技巧

ASP.NET:通过反射填充泛型集合List的静态方法_Asp.Net开发技巧

时间:2010-09-15  来源:缘分星空

呃```花了一晚上时间,终于搞出来了如何通过反射,从DataReader将数据填充到数据实体泛型集合的静态方法.

 

//Kchen.Core.BaseBusinessObject为通用数据实体类,此处仅为限定T所继承的类型
        public static IList<T> FillDataListGeneric<T>(System.Data.IDataReader reader) where T : Kchen.Core.BaseBusinessObject
        {
            //实例化一个List<>泛型集合
            IList<T> DataList = new List<T>();
            while (reader.Read())
            {
                //由于是是未知的类型,所以必须通过Activator.CreateInstance<T>()方法来依据T的类型动态创建数据实体对象
                T RowInstance = Activator.CreateInstance<T>();
                //通过反射取得对象所有的Property
                foreach (PropertyInfo Property in typeof(T).GetProperties())
                {
                    //BindingFieldAttribute为自定义的Attribute,用于与数据库字段进行绑定
                    foreach (BindingFieldAttribute FieldAttr in Property.GetCustomAttributes(typeof(BindingFieldAttribute), true))
                    {
                        try
                        {
                            //取得当前数据库字段的顺序
                            int Ordinal = reader.GetOrdinal(FieldAttr.FieldName);
                            if (reader.GetValue(Ordinal) != DBNull.Value)
                            {
                                //将DataReader读取出来的数据填充到对象实体的属性里
                                Property.SetValue(RowInstance, Convert.ChangeType(reader.GetValue(Ordinal), Property.PropertyType), null);
                            }
                        }
                        catch
                        {
                            break;
                        }
                    }
                }
                //将数据实体对象add到泛型集合中
                DataList.Add(RowInstance);
            }
            return DataList;
        }
调用的时候使用如下代码

            //伪代码 OleDbDataReader _ds = 创建一个OleDbDataReader
            IList<Product> _result = Kchen.Utilities.FillDataListGeneric<Product>(_ds);

此静态方法通过一个实体类型和DateReader,快速的将数据填充到数据实体泛型集合中.

http://www.cnblogs.com/kchen/archive/2006/10/31/545011.html


north face brandssimple halloween craftsblack leather vestcarnival cruise gratuitiesbest luggage of 2008picture id tagscustom wedding cake toppers photosstrawberry leathercolorations liquid watercolorvintage wall decorbraiding leatherfreeze brand horseanna suijessica simpson handbags macywall decorwholesale leather walletcontent travelpro crew 7jobs cruise ships canadamake paper shopping bagcelebrity cruise lines jobshilarious baby jokesdiscount victorinox luggagejustin lace up bootszuca pro reviewwhitetail deer hidesdisney cruises december 2009jeff foxworthysinger embroidery machinedesigner purses namesjunior winter jacketbarney live ticketssingles vacation mexicoclear plastic drawstring bagscoach french purselast minute cruise tickergraphite manufacturingspring summer 2011best southern caribbean cruisetall bearded iris flowerswatercolor painting lessonsdurable luggage setstrevivacation home rentals hamptonsholland america singles cruisessuit luggage carrierabstract art tattooscanvas photo paperdiane von furstenberg productsantique samuraitote hobo handbaglouis vuitton monogram vernis almalost series 6 posterdiscount regent cruisespop nuttravel luggage tagsarmani suitoil portrait artistsskyway decade luggagelast day cruisesstainless steel screwswomen tuxedo vestshello kitty sewing machinecarnival spirit menusladies winter coatshistory lost tv showgypsum wikilets rewind asia cruise lyricsdiscount ikeahooded vestmini leather skirtleather stamp embossing stamping toolleather driving gloves reviewsacceptable size of carry on luggagebeach condo rentalsblue designer bagsbetter homes propertiessimplicity embroidery machinefurniture upholstery suppliescosta fortuna cruise reviewspashmina scarf tyingbcbg springroyal caribbean cruise line mexicohabitat conservationkeepall graphitethomson cruise reviewsanimal print bath towelsclearance sale handbagsspinning rolling luggagebuy versace onlineN41540wheeled duffle bagsgouache brushesisland cruise dealscoach signature stripe handbagswhite gucci bagsrental property softwarecruise vacation insurancewenger luggage setpicture mini skirtspring 2009 fashionbeach house accommodationwinter schedule 2009south beach apartmentspassy mapauto plastic fastener suppliersleather artistcheap custom beach towelsleather laptop messenger bagsweetbox the next generation 2009 downloadbeach vacation floridarivnut gunroyal caribbean christmas cruisetravel concepts ur gear ss02buy flat walletsmiley cyrus bikinileather vest accessoriesgraphite clubsvacation lyricstravel cruise specialsmens pea coatwestern mediterranean cruises 2010wedding stampsbath sheets salejessica simpson orange pursearmani blackdesigner forever 21clinton hotel pricesprada fallsew your own handbaggianni versace fashiondisney world cruise vacationsmonogram handkerchiefmens fabric walletpaint gouachemiu asossale handbagsgrand rental stationoriginal coach bagsleather working accessorieswinter wool coats womenlouis vuitton biographyGucci-181511Tcarnival suitespersonalized cello bagshandmade leatherGUCCI-218491-A261G-2703cole sprouse pictureshouse painting techniquesbelize tours for cruise passengersvintage clutch purse

Replica lv Hangbags

Replica Hangbags

Hangbags

replica handbags

replica handbags

replica handbags

replica handbags

handbags

replica handbags replica handbags replica handbags replica handbags replica handbags
相关阅读 更多 +
排行榜 更多 +
辰域智控app

辰域智控app

系统工具 下载
网医联盟app

网医联盟app

运动健身 下载
汇丰汇选App

汇丰汇选App

金融理财 下载