【问题标题】:Pass function as a parameter to Action将函数作为参数传递给 Action
【发布时间】:2013-10-25 14:08:39
【问题描述】:

我正在尝试将方法作为操作传递,但似乎强制转换不是按人说的。

这就是我的做法:

public class RequestHandler<T> where T : struct
{
    public enum EmployeeRequests { BasicDetails, DependentsAndEmergencyContacts , MedicalHistory }

    protected Dictionary<T, Action> handlers = new Dictionary<T, Action>();

    protected EmployeeManagement empMgmnt = new EmployeeManagement();

    public void InitializeHandler(int employeeID)
    {

        this.AddHandler(EmployeeRequests.BasicDetails, () => empMgmnt.GetEmployeeBasicDetails(0));
    }

    public void AddHandler(T caseValue, Action action)
    {
        handlers.Add(caseValue, action);
    }

    public void RemoveHandler(T caseValue)
    {
        handlers.Remove(caseValue);
    }

    public void ExecuteHandler(T actualValue)
    {
        ExecuteHandler(actualValue, Enumerable.Empty<T>());
    }

    public void ExecuteHandler(T actualValue, IEnumerable<T> ensureExistence)
    {
        foreach(var val in ensureExistence)
            if (!handlers.ContainsKey(val))
                throw new InvalidOperationException("The case " + val.ToString() + " is not handled.");
        handlers[actualValue]();
    }
}

这是我作为参数传递的函数:

public object GetEmployeeBasicDetails(int employeeID)
{
    return new { First_Name = "Mark", Middle_Initial = "W.", Last_Name = "Rooney"};
}

我收到此错误:

重载的方法有一些无效的参数。

更新

这就是我设法解决这个问题的方法:

public static class RequestHandler
{
    public enum EmployeeRequests { BasicDetails = 0, DependentsAndEmergencyContacts = 1 , MedicalHistory = 2 }

    private static Dictionary<EmployeeRequests, Func<object>> handlers = new Dictionary<EmployeeRequests, Func<object>>();

    public static void InitializeHandler(int employeeID)
    {
        Func<object> EmpBasicDetails = delegate { return EmployeeManagement.GetEmployeeBasicDetails(0); };
        AddHandler(EmployeeRequests.BasicDetails, EmpBasicDetails);
    }

    private static void AddHandler(EmployeeRequests caseValue, Func<object> empBasicAction)
    {
        handlers.Add(caseValue, empBasicAction);
    }

    public static void RemoveHandler(int caseValue)
    {
        var value = (EmployeeRequests)Enum.Parse(typeof(EmployeeRequests), caseValue.ToString());
        handlers.Remove(value);
    }

    public static object ExecuteHandler(int actualValue)
    {          
        var request = (EmployeeRequests)Enum.Parse(typeof(EmployeeRequests), actualValue.ToString());
        return handlers[(EmployeeRequests)request]();
    }
}

【问题讨论】:

  • 你从哪里得到错误?
  • Action 是一个特定的方法,返回类型为 void,没有参数。您不能将具有对象返回类型和 int 参数的方法视为操作。您要设置的是与 Func. 匹配的方法
  • 您不应该从GetEmployeeBasicDetails 返回匿名对象。见stackoverflow.com/questions/1070526/…

标签: c# delegates action


【解决方案1】:

您不能将返回值的方法作为Action 传递,因为Action&lt;T&gt; 必须采用参数T 并且不返回任何内容(即void)。

您可以通过传递一个调用您的方法并忽略其输出的 lambda 来解决此问题:

Action empBasicAction = () => GetEmployeeBasicDetails(0);

【讨论】:

  • 好的,我已经让代码工作了,但我想从执行的操作中返回一个对象。类似于object obj = empBasicAction();
  • @faizanjehangir 然后您需要将Action 替换为Func&lt;object&gt;。其余代码将保持不变。
  • 就这么做了。它现在工作正常,如果有人感兴趣,请参阅更新的答案。
猜你喜欢
  • 2013-01-27
  • 1970-01-01
  • 2021-04-13
  • 1970-01-01
  • 2021-01-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多