在函数参数中使用多个谓词?
我有一个类,它使用查询字符串参数等构建 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不要修改函数本身。像这样修改方法调用:
但是,如果您确实想要采用任意数量的委托实例作为参数,请尝试如下修改:
您可以这样调用它:
旁注:
Action;
不称为谓词。谓词返回一个布尔值。Don't modify the function itself. Modify the method call like this:
But if you really want to take arbitrary number of delegate instances as arguments, try modifying it like:
You can call it like:
Side note: An
Action<T>
is not called a predicate. A predicate returns a boolean value.如何使用接受 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.