#regionDataSet is swapped for generic collection/// <summary> ///take advantage of reflection and generics/// </summary> /// <param name= "DT" ></param> /// <returns></returns> Public StaticList<t> converttolist<t>(DataTable dt) {//Defining Collectionslist<t> ts =NewList<t>(); //get the type of this modelType type =typeof(T); //define a temporary variable stringTempname =string. Empty; //traverse all data rows in a DataTable foreach(DataRow Drinchdt. Rows) {T T= (T) activator.createinstance (typeof(T)); //get the public properties of this modelpropertyinfo[] Propertys =T.gettype (). GetProperties (); //Traverse All properties of this object foreach(PropertyInfo Piinchpropertys) {Tempname= Pi. Name;//Assigning a property name to a temporary variable//Check if DataTable contains this column (column name = = Property Name of object) if(dt. Columns.contains (Tempname)) {//determine if this property has a setter if(!pi. CanWrite)Continue;//This property is not writable and jumps directly out of//Take value ObjectValue =Dr[tempname]; //if not NULL, the property assigned to the object if(Value! =DBNull.Value)//Pi. SetValue (t, value, NULL);Pi. SetValue (t, Dynamicconvert (value, pi). PropertyType)); } } //object is added to the generic collectionts. ADD (t); } returnts; } /// <summary> ///Universal Conversion method to convert values to the target type whenever possible/// </summary> Public Static ObjectDynamicconvert (Objectvalue, type type) { if(Value = =NULL&& type. Isgenerictype)returnactivator.createinstance (type); if(Value = =NULL)return NULL; if(Type = = value.) GetType ())returnvalue; if(type. Isenum) {if(Value is string) returnEnum.parse (Type, value as string); Else returnenum.toobject (type, value); } if(!type. Isinterface &&type. Isgenerictype) {if(value.) GetType () = =typeof(string) && convert.tostring (value) = ="") { return NULL; } //DBNull returns null to go back if(Convert.isdbnull (value)) {return NULL; } Type Innertype= Type. GetGenericArguments () [0]; ObjectInnervalue =Dynamicconvert (value, Innertype); returnType. InvokeMember (NULL, Bindingflags.createinstance,NULL,NULL,New Object[] {innervalue}); } //if it is a value type, and the current value is an empty string, or DBNull, the default value is returned if(Type. Isvaluetype && (value. GetType () = =typeof(string) && convert.tostring (value) = ="") ||Convert.isdbnull (value)) { returnType. InvokeMember (NULL, Bindingflags.createinstance,NULL,NULL,New Object[] { }); } //if (value is string && type = = typeof (Guid)) return new Guid (value as String); //if (value is string && type = = typeof (Version)) return new Version (value as String); if(! (Value isIConvertible))returnvalue; returnConvert.changetype (value, type); } /// <summary> ///Universal Conversion method to convert values to the target type whenever possible/// </summary> Public StaticT dynamicconvert<t> (Objectvalue) {T result= (T) Dynamicconvert (value,typeof(T)); returnresult; } #endregion
DataSet is swapped for generic collection 222