代码之家  ›  专栏  ›  技术社区  ›  ThunD3eR

如何在C语言中执行函数参数#

  •  0
  • ThunD3eR  · 技术社区  · 7 年前

    我正在尝试创建一个 通用 将采用的方法 Func 参数。

    传统的功能是我们创造 methodOne() 把它当作 参数 到另一个方法。然而,这将破坏我试图创建的方法的目的 通用 .

    我的代码:

    public static List<T> ExecuteQuery<T>(string connection, 
                                          string commandText, 
                                          Func<SqlDataReader, List<T>> myMethodName)
    {
        List<T> items = new List<T>();
        SqlDataReader sqlDataReader = null;
    
        try
        {
            using (SqlConnection con = new SqlConnection(connection))
            {
                using (SqlCommand cmd = new SqlCommand(commandText, con))
                {
                    try
                    {
                        con.Open();
                        sqlDataReader = cmd.ExecuteReader();
    
                        items = myMethodName(sqlDataReader);
                    }
                    catch (Exception ex)
                    {
                        if (sqlDataReader != null) sqlDataReader.Close();
    
                        cmd.Dispose();
    
                        throw ex;
                    }
                    finally
                    {
                        if (sqlDataReader != null) sqlDataReader.Dispose();
    
                        cmd.Dispose();
                    }
                }
            }
        }
        catch (Exception)
        {
            throw;
        }
    
        return items;
    }
    

    尝试像这样调用上面的方法 :

        public List<Function> GetDeletedFunctions(string connectionString)
        {
            SqlDataReader sqlDataReader = null;
            List<Function> functions;
    
            string cmdText = @"SELECT * FROM Table "; // dumy query
    
            functions = DbHelper.ExecuteQuery<Function>(
              connectionString, 
              cmdText, 
              List<Function>(sqlDataReader)
            {
    
                var f =
                (from x in sqlDataReader.Cast<DbDataRecord>()
                 select new Function
                 {
                     Param1 = DbHelper.GetValue<string>("Param1 ", x),
                     Param2 = DbHelper.GetValue<string>("Param2", x),
                 }).ToList();
    
                return f;
            } );
    }
    

    编译时错误:

    错误cs1955不可调用的成员“list”不能像 方法。

    我假设有一个 语法问题 这里和它让我发疯。有什么建议吗?

    1 回复  |  直到 7 年前
        1
  •  2
  •   Dmitrii Bychenko    7 年前

    包装时 using 您不必捕获异常情况(NET将为您释放资源,让您大放异彩)

    // Dangerous practice: what if I put commandText "drop table MyTable"?
    public static List<T> ExecuteQuery<T>(string connection, 
                                          string commandText, 
                                          Func<SqlDataReader, List<T>> myMethodName) {
      //ToDo: Validate parameters here
    
      using (var con = new SqlConnection(connection)) {
        con.Open();
    
        using (var cmd = new SqlCommand(commandText, con)) {
          // IDataReader is IDisposable as well       
          using (var reader = cmd.ExecuteReader()) {
            return myMethodName(reader);
          } 
        }
      }  
    }
    

    然后我们要做的就是提供参数: connectionString , commandText 和映射函数;让我们来做:

    public List<Function> GetDeletedFunctions(string connectionString) {
      //TODO: Validate connectionString here 
    
      return DbHelper.ExecuteQuery<Function>(
         connectionString,                                // Connection String
       @"SELECT * FROM Table ",                           // Query
        (reader) => {                                     // Map function
           // When given reader, the map function creates list
           List<Function> result = new List<Function>();
    
           // Convert each record into Function instance and add them into the list
           while (reader.Read()) {
             Function item = new Function() {
               Param1 = Convert.ToString(reader["Param1"]),
               Param2 = Convert.ToString(reader["Param2"]),
             }
    
             result.Add(item); 
           }
    
           // and return the list
           return result;
        }
      )
    }
    
        2
  •  1
  •   Sarvesh Mishra    7 年前

    您试图传递函数作为参数的方式有问题。请使用以下代码:

    public List<Function> GetDeletedFunctions(string connectionString)
    {                        
        string cmdText = @"SELECT * FROM Table "; // dumy query
        return DbHelper.ExecuteQuery<Function>(connectionString,
                                               cmdText,
                                               (SqlDataReader sqlDataReader) =>
                                               {
                                                   var f = (from x in sqlDataReader.Cast<DbDataRecord>()
                                                   select new Function
                                                   {
                                                       Param1 = DbHelper.GetValue<string>("Param1 ", x),
                                                       Param2 = DbHelper.GetValue<string>("Param2", x)
                                                   }).ToList();
    
                                                   return f;
                                               });
    }
    
    推荐文章