SoFunction
Updated on 2025-03-01

TypeParse of C#, general extension tools

usage:

var int1 = "2".TryToInt();//Failed to int, return 0var int2 = "2x".TryToInt();
var int3 = "2".TryToInt(1);//Failed to int return 1var int4 = "2x".TryToInt(1);
 
 
var d1 = "2".TryToMoney(); //Same as abovevar d2 = "2x".TryToMoney();
var d3 = "2".TryToMoney(1);
var d4 = "2x".TryToMoney(1);
 
string a = null;
var s1 = ();
var s3 = ("1");
 
 
var d11 = "2".TryToDecimal();
var d22 = "2x".TryToDecimal();
var d33 = "2".TryToDecimal(1);
var d44 = "2x".TryToDecimal(1);
 
 
var de1 = "2013-1-1".TryToDate();
var de2 = "x2013-1-1".TryToDate();
var de3 = "x2013-1-1".TryToDate();
 
 
//Json and model conversionvar json = new { id = 1 }.ModelToJson();
var model = "{id:1}".JsonToModel<ModelTest>();
 
 
//Conversion of list and dataTablevar dt = new List<ModelTest>().ListToDataTable();
var list = <ModelTest>();

Code:

using System;
using ;
using ;
using ;
using ;
using ;
using ;
using ;
 
namespace SyntacticSugar
{
  /// <summary>
  /// ** Description: Type conversion  /// ** Founding time: 2015-6-2  /// ** Modification time:-  /// ** Author: sunkaixuan  /// ** Instructions for use:  /// </summary>
  public static class TypeParseExtenions
  {
    #region Force to int If it fails, return 0    /// <summary>
    /// Forced to int If it fails, return 0    /// </summary>
    /// <param name="thisValue"></param>
    /// <param name="i"></param>
    /// <returns></returns>
    public static int TryToInt(this object thisValue)
    {
      int reval = 0;
      if (thisValue != null && ((), out reval))
      {
        return reval;
      }
      return reval;
    }
    #endregion
    #region Force to int If it fails, return errorValue    /// <summary>
    /// Forced to int If it fails, return errorValue    /// </summary>
    /// <param name="thisValue"></param>
    /// <param name="i"></param>
    /// <returns></returns>
    public static int TryToInt(this object thisValue, int errorValue)
    {
      int reval = 0;
      if (thisValue != null && ((), out reval))
      {
        return reval;
      }
      return errorValue;
    }
    #endregion
    #region Forced to double If it fails, return 0    /// <summary>
    /// Forced to money if it fails and returns 0    /// </summary>
    /// <param name="thisValue"></param>
    /// <param name="i"></param>
    /// <returns></returns>
    public static double TryToMoney(this object thisValue)
    {
      double reval = 0;
      if (thisValue != null && ((), out reval))
      {
        return reval;
      }
      return 0;
    }
    #endregion
    #region Force to double If it fails, return errorValue    /// <summary>
    /// Forced to double if failed to return errorValue    /// </summary>
    /// <param name="thisValue"></param>
    /// <param name="errorValue"></param>
    /// <returns></returns>
    public static double TryToMoney(this object thisValue, int errorValue)
    {
      double reval = 0;
      if (thisValue != null && ((), out reval))
      {
        return reval;
      }
      return errorValue;
    }
    #endregion
    #region Force to string If it fails, return ""    /// <summary>
    /// Forced to string if it fails to return ""    /// </summary>
    /// <param name="thisValue"></param>
    /// <param name="i"></param>
    /// <returns></returns>
    public static string TryToString(this object thisValue)
    {
      if (thisValue != null) return ().Trim();
      return "";
    }
    #endregion
    #region Force to string If it fails, return errorValue    /// <summary>
    /// Forced to string if it fails to return to str    /// </summary>
    /// <param name="thisValue"></param>
    /// <param name="errorValue"></param>
    /// <returns></returns>
    public static string TryToString(this object thisValue, string errorValue)
    {
      if (thisValue != null) return ().Trim();
      return errorValue;
    }
    #endregion
    #region Forced to Decimal If it fails, return 0    /// <summary>
    /// Forced to Decimal if it fails to return 0    /// </summary>
    /// <param name="thisValue"></param>
    /// <param name="i"></param>
    /// <returns></returns>
    public static Decimal TryToDecimal(this object thisValue)
    {
      Decimal reval = 0;
      if (thisValue != null && ((), out reval))
      {
        return reval;
      }
      return 0;
    }
    #endregion
    #region Force to Decimal If it fails, return errorValue    /// <summary>
    /// Forced to Decimal if it fails to return errorValue    /// </summary>
    /// <param name="thisValue"></param>
    /// <param name="errorValue"></param>
    /// <returns></returns>
    public static Decimal TryToDecimal(this object thisValue, int errorValue)
    {
      Decimal reval = 0;
      if (thisValue != null && ((), out reval))
      {
        return reval;
      }
      return errorValue;
    }
    #endregion
    #region Force to DateTime If it fails    /// <summary>
    /// Forced to DateTime if it fails    /// </summary>
    /// <param name="thisValue"></param>
    /// <param name="i"></param>
    /// <returns></returns>
    public static DateTime TryToDate(this object thisValue)
    {
      DateTime reval = ;
      if (thisValue != null && ((), out reval))
      {
        return reval;
      }
      return reval;
    }
    #endregion
    #region Force to DateTime If it fails, return errorValue    /// <summary>
    /// Forced to DateTime if it fails to return errorValue    /// </summary>
    /// <param name="thisValue"></param>
    /// <param name="errorValue"></param>
    /// <returns></returns>
    public static DateTime TryToDate(this object thisValue, DateTime errorValue)
    {
      DateTime reval = ;
      if (thisValue != null && ((), out reval))
      {
        return reval;
      }
      return errorValue;
    }
    #endregion
 
