2

我想传递一个方法作为参数来从我的程序中删除重复的代码。这是我想要的功能:

 private void sqlQuery(Method sqlMethod)
{
    conn.ConnectionString = ConfigurationManager.ConnectionStrings["ApplicationServices"].ConnectionString;
    try
    {
        //open SQL connection
        conn.Open();
        sqlMethod();
    }
    catch (Exception ex)
    {
        System.Diagnostics.Debug.Write(ex.ToString());
    }
    finally
    {
        conn.Close();
    }
}

使用:

private void insertTemplate()
{
    //create SQL command
    SqlCommand insertTemplate = new SqlCommand("INSERT INTO [SavedDescriptionTemplates] (SavedDescription, UserId, CreatedDate) VALUES (@descriptionParam, @userIdParam, @createdDateParam)", conn);
    //create and assign parameters
    insertTemplate.Parameters.AddWithValue("@descriptionParam", descriptionInput.Text);
    insertTemplate.Parameters.AddWithValue("@userIdParam", Int32.Parse(userNameDropDown.SelectedItem.Value));
    insertTemplate.Parameters.AddWithValue("@createdDateParam", DateTime.Now);
    //execute command and retrieve primary key from the above insert and assign to variable
    insertTemplate.ExecuteNonQuery();
}

电话是:

sqlQuery(insertTemplate());

这可能吗?

4

7 回答 7

4

使用Action委托。

改变

private void sqlQuery(Method sqlMethod)

进入

private void sqlQuery(Action sqlMethod)

sqlQuery(insertTemplate());

进入

sqlQuery(insertTemplate);
于 2013-07-03T14:02:44.793 回答
1

Yes, but slightly different...

Try this:

sqlQuery(insertTemplate); // don't CALL the method, just name it

And declare sqlQuery like this:

private void sqlQuery(Action sqlMethod)
于 2013-07-03T14:03:14.343 回答
1

You can achieve that with a delegate.

Use for example Action:

private void sqlQuery(Action action)
{
    conn.ConnectionString = ConfigurationManager.ConnectionStrings["ApplicationServices"].ConnectionString;
    try
    {
        //open SQL connection
        conn.Open();

        action();
    }
    catch (Exception ex)
    {
        System.Diagnostics.Debug.Write(ex.ToString());
    }
    finally
    {
        conn.Close();
    }
}
于 2013-07-03T14:03:19.463 回答
1

Look into delegates. In your case, since your method takes no parameters, you can use the built in Action delegate:

private void sqlQuery(Action sqlMethod)
{
    //...
}

sqlQuery(insertTemplate);
于 2013-07-03T14:03:55.277 回答
1

Blocks of logic in C# are passed as delegates. There are several ways of declaring a delegate - you can define a custom one, or use one of predefined types:

  • You can use Action<...> for delegates that do not return values, or
  • You can use Func<...,R> for delegates that return values.

Here is one way to define a function that takes a delegate:

private void sqlQuery(Action<IDbConnection> sqlMethod) {
   ...
   sqlMethod(conn);
   ...
}

Call sqlQuery with a named or an anonymous delegate which could be defined inline, as a named method, or as a lambda expression:

// Using some method that takes IDbConnection
sqlQuery(MySqlMethod);
// Using an anonymous delegate
sqlQuery((conn) => {
    ... // Do something with conn
});
于 2013-07-03T14:04:27.697 回答
1

是的。您需要使用委托、匿名函数或 lambda 表达式。

这是一个简单的例子:

public void SomeMethod(Action<string> passedMethod)
{
    passedMethod("some string");
}

public void Caller()
{
    SomeMethod(x => Console.WriteLine(x));
}

如果你运行“Caller”,它会输出“some string”。

于 2013-07-03T14:05:05.633 回答
0

代表就是答案 -> Microsoft on C# Delegates

public void delegate MyDelegate();

MyDelegate del = this.insertTemplate;

private void sqlQuery(MyDelegate sqlMethod)
{
 ...
 del();
 ...
}
于 2013-07-03T14:06:14.530 回答