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, th开发者_高级运维is 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.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.
精彩评论