简体   繁体   English

C#-使用Lambda指定非默认参数

[英]C# - Using a lambda to specify non-default params

I'm building a library which contains a series of methods, each of which has several overloads which I'm trying to simplify. 我正在建立一个包含一系列方法的库,每个方法都有几个我要简化的重载。

Many of the methods have recurring overload options. 许多方法都有循环重载选项。 For example 例如

AddString(string option1)
AddString(string option1, int option2)
AddString(string option1, int option2, bool option3)

AddArray(string option1)
AddArray(string option1, int option2)
AddArray(string option1, int option2, bool option3)

Now, I know that I can specify those as default parameters - parameters with a specified default value. 现在,我知道可以将它们指定为默认参数-具有指定默认值的参数。 I don't want to do this because I want the defaults to be configurable from project to project, or even from caller to caller. 我不想这样做,因为我希望可以在项目之间甚至甚至在调用者之间对默认值进行配置。

I also realise that I could build an Options object which has the (configurable) defaults, and the user could pass an instance of that to another overload like so 我还意识到我可以构建一个具有(可配置)默认值的Options对象,并且用户可以将其实例传递给另一个重载,如下所示

var options = new Options();
options.OptionProperty1 = "A new string";
AddString(options);

But what I'd prefer is to have a static Options object which contained the defaults for all the parameters, and somehow specify only the changes in an inline call, like so 但是我更希望有一个静态的Options对象,其中包含所有参数的默认值,并且以某种方式仅指定内联调用中的更改,如下所示

AddString(options => options.Set(
                            OptionProperty1 = "A string",
                            OptionProperty2 = 1,
                            OptionProperty3 = true
                        ));

Is this possible, and if so then what would the method signature look like? 这是可能的,如果可以,那么方法签名将是什么样?

In order to have a Set method like you're after it would have to have a parameter for each option you wanted to set, which will probably get unwieldy after time, 为了像您一样拥有Set方法,它必须为要设置的每个选项都具有一个参数,一段时间后可能会变得笨拙,

You can opt for a fluent interface which should give you want you want. 您可以选择一个流畅的界面,它应该可以为您提供想要的东西。 For example: 例如:

class Options
{
  Options Option1(string value)
  {
    m_Option1 = value;
    return this;
  }

  Options Option2(int value)
  {
    m_Option2 = value;
    return this;
  }
}

Now AddString would look like this: 现在, AddString将如下所示:

AddString(options = > options.Option1("A String").Option2(1)));

And the implementation of AddString would look like this: 并且AddString的实现如下所示:

public void AddString(Action<Options> optionConfig)
{
  var options = new Options();
  optionConfig(options);

  // rest of method
}

Alternatively you could have the caller create the Options instance. 另外,您可以让调用者创建Options实例。 This would allow them to set the properties directly: 这将允许他们直接设置属性:

class Options
{
  public string Option1{get;set;}
  public int Option2{get;set;}
}

And now the caller can say: 现在,呼叫者可以说:

AddString(() = > new Options{Option1="A String", Option2=1});

Where AddString looks like this: AddString如下所示:

public AddString(Func<Options> optionsCreator)
{
  Options options = optionsCreator();

  // rest of method
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM