C# Action 的一般用法

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;
                    }
            }
        }

 

 

 xxx.ExecuteReader(termsCommand, reader =>
            {
                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的方法。
posted @ 2012-08-27 11:38  赤狐(zcm123)  阅读(6855)  评论(1编辑  收藏  举报