0
static class MyClass
{
static void Main()
{
bool b1 = false;
if (b1)
{
throw new MyException(GetText(b1));
}
}
public static string GetText(bool bFlag)
{
if (bFlag==false)
{
throw new Exception("'GetText' method should not be called when condition is 'false');
}
return "Some error text";
}
}
所有工作正常,例如在上面(我的意思是,当条件B1是“真”,那么“MyException”将适当的文本生成。而当条件如果“假”,则什么也不会发生如何延迟参数值计算?
我希望有一些辅助的方法对我的异常类:
class MyException : Exception
{
public void FailIfTrue(bool bCondition, string strErrorMessage)
{
if (bCondition)
{
throw new Exception(strErrorMessage);
}
}
}
在这种情况下,“主”的方法将被更改为另一个问题:
static class MyClass
{
static void Main()
{
bool b1 = false;
MyException.FailIfTrue(
b1,
GetText(b1)
);
}
}
在这种情况下“的getText”的方法将被调用......
问题:需要
你看到什么好的办法或解决方案,以创建一个帮助,将调用“的getText”只有当它的结果'FailIfTrue'函数的内部?
欢迎任何想法。
谢谢。
是的,传入委托只在某些情况下执行是推迟执行的好方法。 – 2010-08-30 19:10:23