1

关于这个错误,我已经在网上查看了大约 20 多个页面。我一直无法找到解决方案。我知道为什么会发生错误,但无法弄清楚如何解决这个问题。:

我只需要为我的操作分配一个操作,但这些操作必须有一个字符串类型的传入参数。任何想法都非常感谢,因为我已经为此苦苦挣扎了一段时间。

public class CommonDemoHelper
{
    static Action<TextBoxSettings> textBoxSettingsMethod(string txtBoxName);
    static Action<DateEditSettings> dateEditSettingsMethod;
    static Action<LabelSettings> labelSettingsMethod;
    static Action<LabelSettings> wideLabelSettingsMethod;
    static Action<SpinEditSettings> spinEditSettingsMethod;
    static Action<MemoSettings> memoSettingsMethod;

    public static Action<TextBoxSettings> TextBoxSettingsMethod(string txtBoxName)
    {
        get
        {

            if (textBoxSettingsMethod(txtBoxName) == null)
                //Getting error below cannot assign to method group
                textBoxSettingsMethod = CreateTextBoxSettingsMethod(txtBoxName);
            return textBoxSettingsMethod(txtBoxName);
        }
    }

    static Action<TextBoxSettings> CreateTextBoxSettingsMethod(string txtBoxName)
    {
        return settings =>
        {
            settings.ControlStyle.CssClass = "editor";
            settings.ShowModelErrors = true;
            settings.Properties.ValidationSettings.ErrorDisplayMode = ErrorDisplayMode.ImageWithText;
        };
    }
}
4

2 回答 2

2

您声明textBoxSettingsMethod为一种方法:

static Action<TextBoxSettings> textBoxSettingsMethod(string txtBoxName);

当看起来您正试图将其定义为属性时:

static Action<TextBoxSettings> textBoxSettingsMethod;

尽管txtBoxName未使用since,CreateTextBoxSettingsMethod但对于您实际要完成的工作来说,这有点令人困惑。

于 2013-01-10T17:27:56.000 回答
2

如果一个方法(或委托)返回一个值,它将是一个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;
    }
}
于 2013-01-10T17:49:46.253 回答