运行时检查一个Func委托(Runtime check for a Func delegate)

编程入门 行业动态 更新时间:2024-10-28 01:19:05
运行时检查一个Func委托(Runtime check for a Func delegate)

考虑下面的代码片段:

class Test { public int Length{ get; set; } public Func<List<int>, double> Calculate { get; set; } public void DoStuff(List<int> list) { // ... do some processing here ... double ans = Calculate(list); // ... do some more stuff ...... } }

现在,我已经意识到,将附加到Calculate属性的典型匿名Func委托与Test类的Length属性以及传递给委托的列表的长度有关。 对于这两个长度之间的某些关系(例如,this.Length <list.Count),我应该能够引发异常。 但是,这必须在运行时发生,并且我无法控制此类的用户将作为“委托”添加到“计算”属性中。

这个怎么做? 我可以在Calculate属性的set方法中做些什么吗? 如何在运行时检查是否满足涉及Test类的Length属性和传递给委托的列表对象的长度(计数)的某些情况?

我还会考虑Test类的设计更改以实现我的目标(运行时检查),但为用户附加Func委托并完成此操作的方法将很整齐。 谢谢。

Consider the following code snippet:

class Test { public int Length{ get; set; } public Func<List<int>, double> Calculate { get; set; } public void DoStuff(List<int> list) { // ... do some processing here ... double ans = Calculate(list); // ... do some more stuff ...... } }

Now, I have realized that the the typical anonymous Func delegate that will be attached to the Calculate property, would have something to do with the Length property of the Test class, and the length of the list which is being passed to the delegate. For some relationship between these two lengths (e.g., this.Length < list.Count), I should be able to throw an exception. But, this has to happen at runtime, and I would have no control over what the users of this class would be adding as delegate in the Calculate property.

How to do this? Can I do something in the set method of the Calculate property? How to check at runtime if certain condition involving the Length property of the Test class and the length (count) of the list object passed to the delegate is satisfied?

I would also consider design changes of the Test class to accomplish my objective (of runtime checking), but a way for the users to attach a Func delegate and still accomplish this would be neat. Thanks.

最满意答案

在调用提供的代理之前添加您自己的支票。 这样,您可以在调用之前检查所有需求,并在一个地方完成检查(而不是在每个实施中重复)。

public void DoStuff(List<int> list) { CheckPreconditions(list); double ans = Calculate(list); } private void CheckPreconditions(List<int> list) { if(list == null) // throw ... if(list.Length == 0) // throw ... if(Length < list.Count) throw ArgumentException("list", "list must have blah blah blah to satisfy requirements"); }

这与模板方法模式相似,除了使用继承之外,您正在委托给用户提供的函数:)

此外,如果您计算一个字段而不是公共属性,您可以强制它通过DoStuff方法执行,因此检查将始终执行。

就像是:

class Test { public int Length{ get; set; } private Func<List<int>, double> m_calculate; // Inject into ctor public Test(Func<List<int>, double> calculate) { if(calculate == null) throw new ArgumentNullException("calculate"); m_calculate = calculate; } // all access to Calculate is now through this method public void DoStuff(List<int> list) { CheckPreconditions(list); double ans = m_calculate(list); } }

如果“计算”只设置一次,这将非常合适。

Add your own checks prior to invoking the supplied delegate. This way, you can check all of the requirements are met prior to invoking and the checking is done in one place (instead of duplicated in each implementation).

public void DoStuff(List<int> list) { CheckPreconditions(list); double ans = Calculate(list); } private void CheckPreconditions(List<int> list) { if(list == null) // throw ... if(list.Length == 0) // throw ... if(Length < list.Count) throw ArgumentException("list", "list must have blah blah blah to satisfy requirements"); }

This is similar to the template method pattern except instead of using inheritance, you're delegating to a user-supplied function :)

Also, if you make Calculate a field instead of a public property, you can force it to be executed through the DoStuff method, so the checks will always be carried out.

Something like:

class Test { public int Length{ get; set; } private Func<List<int>, double> m_calculate; // Inject into ctor public Test(Func<List<int>, double> calculate) { if(calculate == null) throw new ArgumentNullException("calculate"); m_calculate = calculate; } // all access to Calculate is now through this method public void DoStuff(List<int> list) { CheckPreconditions(list); double ans = m_calculate(list); } }

This would be a good fit if Calculate is only ever set once.

更多推荐

本文发布于:2023-08-07 23:34:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1466046.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Runtime   Func   delegate   check

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!