Farseer.net lightweight open source framework intermediate: common extension methods

Source: Internet
Author: User

Farseer.net lightweight open source framework intermediate: common extension methods
When using the framework, you must reference the namespace of the Extension Method: using FS. extend; copy code 1 // <summary> 2 // convert the value to the value of the type object (this method is a public call and only supports single-value conversion) 3 /// </summary> 4 /// <param name = "objValue"> value to be converted </param> 5 /// <param name = "objType"> value type to be converted </param> 6 // <param name = "defType"> when the conversion fails, default Value Type </param> 7 // <returns> </returns> 8 private static object ConvertType (object objValue, Type objType, Type defType) when the author of The copied code writes the code, the most common method is ConvertType. Any basic type can be transferred to the new basic type. For example, convert "1" to 1, and convert "true" to true. It is too practical. 1 /// <summary> 2 /// when the result is True, output parameter 3 /// </summary> 4 /// <param name = "B"> determine the source result </param> 5 /// <param name = "t"> output value </param> 6 public static T IsTrue <T> (this bool B, T t) if it is true, the input result is returned. Can Replace: true? "Xxxx. Copy code 1 // <summary> 2 // obtain Chinese 3 // </summary> 4 // <param name = "B"> </param> 5 /// <param name = "strTrue"> Chinese when the value is True: yes </param> 6 // <param name = "strFalse"> Chinese when the value is False: no </param> 7 // <returns> </returns> 8 public static string GetName (this bool B, string strTrue = "yes", string strFalse = "no ") when we copy the code to get the bool result, sometimes we want to: true? "Yes": "wrong. 1 /// <summary> 2 /// sort DataTable 3 /// </summary> 4 /// <param name = "dt"> table to be sorted </ param> 5 // <param name = "sort"> field to be sorted </param> 6 public static DataTable Sort (this DataTable dt, string sort = "id desc") Is it difficult for a friend who often operates on a able to sort it? The cloned result is used here. Copy code 1 // <summary> 2 // pagination to DataTable 3 /// </summary> 4 // <param name = "dt"> source table </ param> 5 // <param name = "pageSize"> Number of records displayed per page </param> 6 /// <param name = "pageIndex"> page number </param> 7 // <returns> </returns> 8 public static DataTable Split (this DataTable dt, int pageSize = 20, int pageIndex = 1) it is also very practical to copy the code to the able page. 1 /// <summary> 2 // DataTable converted to object class 3 /// </summary> 4 /// <param name = "dt"> source DataTable </ param> 5 // <typeparam name = "T"> entity class </typeparam> 6 public static List <TResult> ToList <TResult> (this DataTable dt) where TResult: ModelInfo, new () converts DataTable to List <entity>. 1 // <summary> 2 // obtain the enumerated Chinese 3 /// </summary> 4 // <param name = "eum"> enumerated value </param> 5 public static string GetName (this Enum eum) 1 // <summary> 2 // obtain the enumerated list 3 /// </summary> 4 public static Dictionary <int, string> ToDictionary (this Type enumType) you can convert all the enumerated elements into a dictionary to copy code /// <summary> /// IDataReader to an object class /// </summary> /// <param name = "reader "> source IDataReader </param> // <typeparam name =" T "> entity class </typ Eparam> public static List <T> ToList <T> (this IDataReader reader) where T: modelInfo /// <summary> /// data filling /// </summary> /// <param name = "reader"> source IDataReader </param> /// <typeparam name = "T"> entity class </typeparam> public static T ToInfo <T> (this IDataReader reader) where T: ModelInfo, new () copy code to convert IDataReader to List <entity> or copy code 1 // <summary> 2 // data pagination 3 /// </summary> 4 // <typeparam name = "TSource"> entity </Typeparam> 5 // <param name = "source"> source object </param> 6 /// <param name = "pageIndex"> page number </param> 7 // <param name = "pageSize"> size of each page </param> 8 public static IQueryable <TSource> Split <TSource> (this IQueryable <TSource> source, int pageSize = 20, int pageIndex = 1) copy the code to paging List 1 /// <summary> 2 /// convert List to string 3 /// </summary> 4 /// <param name = "lst "> LIST </param> 5 // <param name =" sign "> separator </param> 6 public Static string ToString (this IEnumerable lst, string sign = ",") concatenates List <T> into a string based on the specified delimiter. For example, "1, 2, 3, 4 "1 // <summary> 2 // determine whether a record exists. 3 // </summary> 4 // <param name =" IDs "> condition, equivalent to: o => IDs. contains (o. </param> 5 public static bool IsHaving <TInfo> (this IEnumerable <TInfo> lst, List <int> IDs) where TInfo: ModelInfo, new () check whether the copy code 1 is available. /// <summary> 2 // field selector 3 /// </summary> 4 /// <param name = "select"> field selector </param> 5 // <param name = "IDs"> condition, equivalent to: o => IDs. contains (o. operation </param> 6 /// <Param name = "lst"> List </param> 7 public static List <T> ToSelectList <TInfo, T> (this IEnumerable <TInfo> lst, Func <TInfo, t> select) copy the code to project the list of specified elements. 1 /// <summary> 2 /// convert the collection class to ableable3 /// </summary> 4 /// <param name = "list"> set </param> 5 /// <returns> </returns> 6 public static DataTable ToDataTable (this IList list) list <entity> to DataTable copy code 1 // <summary> 2 // convert any array to a string connected with a symbol 3 /// </summary> 4 // /<param name = "obj"> Any object </param> 5 // <param name = "func"> specifies the method to be executed during the conversion. </ param> 6 /// <param name = "sign"> separator </param> 7 /// <typeparam name = "T"> basic object </t Ypeparam> 8 public static string ToString <T> (this T [] obj, string sign = ",", Func <T, string> func = null) copy the code to concatenate the array T [] into a string based on the specified separator. For example, "1, 2, 3, 4 "Copy code 1 // <summary> 2 // directly return the separated element 3 /// </summary> 4 /// <param name =" arr "> the string to be separated </param> 5 // <param name =" tag "> the separator </param> 6 /// <param name =" index"> element subscript </param> 7 // <returns> </returns> 8 public static string Split (this string arr, string tag, int index) the copy code is separated by a specified string, which is equivalent to "xxxx; xxx ;". split (";") [index] Copy code 1 // <summary> 2 // specify the content of the tag to be cleared 3 /// </summary> 4 /// <param name = "str"> content </par Am> 5 /// <param name = "tag"> tag </param> 6 /// <param name = "options"> Option </param> 7 public static string clearString (this string str, string tag, RegexOptions options = RegexOptions. none) copy the code and use regular expressions to clear the matching strings. Copy code 1 // <summary> 2 // convert string to List type 3 /// </summary> 4 // <param name = "str"> conversion string </param> 5 // <param name = "splitString"> when the separator is NullOrEmpty, char </param> 6 // <param name = "defValue"> default value (if a single conversion fails, the default value is NullOrEmpty, otherwise, use the default value) </param> 7 // <typeparam name = "T"> basic type </typeparam> 8 public static List <T> ToList <T> (this string str, T defValue, string splitString = ",") copy the code to convert the string to a List with the specified separator, for example, "1, 2 "., 3, 4, 5 "separated by, into List <int> 1 // <summary> 2 // Delete the specified final string (until it is found) 3 /// </summary> 4 /// <param name = "str"> string to be converted </param> 5 /// <param name = "strChar"> string to be deleted </param> 6 public static string DelLastOf (this string str, string strChar. Copy code 1 // <summary> 2 // compare whether the two are equal, case insensitive, space on both sides 3 /// </summary> 4 /// <param name = "str"> comparison 1 </param> 5 /// <param name = "str2"> comparison 2 </param> 6 // <returns> </returns> 7 public static bool IsEquals (this string str, string str2) copy the code to compare whether the two are equal, case-insensitive, duplicate code 1 with spaces on both sides /// <summary> 2 /// whether it is not Null or Empty 3 /// </summary> 4 /// <param name = "str"> string to be judged </param> 5 public static bool IsHaving (this string str) 6 7 // <su Mmary> 8 /// whether it is Null or Empty 9 /// </summary> 10 /// <param name = "str"> string to be judged </param> 11 public static bool IsNullOrEmpty (this string str) copy the code to determine if the value is null. Copy code 1 /// <summary> 2 /// compare whether the start character is consistent. 3 /// </summary> 4 /// <param name = "str"> original string </param> 5 // <param name = "value"> string to be compared </param> 6 /// <returns> </returns> 7 public static bool IsStartsWith (this string str, string value) 8 9 // <summary> 10 // Is the same as the start character 11? // </summary> 12 // <param name = "str"> original string </param> 13 /// <param name =" value "> the string to be compared </param> 14 // <returns> </returns> 15 public static bool IsEndsWith (this string str, string value) duplicate code is case-insensitive copy code 1 /// <summary> 2 /// separator string 3 /// </summary> 4 /// <param name = "str"> string </param> 5 /// <param name = "splitString"> separator number </param> 6 /// <returns> </returns> 7 public static string [] split (this string s Tr, string splitString = ",") the copy Code supports string separator 1 // <summary> 2 // when NullOrEmpty, replace it with a new string; otherwise, use the original one. 3 /// </summary> 4 /// <param name = "str"> value to be detected </param> 5 /// <param name = "newString"> new string to be replaced </param> 6 public static string WhileNullOrEmpty (this string str, string newString) When NullOrEmpty, replace it with a new string; otherwise, use the original string. Most of the time, some values are null, but we want it to be NULL by default. If it is not NULL, ignore it. 1 // <summary> 2 // Format the number and convert it to 1000,103 /// </summary> 4 public static string Format (this int number, bool isHaveTag = true, int len = 2) convert to the money format.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.