寫了很多GetModelByFilter(string strFilter)方法,發現函數體內除了紅色那兩句(實例化)不同外,別的都一
樣。看過設計模式,了解反射,但不知道在這裡怎麼利用它來實現把重復代碼抽象出來。
謝謝大家啦!
public static IList<Model.Tccom010Info> GetModelByFilter(string strFilter)
{
IList<Model.Tccom010Info> Lists = new List<Model.Tccom010Info>(); string strSql; if (strFilter.Length == 0)
{
strSql = strSqlBasic;
}
else
{
strSql = strSqlBasic + " where " + strFilter;//strSqlBasic 的定義看附1
} using (SqlDataReader reader = SqlHelper.ExecuteDataReader(strSql))
{
while (reader.Read())
{
Model.Tccom010Info Info = new WareHouse.Model.Tccom010Info(reader);//定義看附2 Lists.Add(Info);
}
} return Lists;
}
附1:private static readonly string strSqlBasic = "select * from dbo.tccom010 "; 附2:public Tccom010Info(SqlDataReader reader)
{
_suno = reader["suno"].ToString();
_nama = reader["nama"].ToString();
_addr = reader["addr"].ToString();
_tele = reader["tele"].ToString();
_faxa = reader["faxa"].ToString();
_ccur = reader["ccur"].ToString();
_cpay = reader["cpay"].ToString();
}
IList<Model.Tccom010Info> Lists = new List<Model.Tccom010Info>();Model.Tccom010Info Info = new WareHouse.Model.Tccom010Info(reader);
是的,那麼我的想法是寫這樣一個方法,隻需傳需實例化的類名、和SQL(或者strFilter),返回與相應類名
對應的IList
对于第二句代码,我有这么一个想法,在参数中加入Type t,然后在代码中加入:
object o = Activator.CreateInstance(tp);
但是我不能把o转换成对应的类型;如果可以的话,就能够正常使用了。
C# 4.0的主题就是动态编程(Dynamic Programming)。虽然C#仍然是一种静态预言,但是对象的意义开始变得越来越“动态”。它们的结构和行为无法通过静态类型来捕获,或者至少编译器在编译程序时无法得知对象的结构和行为。
C#引入了一种新的静态类型”dynamic”,当你拥有了一个dynamic类型的对象后,你“对它做的事情”只会在运行时进行解析。
C#编译器允许你通过dynamic对象调用任何方法,即使这个方法根本不存在,编译器也不会在编译的时候报编译错误。只有在运行的时候,它才会检查这个对象的实际类型,并检查在它的方法是什么意思。
也就是说,我们可以声明dynamic类型的变量,可以将object强制转换为dynamic类型,并调用它的方法。
暫時可能不會改過來,但會好好學習的,為下一個項目做準備
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;namespace WareHouse
{
public partial class Form1 : Form
{
private static readonly string strSqlBasic = "select * from dbo.tccom010 "; public Form1()
{
InitializeComponent();
System.Collections.IList Lists = new List<Model.CTccom010Info>();
GetModelByFilter(Lists, "aaa");
} public static System.Collections.IList GetModelByFilter(System.Collections.IList Lists, string strFilter)
{
string strSql; if (strFilter.Length == 0)
{
strSql = strSqlBasic;
}
else
{
strSql = strSqlBasic + " where " + strFilter;//strSqlBasic 的定義看附1
} using (SqlDataReader reader =null)
{
while (reader.Read())
{
TBaseInfo Info = new WareHouse.Model().GetTccom010Info(reader);//定義看附2 Lists.Add(Info);
}
}
return Lists;
} } public class TBaseInfo
{
public String _suno;
public String _nama;
public String _addr;
public String _tele;
public String _faxa;
public String _ccur;
public String _cpay;
} public class Model
{
public class CTccom010Info : TBaseInfo
{
public String OtherInfo;
} CTccom010Info C = new CTccom010Info(); public CTccom010Info Tccom010Info
{
get
{
return C;
}
} public CTccom010Info GetTccom010Info(SqlDataReader reader)
{
C._suno = reader["suno"].ToString();
C._nama = reader["nama"].ToString();
C._addr = reader["addr"].ToString();
C._tele = reader["tele"].ToString();
C._faxa = reader["faxa"].ToString();
C._ccur = reader["ccur"].ToString();
C._cpay = reader["cpay"].ToString();
return C;
}
}
}
System.Collections.Generic.IList<T>
而是用
System.Collections.IList