    #region json conversion    /// <summary>
    /// Serialize json to entity    /// </summary>
    /// <typeparam name="TEntity"></typeparam>
    /// <param name="json"></param>
    /// <returns></returns>
    public static TEntity JsonToModel<TEntity>(this string json)
    {
      JavaScriptSerializer jsSerializer = new JavaScriptSerializer();
      return <TEntity>(json);
    }
    /// <summary>
    /// Serialize entity to json    /// </summary>
    /// <param name="model"></param>
    /// <returns></returns>
    public static string ModelToJson<T>(this T model)
    {
      JavaScriptSerializer jsSerializer = new JavaScriptSerializer();
      return (model);
    }
 
    #endregion
 
    #region DataTable List
    /// <summary>
    /// Convert collection class to DataTable    /// </summary>
    /// <param name="list">collection</param>    /// &lt;returns&gt;&lt;/returns&gt;
    public static DataTable ListToDataTable&lt;T&gt;(this List&lt;T&gt; list)
    {
      DataTable result = new DataTable();
      if ( &gt; 0)
      {
        PropertyInfo[] propertys = typeof(T).GetProperties();
        foreach (PropertyInfo pi in propertys)
        {
          (, );
        }
 
        for (int i = 0; i &lt; ; i++)
        {
          ArrayList tempList = new ArrayList();
          foreach (PropertyInfo pi in propertys)
          {
            object obj = (list[i], null);
            if (obj != null &amp;&amp; obj != )
              (obj);
          }
          object[] array = ();
          (array, true);
        }
      }
      return result;
    }
    /// &lt;summary&gt;
    /// Convert datatable to list    /// &lt;/summary&gt;
    /// &lt;typeparam name="T"&gt;&lt;/typeparam&gt;
    /// &lt;param name="dt"&gt;&lt;/param&gt;
    /// &lt;returns&gt;&lt;/returns&gt;
    public static List&lt;T&gt; DataTableToList&lt;T&gt;(this DataTable dt)
    {
      var list = new List&lt;T&gt;();
      Type t = typeof(T);
      var plist = new List&lt;PropertyInfo&gt;(typeof(T).GetProperties());
 
      foreach (DataRow item in )
      {
        T s = &lt;T&gt;();
        for (int i = 0; i &lt; ; i++)
        {
          PropertyInfo info = (p =&gt;  == [i].ColumnName);
          if (info != null)
          {
            if (!(item[i]))
            {
              (s, item[i], null);
            }
          }
        }
        (s);
      }
      return list;
    }
    #endregion
 
  }
}