将动作委托作为参数传递给构造函数时出现参数错误 [英] Argument error while passing Action delegate as parameter to constructor

查看:61
本文介绍了将动作委托作为参数传递给构造函数时出现参数错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图将Action委托传递给构造函数,但遇到以下错误:

I am trying to pass Action delegate with to constructor but getting below error :


Delegate'Action'不接受0参数

Delegate 'Action' does not take 0 arguments

代码:

public sealed class VariantProcessor
{
    private string _myAppConnectionString { get; set; }
    private readonly Action<Variant> _transform;
    public Variant(string _myAppConnectionString,Action<Variant> transform)
    {
         _myAppConnectionString = _myAppConnectionString;
         _transform = transform;
    }
    public void Process(Variant model)
    {
        try
        {
            _transform(model);
             //version creation shared by both the derived types
        }
        catch (Exception) 
        {
        }
    }
}

public class AggregateCalculator : IVariantProcessor
{
    private string _myAppConnectionString { get; set; }
     public void Process(Variant model)
     {
            _myAppConnectionString = ConfigurationManager.ConnectionStrings["dbConnectionString"].ConnectionString;
            new VariantProcessor( _myAppConnectionString,
               () => Transform(model) //error
               );
     }
    private void Transform(Variant model)
    {
        //logic on variant model
    }
}

我也尝试过这种方法,但还是没有运气:

I tried like this as well but still no luck :

new VariantProcessor(_myAppConnectionString,
                   Transform(model) // error
                    );

实际上我在理解此语法时遇到问题()=> Transform(model)因此在这里我没有得到什么问题。

Actually I have problem understanding this syntax () => Transform(model) hence I am not getting whats the problem here.

有人可以帮我弄清楚这里有什么问题吗?

Can someone please help me figuring out whats the problem here ?

推荐答案

构造函数执行的动作 Action< Variant> ,这意味着它是一段代码,它使用类型为 Variant 的单个参数并返回void(因为 Action 不返回任何内容。

The Action that your constructor takes is a Action<Variant> which means it is a piece of code that takes a single parameter of type Variant and returns void (because an Action doesn't return anything.

调用构造函数时,您可以传递一个带有单个参数的lambda像这样:

When you call the constructor you can either pass it a lambda that takes a single parameter like this:

new VariantProcessor(_myAppConnectionString, model => Transform(model));

或将其传递给采用 Variant 并返回 void

or you can pass it the name of a function that takes a Variant and returns void:

new VariantProcessor(_myAppConnectionString, Transform);

语法:

() => Transform(model)

表示一个带零参数的lambda(即() )表示。您需要提供一个包含单个参数的lambda。您可以将其编写为:

means a lambda that takes zero parameters (that's that the ()) means. You need to supply a lambda that takes a single parameter. You can write it either as:

model => Transform(model)

(model) => Transform(model)

当您拥有需要多个参数的lambda时,您必须放在括号内。

When you've got a lambda that takes more than one parameter then you have to put them inside brackets.

这篇关于将动作委托作为参数传递给构造函数时出现参数错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