class Program
{
static void Main(string[] args)
{
string myName = "CC";
GetFullInfo(myName,s => Console.WriteLine(s));
Console.ReadLine();
}
static void GetFullInfo(string yourname, Action<string> action)
{
string firstStr = "Welcome to cnblogs ";
action(firstStr + yourname);
}
}
将不变的逻辑封装,变化的部分使用委托Action,增加了程序的灵活度!
----------------------------------------------------------------------
public void ExecuteReader(SqlCommand cmd, Action<SqlDataReader> dataReaderCallBack)
{
using (var conn = GetConnection(_connectionString))
{
cmd.PrepareCommand(conn);
try
{
using (SqlDataReader reader = cmd.ExecuteReader())
{
dataReaderCallBack(reader);
}
}
catch (Exception)
{
throw;
}
}
}
{
while (reader.Read())
{
BasicTerm term = new BasicTerm();
term.ExternalName = reader.GetString("ExternalName");
term.ImageName = reader.GetString("FileName");
term.Title = reader.GetString("ShortText");
term.ReplacementToken = reader.GetString("ReplacementToken");
term.Text = reader.GetString("LongText");
term.ValueType = reader.GetString("ValueDataType");
}
});
这个是调用datareader的方法。