扩展方法

static public class DataRowExtension
{
  public static string GetString(this DataRow row, string ColumnName)
  {
    if (row[ColumnName] == DBNull.Value)
    {
      return "";
    }
    else
    {
      return row[ColumnName].ToString();
    }

    }
}

使用方法如下:哪个可读性最好呢,应该是第三个吧,就这是扩展方法的使用,

m.CustomerName = GetString(theRow["CustomerName"]);
m.CustomerName = DataRowExtension.GetString(theRow, "CustomerName");
m.CustomerName = theRow.GetString("CustomerName");

原文地址:https://www.cnblogs.com/LongHuaiYu/p/5124503.html