将布尔表达式作为方法参数传递

pass a bool expression as a method parameter

我有点困惑,试图使用 lambda/LINQ ExpressionFunc<bool> 来简单地替换 bool 来实现更优雅的通用解决方案return类型。

假设表达式是:

public bool someBoolRetMethod(someType parA, someOtherType parB) {
    if(parA==null)
        return new ExpM("relevant msg").Rtrn;
} 

所以现在如果 parAnullExpM() 是处理错误的 class

我想做的是将条件作为参数传递:

public class ExpBoolCond:ExpM {

    public bool Rtrn{get;set;}
    public ExpBoolCond(theBool, themsg) {
        variable to hold theBool;
        if(theBool) new specialMbxWindow(themsg)
        then set Rtrn..
    }
}

这样我就可以使用:

 var condNullParA = new ExpBoolCond(parA==null, "passed ParA is Null,\r\nAborting method <sub>(methodName and line# is handled inside  ExpM base class)</sub> !")
if(condNullParA.Rtrn) ....

正确的实施方式是什么?

更新:

 public class ExcBCondM:ExcpM
 {
     public bool Rtrn { get { return this._Rtrn(); } }
     Func<bool> _Rtrn { get; set; }
     public ExcBCondM(Func<bool> cond, string bsMsg)
        : base(bsMsg,false)
     {
        this._Rtrn = cond;
        //if (this._Rtrn) this.show();
     }
     public bool activateNon() { this.show(); return false; }
 }

用法:

public bool someBoolRetMethod(some_passed_Type parA)
{
    var someCondExpM =  new ExpBoolCond(() => parA==null, "relevant Message");
    if(someCondExpM.Rtrn) 
    return someCondExpM.activateNon(); //if() now Calls Func<bool> _Rtrn  when called rather where stated.
  return true;//if ok...
}

如果要将Func<bool>创建为lambda表达式,语法如下:

var condNullParA = new ExpBoolCond(() => parA==null, "passed ParA is Null,\r\nAborting method <sub>(methodName and line# is handled inside  ExpM base class)</sub> !")
//                                 ^^^^^

() => 部分告诉 C# 编译器后面的表达式应该成为不带参数的 lambda 的主体,并且 returns 无论表达式右侧的类型是什么=> 符号,即 bool.