公开要在构造函数中定义的 class 私有字段
Expose class private field to be defined in constructor
我有以下 class 在其构造函数中进行配置:
public class Importer {
private ImporterConfiguration _configuration;
public Importer(String path, ImporterConfiguration configuration) {
_configuration = configuration;
}
}
我见过一些class可以使用如下:
Class class = new Class("path", x => {
x.ConfigurationProperty1 = 1;
x.ConfigurationProperty2 = 2;
});
我怎样才能允许以这种方式定义我的导入器 Class _configuration
?
不接受配置值,而是接受委托并执行它,传递您现有的配置实例。
public class Importer {
private ImporterConfiguration _configuration;
public Importer(String path, Action<ImporterConfiguration> configurator) {
var configuration = new ImporterConfiguration();
configurator(configuration);
_configuration = configuration;
}
}
可能有两个可能不同的语法选项可用于配置此 属性。如果您正在寻找一个特定的对象,我将包含每个对象的一些详细信息。
匿名对象语法
您是否可能指的是使用像这样的语法正确地为您的特定对象定义匿名对象?
var importer = new Importer("path", new ImporterConfiguration(){
ConfigurationProperty1 = 1;
ConfigurationProperty2 = 2;
});
这种方法可行,并允许您在 ImporterConfiguration 对象中设置可公开访问的任何属性。
委派/动作语法
否则,如果你想要 lambda 风格的语法,你会想要定义一个 Action<ImporterConfiguration>
作为你的第二个参数,这将允许你传入一个委托来处理配置:
// Constructor that accepts a delegate for your configuration
public Importer(String path, Action<ImporterConfiguration> config)
{
// Create an instance
var configuration = new ImporterConfiguration();
configuration.Invoke(config);
// Set your backing property
_configuration = configuration;
}
这将被称为:
var importer = new Importer("path", x => { ... });
您需要定义一个传递 Type ImporterConfiguration 的 Action,然后在构造函数本身内部创建对象,然后调用传递您创建的对象的上下文的操作。
示例:
public Importer(String path, Action<ImporterConfiguration> configuration)
{
ImporterConfiguration importerConfiguration = new ImporterConfiguration();
configuration.Invoke(importerConfiguration);
_configuration = importerConfiguration;
}
用法:
Importer importer = new Importer("foo", x => { /*..*/ });
编辑(我为什么使用 .Invoke):
您还可以创建基本配置,也就是说,如果您对构造函数进行更改,将 Action 默认值传递为 null,并在操作上使用可为 null 的调用:
configuration?.Invoke(importerConfiguration);
这样,它将使用默认值创建 ImporterConfiguration,如果配置为 null,则它将使用 "default settings"
我有以下 class 在其构造函数中进行配置:
public class Importer {
private ImporterConfiguration _configuration;
public Importer(String path, ImporterConfiguration configuration) {
_configuration = configuration;
}
}
我见过一些class可以使用如下:
Class class = new Class("path", x => {
x.ConfigurationProperty1 = 1;
x.ConfigurationProperty2 = 2;
});
我怎样才能允许以这种方式定义我的导入器 Class _configuration
?
不接受配置值,而是接受委托并执行它,传递您现有的配置实例。
public class Importer {
private ImporterConfiguration _configuration;
public Importer(String path, Action<ImporterConfiguration> configurator) {
var configuration = new ImporterConfiguration();
configurator(configuration);
_configuration = configuration;
}
}
可能有两个可能不同的语法选项可用于配置此 属性。如果您正在寻找一个特定的对象,我将包含每个对象的一些详细信息。
匿名对象语法
您是否可能指的是使用像这样的语法正确地为您的特定对象定义匿名对象?
var importer = new Importer("path", new ImporterConfiguration(){
ConfigurationProperty1 = 1;
ConfigurationProperty2 = 2;
});
这种方法可行,并允许您在 ImporterConfiguration 对象中设置可公开访问的任何属性。
委派/动作语法
否则,如果你想要 lambda 风格的语法,你会想要定义一个 Action<ImporterConfiguration>
作为你的第二个参数,这将允许你传入一个委托来处理配置:
// Constructor that accepts a delegate for your configuration
public Importer(String path, Action<ImporterConfiguration> config)
{
// Create an instance
var configuration = new ImporterConfiguration();
configuration.Invoke(config);
// Set your backing property
_configuration = configuration;
}
这将被称为:
var importer = new Importer("path", x => { ... });
您需要定义一个传递 Type ImporterConfiguration 的 Action,然后在构造函数本身内部创建对象,然后调用传递您创建的对象的上下文的操作。
示例:
public Importer(String path, Action<ImporterConfiguration> configuration)
{
ImporterConfiguration importerConfiguration = new ImporterConfiguration();
configuration.Invoke(importerConfiguration);
_configuration = importerConfiguration;
}
用法:
Importer importer = new Importer("foo", x => { /*..*/ });
编辑(我为什么使用 .Invoke):
您还可以创建基本配置,也就是说,如果您对构造函数进行更改,将 Action 默认值传递为 null,并在操作上使用可为 null 的调用:
configuration?.Invoke(importerConfiguration);
这样,它将使用默认值创建 ImporterConfiguration,如果配置为 null,则它将使用 "default settings"