在函数参数中使用多个谓词?

发布于 08-06 22:04 字数 1232 浏览 7 评论 0原文

我有一个类,它使用查询字符串参数等构建 url。该类有一个方法:Url(),它返回由类属性组成的完整 url,另一个方法:UrlNew(),它允许将谓词作为参数传递,以替换其中一个属性的值,然后返回修改后的 URL 。现在,我需要修改这个函数以使用两个参数,两个参数都是谓词。我该怎么做?我尝试将方法的参数修改为谓词列表,但我可能没有做正确的事情:

我的旧 UrlNew() 方法看起来像这样:

public static string Url() (Action<LGUrlBuilder> predicate)
    {
        var instance = new LGUrlBuilder();
        if (predicate != null) predicate(instance);
        return instance.BuildUrl();
    }

我的新 UrlNew() 方法看起来像这样:

public static string UrlNew(List<Action<LGUrlBuilder>> predicateList)
    {
        var instance = new LGUrlBuilder();
        if (predicateList != null && predicateList.Count > 0)
        {
            foreach (Action<LGUrlBuilder> predicate in predicateList)
            {
                if (predicate != null) predicate(instance);
            }
        }

        return instance.BuildUrl();
    }

这编译得很好,但是当我运行它时,在 ASPX 中使用它会给我这个错误:

CS1660: Cannot convert lambda expression to type 'System.Collections.Generic.List<System.Action<public_site.Library.LG.LGUrlBuilder>>' because it is not a delegate type

我是 C# 初学者,我确信我做的事情完全错误。任何建议都会有所帮助。谢谢!

I have a class that builds a url with query string parameters and so on. The class has a method: Url() which returns the complete url composed from the class properties and another method: UrlNew() which allows passing a predicate as parameter for the replacement of the value of one of the properties and THEN Returns the modified URL. Now, I need to modify this function to use TWO parameters, both predicates. How do I do that? I tried modifying the method's parameters as a List of predicates but I probably am not doing something right:

My OLD UrlNew() method looked like this:

public static string Url() (Action<LGUrlBuilder> predicate)
    {
        var instance = new LGUrlBuilder();
        if (predicate != null) predicate(instance);
        return instance.BuildUrl();
    }

My NEW UrlNew() method looks like this:

public static string UrlNew(List<Action<LGUrlBuilder>> predicateList)
    {
        var instance = new LGUrlBuilder();
        if (predicateList != null && predicateList.Count > 0)
        {
            foreach (Action<LGUrlBuilder> predicate in predicateList)
            {
                if (predicate != null) predicate(instance);
            }
        }

        return instance.BuildUrl();
    }

This compiles just fine but when I run it, using it in ASPX gives me this error:

CS1660: Cannot convert lambda expression to type 'System.Collections.Generic.List<System.Action<public_site.Library.LG.LGUrlBuilder>>' because it is not a delegate type

I am a C# beginner and I am sure I am doing something completely wrong. Any advice would help. Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

何处潇湘 2024-08-13 22:04:14

不要修改函数本身。像这样修改方法调用:

UrlNew(x => { func1(x); func2(x); });

但是,如果您确实想要采用任意数量的委托实例作为参数,请尝试如下修改:

public static void UrlNew(params Action<LGUrlBuilder>[] list) {
    // ... do what you're already doing in the second snippet ...
}

您可以这样调用它:

UrlNew(x => firstthing(), x => secondthing(x), thirdthing);

旁注:Action; 不称为谓词。谓词返回一个布尔值。

Don't modify the function itself. Modify the method call like this:

UrlNew(x => { func1(x); func2(x); });

But if you really want to take arbitrary number of delegate instances as arguments, try modifying it like:

public static void UrlNew(params Action<LGUrlBuilder>[] list) {
    // ... do what you're already doing in the second snippet ...
}

You can call it like:

UrlNew(x => firstthing(), x => secondthing(x), thirdthing);

Side note: An Action<T> is not called a predicate. A predicate returns a boolean value.

城歌 2024-08-13 22:04:14

如何使用接受 2 个参数的重载 Action 委托。
如果您希望使用需要布尔返回值的谓词,请改用 Func

How about using the overloaded Action<T1,T2> delegate which accepts 2 parameters.
If you are looking to use a predicate instead which expects a boolean return value then use Func<T1,T2,bool> instead.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文