我有一个想法,听起来好像可行,但我不完全确定,因此就是否可以实现以及如何实现寻求建议。
在我的网络表单上,我有一个名为“error”的 bool 值。
页面上需要发生许多事情才能成功加载。
我可以这样写代码:
bool thisSuccess = DoThis();
if(!thisSuccess)
then error;
bool thatSuccess = DoThat();
if(!thatSuccess)
then error;
if(error)
FailoverActions();
等等。
当然那会是完全低效的,所以我认为创建某种委托(delegate)可能是可行的,代码看起来像这样:
错误 = DoThis();
...还有某种触发器,它在 error = true 时调用函数;
很抱歉缺乏精确的细节,但这对我来说是新的领域。
更新
感谢大家的好主意。
之所以没有详细说明,是因为我经验不足,而我迄今为止发现的 .net 是,尽管有很多方法可以打鸡蛋,但总的来说有一些比其他方法更好。
我很欣赏你有经验的观点。
再次感谢。
最佳答案
为什么不呢。返回分配给被委托(delegate)者的 bool 值的方法。
这边
public delegate bool PerformCalculation();
PerformCalculation = DoThis();
if (!PerformCalculation())
then error;
PerformCalculation = DoThat();
if(!PerformCalculation())
then error;
if(error)
FailoverActions();
替代解决方案
不需要委托(delegate)。只需2个方法
bool DoThis()
和 bool DoThat()
if (!DoThis())
then error;
if(!DoThat())
then error;
if(error)
FailoverActions();
关于c# 是否可以创建这样的委托(delegate)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12367335/