Life is wonderful, so I create my own favorite things, or take the good.
Sometimes code tired, repetitive things also more ... is not very beautiful, how to let oneself more cool quickly handy coding it ....
So the extension method also comes in.
One, the extension method describes the party:
Extension methods enable you to "add" methods to an existing type without creating a new derived type, recompiling, or otherwise modifying the original type. An extension method is a special static method, but it can be called just like an instance method on an extended type. For client code written in C # and Visual Basic, there is no significant difference between calling extension methods and calling methods that are actually defined in the type.
Extension methods are defined as static methods, but they are called through the instance method syntax. Their first argument specifies which type the method acts on, and the parameter is prefixed with the This modifier. Extension methods are only in scope when you explicitly import namespaces into the source code using a using directive.
You can use extension methods to extend a class or interface, but you cannot override extension methods. Extension methods that have the same name and signature as an interface or class method are never called. At compile time, the extension method's precedence is always lower than the instance method defined in the type itself. In other words, if a type has a method named process (int i), and you have an extension method with the same signature, the compiler always binds to that instance method. When the compiler encounters a method call, it first looks for a matching method in the instance method of that type. If no matching method is found, the compiler searches for any extension methods defined for the type, and binds to the first extension method it finds. The following example shows how the compiler determines which extension method or instance method to bind to.
Second, the case of demo datatable=>list<t>
1. Do not say that everyone only said I myself, usually in the project will get the data DataTable from the database, and then converted to the corresponding entity object list. "The field name of the entity and the DataTable column name are the corresponding"
Entity class:
Public classSendmsgmodel { Public intId {Get;Set; } PublicString Destnumber {Get;Set; } PublicString Content {Get;Set; } PublicString SmsId {Get;Set; } PublicString UserName {Get;Set; } PublicString Password {Get;Set; } }
Pre-Extension code:
Public classToobject {/// <summary> /// /// </summary> /// <typeparam name= "T" ></typeparam> /// <param name= "DT" ></param> /// <returns></returns> Public StaticList<t> list<t>(DataTable dt) {varList =NewList<t>(); Type T=typeof(T); varPlist =NewList<propertyinfo> (typeof(T). GetProperties ()); foreach(DataRow Iteminchdt. Rows) {T s= system.activator.createinstance<t>(); for(inti =0; i < dt. Columns.count; i++) {PropertyInfo info= plist. Find (p = = P.name = =dt. Columns[i]. ColumnName); if(Info! =NULL) { if(!Convert.isdbnull (Item[i])) {Info. SetValue (S, Item[i],NULL); }}} list. ADD (s); } returnlist; } }
Call:
DataTable dt= ODBC. Executedatatable (@ "select Top" Id, Destnumber , Content , SmsId , UserName , Password from Send"; var list=toobject.list<sendmsgmodel> (DT);
Post-Extension code:
/// <summary> ///Extension Classes/// </summary> Public Static classExtension {/// <summary> /// /// </summary> /// <typeparam name= "T" ></typeparam> /// <param name= "DT" ></param> /// <returns></returns> Public StaticList<t> list<t> ( ThisDataTable DT) { varList =NewList<t>(); Type T=typeof(T); varPlist =NewList<propertyinfo> (typeof(T). GetProperties ()); foreach(DataRow Iteminchdt. Rows) {T s= system.activator.createinstance<t>(); for(inti =0; i < dt. Columns.count; i++) {PropertyInfo info= plist. Find (p = = P.name = =dt. Columns[i]. ColumnName); if(Info! =NULL) { if(!Convert.isdbnull (Item[i])) {Info. SetValue (S, Item[i],NULL); }}} list. ADD (s); } returnlist; } }
Call:
DataTable dt= ODBC. Executedatatable (@ "select Top" Id, Destnumber , Content , SmsId , UserName , Password from Send"; var list2= dt. List<sendmsgmodel> ();
Third, Comparison
1. Former var list=toobject.list<sendmsgmodel> (DT);
2. after var list2= dt. list<sendmsgmodel> ();
Is it a big difference in style? I feel ha. The feeling of Linq?
Then continue to accumulate slowly, for performance has not been studied, it should be not bad.
JASONXUVIP Source: http://www.cnblogs.com/jasonxuvip/
Extension method DataTable to List<t>