【问题标题】:How to pass a Func<> property/method to a NancyFx Get() method?如何将 Func<> 属性/方法传递给 NancyFx Get() 方法?
【发布时间】:2018-08-09 22:11:29
【问题描述】:

NancyFx (2.x) NancyModule.Get() 方法定义为:

public virtual void Get(string path, Func<dynamic, object> action, [Func<NancyContext, bool> condition = null], [string name = null]);

正常用法是:

public class MyModule
{
    public MyModule() 
    {
        this.Get("/", parameters => {
            this.RequestHandler = new RequestHandler();

            return this.RequestHandler.HandleRequest("/", parameters, someOtherInfo);
        });
    }
}

我想将第二个参数定义为一个属性,这样我就可以像这样用于多个路径:

public class MyModule
{
    Func<dynamic, object> indexHandler = parameters => {
        // Error: Keyword "this" is not available in this context
        this.RequestHandler = new RequestHandler();

        // Error: Keyword "this" is not available in this context
        return this.RequestHandler.HandleRequest("/", parameters, someOtherInfo);
    };

    public MyModule() 
    {
        this.Get("/", indexHandler);
        this.Get("/index", indexHandler);
    }
}

如果我这样做,它会起作用:

public class MyModule
{
    public MyModule() 
    {
        Func<dynamic, object> indexHandler = parameters => {
            this.RequestHandler = new RequestHandler();

            return this.RequestHandler.HandleRequest("/", parameters, someOtherInfo);
        };

        this.Get("/", indexHandler);
        this.Get("/index", indexHandler);
    }
}

但我不想在构造函数中定义它。我究竟做错了什么?有没有其他方法可以做到这一点?

MVCE

依赖包: Nancy(版本:2.0.0-clinteastwood)

using Nancy;
using Nancy.Responses.Negotiation;

namespace MyNamespace
{
    public class MyModule : NancyModule
    {
        private RequestHandler RequestHandler;
        private object IndexHandler(dynamic parameters)
        {
            this.RequestHandler = new RequestHandler();
            var someOtherInfo = "";
            return this.RequestHandler.HandleRequest("/", parameters, someOtherInfo);
        }

        public MyModule()
        {
            this.Get("/", IndexHandler);
            this.Get("/index", IndexHandler);

            this.Get("/home", parameters => {
                return this.Negotiate.WithView("myview.html");
            });
        }
    }

    public class RequestHandler
    {
        public Negotiator HandleRequest(string path, dynamic parameters, string someOtherInfo)
        {
            return new Negotiator(new NancyContext());
        }
    }
}

【问题讨论】:

  • @john-wu 我不是在问为什么我不能在房产中使用“这个”。我要求一种在构造函数以外的地方定义函数的方法,因此它与您标记的问题不同。请再次阅读我的问题。
  • indexHandler MyModule 的字段,不是吗?
  • @JohnWu 是的。在我发布的第二段代码中。但我的问题是什么?除了构造函数之外,还有没有更好的方法来定义它。不必作为字段或属性。看看下面的答案。这是否类似于您标记的问题中的答案?再次,请阅读我的问题并尝试理解它。

标签: c# lambda nancy func


【解决方案1】:

这应该可行:

public class MyModule
{
    public MyModule() 
    {
       this.Get("/", IndexHandler);
       this.Get("/index", IndexHandler);
    }

    private object IndexHandler(dynamic parameters) {
        this.RequestHandler = new RequestHandler();
        return this.RequestHandler.HandleRequest("/", parameters, someOtherInfo);
    }
}

【讨论】:

  • 我认为您的参数声明不是有效的 C#。否则正确答案。
  • 看起来不错,但在将其传递给Get() 方法时出现以下错误:'dynamic MyModule.IndexHandler(object)' has the wrong return type。有什么想法吗?
  • 我编辑了你的答案@Andrew,Func&lt;dynamic, object&gt; 意味着它需要一个动态参数并返回一个对象。
  • @Haytam 谢谢!
  • @Haytam 我仍然收到同样的错误'object MyModule.IndexHandler(dynamic)' has the wrong return type。不知何故,我觉得这很奇怪。
【解决方案2】:

Andrew 的回答是有效的,应该已经足够了,但显然(在您的 MVCE 中)该方法定义不存在。

这是正确的定义(至少是 VS 想要的):

public virtual void Get(string path, Func<object, Task<object>> action, Func<NancyContext, bool> condition = null, string name = null)

幸运的是,您的 HandleRequest 是可等待的,因此您只需编辑返回类型。
这是正确的定义:

private Task<object> IndexHandler(dynamic parameters)
{
    this.RequestHandler = new RequestHandler();
    var someOtherInfo = "";
    return this.RequestHandler.HandleRequest("/", parameters, someOtherInfo);
}

希望这会有所帮助!

【讨论】:

    猜你喜欢
    • 2013-05-18
    • 2014-03-22
    • 1970-01-01
    • 1970-01-01
    • 2015-10-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-03
    相关资源
    最近更新 更多