@@ -762,6 +762,310 @@ public MyDate TryEvaluate_MyDate(string categoryName, MyDate def)
762762 }
763763 return def ;
764764 }
765+ #endregion
766+
767+ #region TryEvaluateExp
768+ /// <summary>
769+ /// 执行函数,如果异常,返回默认值
770+ /// </summary>
771+ /// <param name="exp"></param>
772+ /// <param name="def"></param>
773+ /// <returns></returns>
774+ public ushort TryEvaluateExp ( string exp , ushort def )
775+ {
776+ try {
777+ var obj = EvaluateExp ( exp ) ;
778+ obj = obj . ToNumber ( "It can't be converted to number!" ) ;
779+ if ( obj . IsError ) {
780+ LastError = obj . ErrorMsg ;
781+ return def ;
782+ }
783+ return ( ushort ) obj . IntValue ;
784+ } catch ( Exception ex ) {
785+ LastError = ex . Message + "\r \n " + ex . StackTrace ;
786+ }
787+ return def ;
788+ }
789+ /// <summary>
790+ /// 执行函数,如果异常,返回默认值
791+ /// </summary>
792+ /// <param name="exp"></param>
793+ /// <param name="def"></param>
794+ /// <returns></returns>
795+ public uint TryEvaluateExp ( string exp , uint def )
796+ {
797+ try {
798+ var obj = EvaluateExp ( exp ) ;
799+ obj = obj . ToNumber ( "It can't be converted to number!" ) ;
800+ if ( obj . IsError ) {
801+ LastError = obj . ErrorMsg ;
802+ return def ;
803+ }
804+ return ( uint ) obj . IntValue ;
805+ } catch ( Exception ex ) {
806+ LastError = ex . Message + "\r \n " + ex . StackTrace ;
807+ }
808+ return def ;
809+ }
810+ /// <summary>
811+ /// 执行函数,如果异常,返回默认值
812+ /// </summary>
813+ /// <param name="exp"></param>
814+ /// <param name="def"></param>
815+ /// <returns></returns>
816+ public ulong TryEvaluateExp ( string exp , ulong def )
817+ {
818+ try {
819+ var obj = EvaluateExp ( exp ) ;
820+ obj = obj . ToNumber ( "It can't be converted to number!" ) ;
821+ if ( obj . IsError ) {
822+ LastError = obj . ErrorMsg ;
823+ return def ;
824+ }
825+ return ( ulong ) obj . IntValue ;
826+ } catch ( Exception ex ) {
827+ LastError = ex . Message + "\r \n " + ex . StackTrace ;
828+ }
829+ return def ;
830+ }
831+ /// <summary>
832+ /// 执行函数,如果异常,返回默认值
833+ /// </summary>
834+ /// <param name="exp"></param>
835+ /// <param name="def"></param>
836+ /// <returns></returns>
837+ public short TryEvaluateExp ( string exp , short def )
838+ {
839+ try {
840+ var obj = EvaluateExp ( exp ) ;
841+ obj = obj . ToNumber ( "It can't be converted to number!" ) ;
842+ if ( obj . IsError ) {
843+ LastError = obj . ErrorMsg ;
844+ return def ;
845+ }
846+ return ( short ) obj . IntValue ;
847+ } catch ( Exception ex ) {
848+ LastError = ex . Message + "\r \n " + ex . StackTrace ;
849+ }
850+ return def ;
851+ }
852+ /// <summary>
853+ /// 执行函数,如果异常,返回默认值
854+ /// </summary>
855+ /// <param name="exp"></param>
856+ /// <param name="def"></param>
857+ /// <returns></returns>
858+ public int TryEvaluateExp ( string exp , int def )
859+ {
860+ try {
861+ var obj = EvaluateExp ( exp ) ;
862+ obj = obj . ToNumber ( "It can't be converted to number!" ) ;
863+ if ( obj . IsError ) {
864+ LastError = obj . ErrorMsg ;
865+ return def ;
866+ }
867+ return obj . IntValue ;
868+ } catch ( Exception ex ) {
869+ LastError = ex . Message + "\r \n " + ex . StackTrace ;
870+ }
871+ return def ;
872+ }
873+ /// <summary>
874+ /// 执行函数,如果异常,返回默认值
875+ /// </summary>
876+ /// <param name="exp"></param>
877+ /// <param name="def"></param>
878+ /// <returns></returns>
879+ public long TryEvaluateExp ( string exp , long def )
880+ {
881+ try {
882+ var obj = EvaluateExp ( exp ) ;
883+ obj = obj . ToNumber ( "It can't be converted to number!" ) ;
884+ if ( obj . IsError ) {
885+ LastError = obj . ErrorMsg ;
886+ return def ;
887+ }
888+ return obj . IntValue ;
889+ } catch ( Exception ex ) {
890+ LastError = ex . Message + "\r \n " + ex . StackTrace ;
891+ }
892+ return def ;
893+ }
894+ /// <summary>
895+ /// 执行函数,如果异常,返回默认值
896+ /// </summary>
897+ /// <param name="exp"></param>
898+ /// <param name="def"></param>
899+ /// <returns></returns>
900+ public float TryEvaluateExp ( string exp , float def )
901+ {
902+ try {
903+ var obj = EvaluateExp ( exp ) ;
904+ obj = obj . ToNumber ( "It can't be converted to number!" ) ;
905+ if ( obj . IsError ) {
906+ LastError = obj . ErrorMsg ;
907+ return def ;
908+ }
909+ return ( float ) obj . NumberValue ;
910+ } catch ( Exception ex ) {
911+ LastError = ex . Message + "\r \n " + ex . StackTrace ;
912+ }
913+ return def ;
914+ }
915+ /// <summary>
916+ /// 执行函数,如果异常,返回默认值
917+ /// </summary>
918+ /// <param name="exp"></param>
919+ /// <param name="def"></param>
920+ /// <returns></returns>
921+ public double TryEvaluateExp ( string exp , double def )
922+ {
923+ try {
924+ var obj = EvaluateExp ( exp ) ;
925+ obj = obj . ToNumber ( "It can't be converted to number!" ) ;
926+ if ( obj . IsError ) {
927+ LastError = obj . ErrorMsg ;
928+ return def ;
929+ }
930+ return obj . NumberValue ;
931+ } catch ( Exception ex ) {
932+ LastError = ex . Message + "\r \n " + ex . StackTrace ;
933+ }
934+ return def ;
935+ }
936+ /// <summary>
937+ /// 执行函数,如果异常,返回默认值
938+ /// </summary>
939+ /// <param name="exp"></param>
940+ /// <param name="def"></param>
941+ /// <returns></returns>
942+ public decimal TryEvaluateExp ( string exp , decimal def )
943+ {
944+ try {
945+ var obj = EvaluateExp ( exp ) ;
946+ obj = obj . ToNumber ( "It can't be converted to number!" ) ;
947+ if ( obj . IsError ) {
948+ LastError = obj . ErrorMsg ;
949+ return def ;
950+ }
951+ return ( decimal ) obj . NumberValue ;
952+ } catch ( Exception ex ) {
953+ LastError = ex . Message + "\r \n " + ex . StackTrace ;
954+ }
955+ return def ;
956+ }
957+ /// <summary>
958+ /// 执行函数,如果异常,返回默认值
959+ /// </summary>
960+ /// <param name="exp"></param>
961+ /// <param name="def"></param>
962+ /// <returns></returns>
963+ public string TryEvaluateExp ( string exp , string def )
964+ {
965+ try {
966+ var obj = EvaluateExp ( exp ) ;
967+ if ( obj . IsNull ) {
968+ return null ;
969+ }
970+ obj = obj . ToText ( "It can't be converted to string!" ) ;
971+ if ( obj . IsError ) {
972+ LastError = obj . ErrorMsg ;
973+ return def ;
974+ }
975+ return obj . TextValue ;
976+ } catch ( Exception ex ) {
977+ LastError = ex . Message + "\r \n " + ex . StackTrace ;
978+ }
979+ return def ;
980+ }
981+ /// <summary>
982+ /// 执行函数,如果异常,返回默认值
983+ /// </summary>
984+ /// <param name="exp"></param>
985+ /// <param name="def"></param>
986+ /// <returns></returns>
987+ public bool TryEvaluateExp ( string exp , bool def )
988+ {
989+ try {
990+ var obj = EvaluateExp ( exp ) ;
991+ obj = obj . ToBoolean ( "It can't be converted to bool!" ) ;
992+ if ( obj . IsError ) {
993+ LastError = obj . ErrorMsg ;
994+ return def ;
995+ }
996+ return obj . BooleanValue ;
997+ } catch ( Exception ex ) {
998+ LastError = ex . Message + "\r \n " + ex . StackTrace ;
999+ }
1000+ return def ;
1001+ }
1002+ /// <summary>
1003+ /// 执行函数,如果异常,返回默认值
1004+ /// </summary>
1005+ /// <param name="exp"></param>
1006+ /// <param name="def"></param>
1007+ /// <returns></returns>
1008+ public DateTime TryEvaluateExp ( string exp , DateTime def )
1009+ {
1010+ try {
1011+ var obj = EvaluateExp ( exp ) ;
1012+ obj = obj . ToMyDate ( "It can't be converted to MyDate!" ) ;
1013+ if ( obj . IsError ) {
1014+ LastError = obj . ErrorMsg ;
1015+ return def ;
1016+ }
1017+ return ( DateTime ) obj . DateValue ;
1018+ } catch ( Exception ex ) {
1019+ LastError = ex . Message + "\r \n " + ex . StackTrace ;
1020+ }
1021+ return def ;
1022+ }
1023+ /// <summary>
1024+ /// 执行函数,如果异常,返回默认值
1025+ /// </summary>
1026+ /// <param name="exp"></param>
1027+ /// <param name="def"></param>
1028+ /// <returns></returns>
1029+ public TimeSpan TryEvaluateExp ( string exp , TimeSpan def )
1030+ {
1031+ try {
1032+ var obj = EvaluateExp ( exp ) ;
1033+ obj = obj . ToMyDate ( "It can't be converted to MyDate!" ) ;
1034+ if ( obj . IsError ) {
1035+ LastError = obj . ErrorMsg ;
1036+ return def ;
1037+ }
1038+ return ( TimeSpan ) obj . DateValue ;
1039+ } catch ( Exception ex ) {
1040+ LastError = ex . Message + "\r \n " + ex . StackTrace ;
1041+ }
1042+ return def ;
1043+ }
1044+ /// <summary>
1045+ /// 执行函数,如果异常,返回默认值。
1046+ /// 解决 def 为 null 二义性问题
1047+ /// </summary>
1048+ /// <param name="exp"></param>
1049+ /// <param name="def"></param>
1050+ /// <returns></returns>
1051+ public MyDate TryEvaluateExp_MyDate ( string exp , MyDate def )
1052+ {
1053+ try {
1054+ var obj = EvaluateExp ( exp ) ;
1055+ obj = obj . ToMyDate ( "It can't be converted to MyDate!" ) ;
1056+ if ( obj . IsError ) {
1057+ LastError = obj . ErrorMsg ;
1058+ return def ;
1059+ }
1060+ return obj . DateValue ;
1061+ } catch ( Exception ex ) {
1062+ LastError = ex . Message + "\r \n " + ex . StackTrace ;
1063+ }
1064+ return def ;
1065+ }
1066+
1067+
1068+
7651069 #endregion
7661070
7671071 #region EvaluateFormula
@@ -796,7 +1100,7 @@ public virtual String EvaluateFormula(String formula, List<char> splitChars)
7961100 } else {
7971101 String d = "" ;
7981102 try {
799- Operand operand = EvaluateOnce ( s ) ;
1103+ Operand operand = EvaluateExp ( s ) ;
8001104 d = operand . ToText ( ) . TextValue ;
8011105 } catch ( Exception ) { }
8021106 stringBuilder . Append ( d ) ;
@@ -809,7 +1113,7 @@ public virtual String EvaluateFormula(String formula, List<char> splitChars)
8091113 /// </summary>
8101114 /// <param name="exp"></param>
8111115 /// <returns></returns>
812- public Operand EvaluateOnce ( string exp )
1116+ public Operand EvaluateExp ( string exp )
8131117 {
8141118 ProgContext context = Parse ( exp ) ;
8151119 if ( context == null ) {
0 commit comments