如果一个方法(或委托)返回一个值,它将是一个Func<return_type>
,而不是一个动作。如果它有输入参数(参数),那么它将是一个Func<arg1,arg2,arg3,return_type>
static Func<string, TextBoxSettings> textBoxSettingsMethod;
public static Func<string, TextBoxSettings> GetTextBoxSettingsMethod(string txtBoxName)
{
if (textBoxSettingsMethod == null)
textBoxSettingsMethod = CreateTextBoxSettingsMethod(txtBoxName);
return textBoxSettingsMethod;
}
static Func<string, TextBoxSettings> CreateTextBoxSettingsMethod(string txtBoxName)
{
...
}
这里Func<string, TextBoxSettings>
指定一个接受字符串参数(输入)并返回TextBoxSettings
(输出)的方法。
此外,由于您需要输入参数,TextBoxSettingsMethod
因此它不能是属性。我把它改成了一种方法。
此外,我更改了测试以确保它textBoxSettingsMethod
不为空(不是在执行时返回结果)。测试textBoxSettingsMethod(txtBoxName) == null
调用该方法并测试 type 的返回值TextBoxSettings
是否为null
。如果只想知道方法是否定义,测试应该是textBoxSettingsMethod == null
.
不要混淆作为textBoxSettingsMethod
方法(或委托)和执行方法的表达式。textBoxSettingsMethod(txtBoxName)
争论有什么string txtBoxName
用?你没有使用它。您是否需要针对不同的文本框使用不同的方法?很难说。
更新
这将始终创建相同的方法,该方法根据文本框名称返回不同的设置(参数string txtBoxName
不是必需的,只有 lambda 表达式的参数,tbName
它在调用时需要实际参数值):
static Func<string, TextBoxSettings> CreateTextBoxSettingsMethod()
{
return tbName =>
{
TextBoxSettings settings;
switch (tbName) {
case "textbox1":
case "textbox2":
case "textbox3":
settings = new TextBoxSettings {
TextBoxName = tbName,
ControlStyle = "editor",
ShowModelErrors = true
};
settings.Properties.ValidationSettings.ErrorDisplayMode = ErrorDisplayMode.ImageWithText;
return settings;
case "textbox4":
case "textbox5":
settings = new TextBoxSettings {
TextBoxName = tbName,
ControlStyle = "displayer",
ShowModelErrors = false
};
settings.Properties.ValidationSettings.ErrorDisplayMode = ErrorDisplayMode.TextOnly;
return settings;
default:
return new TextBoxSettings { TextBoxName = tbName };
}
};
}
相反,这是一种创建不同方法的方法,这些方法总是返回相同的设置
static Func<string, TextBoxSettings> CreateTextBoxSettingsMethod(string txtBoxName)
{
switch (txtBoxName) {
case "textbox1":
case "textbox2":
case "textbox3":
return tbName =>
{
var settings = new TextBoxSettings {
TextBoxName = tbName,
ControlStyle = "editor",
ShowModelErrors = true
};
settings.Properties.ValidationSettings.ErrorDisplayMode = ErrorDisplayMode.ImageWithText;
return settings;
};
case "textbox4":
case "textbox5":
return tbName =>
{
var settings = new TextBoxSettings {
TextBoxName = tbName,
ControlStyle = "displayer",
ShowModelErrors = false
};
settings.Properties.ValidationSettings.ErrorDisplayMode = ErrorDisplayMode.TextOnly;
return settings;
};
default:
return tbName => new TextBoxSettings { TextBoxName = tbName };
}
}
更新#2
也许你想要这个
static Dictionary<string, Func<string, TextBoxSettings>> textBoxSettingsMethod
= new Dictionary<string,Func<string,TextBoxSettings>>();
public static Func<string, TextBoxSettings> GetTextBoxSettingsMethod(string txtBoxName)
{
Func<string, TextBoxSettings> method;
if (!textBoxSettingsMethod.TryGetValue(txtBoxName, out method)) {
method = CreateTextBoxSettingsMethod(txtBoxName);
textBoxSettingsMethod.Add(txtBoxName, method);
}
return method;
}
你可以像这样使用它:
Func<string, TextBoxSettings> method;
TextBoxSettings setting;
method = GetTextBoxSettingsMethod("textbox1");
setting = method("textbox1");
// or
setting = GetTextBoxSettingsMethod("textbox1")("textbox1");
// or, if you are sure that all the methods have been assigned
method = textBoxSettingsMethod["textbox1"];
setting = method("textbox1");
// or
setting = textBoxSettingsMethod["textbox1"]("textbox1");
但我仍然不确定为什么要动态创建这些方法。返回设置的简单方法就足够了
public static TextBoxSettings GetTextBoxSettings(string txtBoxName)
{
switch (txtBoxName) {
// ...
default:
return null;
}
}