@@ -19,18 +19,25 @@ public abstract class Operand : IDisposable
1919 /// False 值
2020 /// </summary>
2121 public static readonly Operand False = Operand . Create ( false ) ;
22+ private bool _isError = false ;
23+ private string _errorMsg = null ;
24+
25+ public Operand ( ) { }
26+ public Operand ( string errMsg ) { _isError = true ; _errorMsg = errMsg ; }
27+
28+
2229 /// <summary>
2330 /// 是否为空
2431 /// </summary>
2532 public virtual bool IsNull => false ;
2633 /// <summary>
2734 /// 是否出错
2835 /// </summary>
29- public virtual bool IsError => false ;
36+ public virtual bool IsError => _isError ;
3037 /// <summary>
3138 /// 错误信息
3239 /// </summary>
33- public virtual string ErrorMsg => null ;
40+ public virtual string ErrorMsg => _errorMsg ;
3441 /// <summary>
3542 /// 操作数类型
3643 /// </summary>
@@ -99,7 +106,7 @@ public static Operand Create(int obj)
99106 /// <returns></returns>
100107 public static Operand Create ( long obj )
101108 {
102- return new OperandNumber ( ( double ) obj ) ;
109+ return new OperandNumber ( ( double ) obj ) ;
103110 }
104111 /// <summary>
105112 /// 创建操作数
@@ -108,7 +115,7 @@ public static Operand Create(long obj)
108115 /// <returns></returns>
109116 public static Operand Create ( ushort obj )
110117 {
111- return new OperandNumber ( ( double ) obj ) ;
118+ return new OperandNumber ( ( double ) obj ) ;
112119 }
113120 /// <summary>
114121 /// 创建操作数
@@ -117,7 +124,7 @@ public static Operand Create(ushort obj)
117124 /// <returns></returns>
118125 public static Operand Create ( uint obj )
119126 {
120- return new OperandNumber ( ( double ) obj ) ;
127+ return new OperandNumber ( ( double ) obj ) ;
121128 }
122129 /// <summary>
123130 /// 创建操作数
@@ -126,7 +133,7 @@ public static Operand Create(uint obj)
126133 /// <returns></returns>
127134 public static Operand Create ( ulong obj )
128135 {
129- return new OperandNumber ( ( double ) obj ) ;
136+ return new OperandNumber ( ( double ) obj ) ;
130137 }
131138 /// <summary>
132139 /// 创建操作数
@@ -135,7 +142,7 @@ public static Operand Create(ulong obj)
135142 /// <returns></returns>
136143 public static Operand Create ( float obj )
137144 {
138- return new OperandNumber ( ( double ) obj ) ;
145+ return new OperandNumber ( ( double ) obj ) ;
139146 }
140147 /// <summary>
141148 /// 创建操作数
@@ -153,8 +160,8 @@ public static Operand Create(double obj)
153160 /// <returns></returns>
154161 public static Operand Create ( decimal obj )
155162 {
156- return new OperandNumber ( ( double ) obj ) ;
157- }
163+ return new OperandNumber ( ( double ) obj ) ;
164+ }
158165 #endregion
159166
160167 /// <summary>
@@ -164,7 +171,8 @@ public static Operand Create(decimal obj)
164171 /// <returns></returns>
165172 public static Operand Create ( string obj )
166173 {
167- if ( object . Equals ( null , obj ) ) {
174+ if ( object . Equals ( null , obj ) )
175+ {
168176 return Operand . CreateNull ( ) ;
169177 }
170178 return new OperandString ( obj ) ;
@@ -176,11 +184,14 @@ public static Operand Create(string obj)
176184 /// <returns></returns>
177185 public static Operand CreateJson ( string txt )
178186 {
179- if ( ( txt . StartsWith ( "{" ) && txt . EndsWith ( "}" ) ) || ( txt . StartsWith ( "[" ) && txt . EndsWith ( "]" ) ) ) {
180- try {
187+ if ( ( txt . StartsWith ( "{" ) && txt . EndsWith ( "}" ) ) || ( txt . StartsWith ( "[" ) && txt . EndsWith ( "]" ) ) )
188+ {
189+ try
190+ {
181191 var json = JsonMapper . ToObject ( txt ) ;
182192 return Operand . Create ( json ) ;
183- } catch ( Exception ) { }
193+ }
194+ catch ( Exception ) { }
184195 }
185196 return Operand . Error ( "string to json is error!" ) ;
186197 }
@@ -237,7 +248,8 @@ public static Operand Create(List<Operand> obj)
237248 public static Operand Create ( ICollection < string > obj )
238249 {
239250 var array = new List < Operand > ( ) ;
240- foreach ( var item in obj ) {
251+ foreach ( var item in obj )
252+ {
241253 array . Add ( Create ( item ) ) ;
242254 }
243255 return new OperandArray ( array ) ;
@@ -250,7 +262,8 @@ public static Operand Create(ICollection<string> obj)
250262 public static Operand Create ( ICollection < double > obj )
251263 {
252264 var array = new List < Operand > ( ) ;
253- foreach ( var item in obj ) {
265+ foreach ( var item in obj )
266+ {
254267 array . Add ( Create ( item ) ) ;
255268 }
256269 return new OperandArray ( array ) ;
@@ -263,7 +276,8 @@ public static Operand Create(ICollection<double> obj)
263276 public static Operand Create ( ICollection < int > obj )
264277 {
265278 var array = new List < Operand > ( ) ;
266- foreach ( var item in obj ) {
279+ foreach ( var item in obj )
280+ {
267281 array . Add ( Create ( item ) ) ;
268282 }
269283 return new OperandArray ( array ) ;
@@ -276,7 +290,8 @@ public static Operand Create(ICollection<int> obj)
276290 public static Operand Create ( ICollection < bool > obj )
277291 {
278292 var array = new List < Operand > ( ) ;
279- foreach ( var item in obj ) {
293+ foreach ( var item in obj )
294+ {
280295 array . Add ( Create ( item ) ) ;
281296 }
282297 return new OperandArray ( array ) ;
@@ -310,13 +325,20 @@ public Operand ToNumber(string errorMessage = null)
310325 if ( Type == OperandType . NUMBER ) { return this ; }
311326 if ( IsError ) { return this ; }
312327 if ( Type == OperandType . BOOLEAN ) { return Create ( BooleanValue ? 1.0 : 0.0 ) ; }
313- if ( Type == OperandType . DATE ) { return Create ( ( double ) DateValue ) ; }
314- if ( Type == OperandType . STRING ) {
315- if ( double . TryParse ( TextValue , NumberStyles . Any , cultureInfo , out double d ) ) {
328+ if ( Type == OperandType . DATE ) { return Create ( ( double ) DateValue ) ; }
329+ if ( Type == OperandType . STRING )
330+ {
331+ if ( double . TryParse ( TextValue , NumberStyles . Any , cultureInfo , out double d ) )
332+ {
316333 return Create ( d ) ;
317334 }
318335 }
319- return Error ( errorMessage ) ;
336+ _isError = true ;
337+ if ( null == _errorMsg )
338+ {
339+ _errorMsg = errorMessage ;
340+ }
341+ return this ;
320342 }
321343 /// <summary>
322344 /// 转bool类型
@@ -328,14 +350,20 @@ public Operand ToBoolean(string errorMessage = null)
328350 if ( Type == OperandType . BOOLEAN ) { return this ; }
329351 if ( IsError ) { return this ; }
330352 if ( Type == OperandType . NUMBER ) { return Create ( NumberValue != 0 ) ; }
331- if ( Type == OperandType . DATE ) { return Create ( ( ( double ) DateValue ) != 0 ) ; }
332- if ( Type == OperandType . STRING ) {
353+ if ( Type == OperandType . DATE ) { return Create ( ( ( double ) DateValue ) != 0 ) ; }
354+ if ( Type == OperandType . STRING )
355+ {
333356 if ( TextValue . Equals ( "true" , StringComparison . OrdinalIgnoreCase ) ) { return Create ( true ) ; }
334357 if ( TextValue . Equals ( "false" , StringComparison . OrdinalIgnoreCase ) ) { return Create ( false ) ; }
335358 if ( TextValue . Equals ( "1" , StringComparison . OrdinalIgnoreCase ) ) { return Create ( true ) ; }
336359 if ( TextValue . Equals ( "0" , StringComparison . OrdinalIgnoreCase ) ) { return Create ( false ) ; }
337360 }
338- return Error ( errorMessage ) ;
361+ _isError = true ;
362+ if ( null == _errorMsg )
363+ {
364+ _errorMsg = errorMessage ;
365+ }
366+ return this ;
339367 }
340368 /// <summary>
341369 /// 转String类型
@@ -350,7 +378,12 @@ public Operand ToText(string errorMessage = null)
350378 if ( Type == OperandType . BOOLEAN ) { return Create ( BooleanValue ? "TRUE" : "FALSE" ) ; }
351379 if ( Type == OperandType . DATE ) { return Create ( DateValue . ToString ( ) ) ; }
352380
353- return Error ( errorMessage ) ;
381+ _isError = true ;
382+ if ( null == _errorMsg )
383+ {
384+ _errorMsg = errorMessage ;
385+ }
386+ return this ;
354387 }
355388 /// <summary>
356389 /// 转Date类型
@@ -361,12 +394,18 @@ public Operand ToDate(string errorMessage = null)
361394 {
362395 if ( Type == OperandType . DATE ) { return this ; }
363396 if ( IsError ) { return this ; }
364- if ( Type == OperandType . NUMBER ) { return Create ( ( Date ) NumberValue ) ; }
365- if ( Type == OperandType . STRING ) {
397+ if ( Type == OperandType . NUMBER ) { return Create ( ( Date ) NumberValue ) ; }
398+ if ( Type == OperandType . STRING )
399+ {
366400 if ( TimeSpan . TryParse ( TextValue , cultureInfo , out TimeSpan t ) ) { return Create ( new Date ( t ) ) ; }
367401 if ( DateTime . TryParse ( TextValue , cultureInfo , DateTimeStyles . None , out DateTime d ) ) { return Create ( new Date ( d ) ) ; }
368402 }
369- return Error ( errorMessage ) ;
403+ _isError = true ;
404+ if ( null == _errorMsg )
405+ {
406+ _errorMsg = errorMessage ;
407+ }
408+ return this ;
370409 }
371410 /// <summary>
372411 /// 转Json类型
@@ -377,16 +416,25 @@ public Operand ToJson(string errorMessage = null)
377416 {
378417 if ( Type == OperandType . JSON ) { return this ; }
379418 if ( IsError ) { return this ; }
380- if ( Type == OperandType . STRING ) {
419+ if ( Type == OperandType . STRING )
420+ {
381421 var txt = TextValue ;
382- if ( ( txt . StartsWith ( "{" ) && txt . EndsWith ( "}" ) ) || ( txt . StartsWith ( "[" ) && txt . EndsWith ( "]" ) ) ) {
383- try {
422+ if ( ( txt . StartsWith ( "{" ) && txt . EndsWith ( "}" ) ) || ( txt . StartsWith ( "[" ) && txt . EndsWith ( "]" ) ) )
423+ {
424+ try
425+ {
384426 var json = JsonMapper . ToObject ( txt ) ;
385427 return Operand . Create ( json ) ;
386- } catch ( Exception ) { }
428+ }
429+ catch ( Exception ) { }
387430 }
388431 }
389- return Error ( errorMessage ) ;
432+ _isError = true ;
433+ if ( null == _errorMsg )
434+ {
435+ _errorMsg = errorMessage ;
436+ }
437+ return this ;
390438 }
391439 /// <summary>
392440 /// 转Array类型
@@ -397,10 +445,13 @@ public Operand ToArray(string errorMessage = null)
397445 {
398446 if ( Type == OperandType . ARRARY ) { return this ; }
399447 if ( IsError ) { return this ; }
400- if ( Type == OperandType . JSON ) {
401- if ( JsonValue . IsArray ) {
448+ if ( Type == OperandType . JSON )
449+ {
450+ if ( JsonValue . IsArray )
451+ {
402452 List < Operand > list = new List < Operand > ( ) ;
403- foreach ( JsonData v in JsonValue ) {
453+ foreach ( JsonData v in JsonValue )
454+ {
404455 if ( v . IsString )
405456 list . Add ( Operand . Create ( v . StringValue ) ) ;
406457 else if ( v . IsBoolean )
@@ -415,7 +466,27 @@ public Operand ToArray(string errorMessage = null)
415466 return Create ( list ) ;
416467 }
417468 }
418- return Error ( errorMessage ) ;
469+ _isError = true ;
470+ if ( null == _errorMsg )
471+ {
472+ _errorMsg = errorMessage ;
473+ }
474+ return this ;
475+ }
476+
477+ /// <summary>
478+ /// 设置失败
479+ /// </summary>
480+ /// <param name="errorMessage"></param>
481+ /// <returns></returns>
482+ public Operand SetError ( string errorMessage )
483+ {
484+ _isError = true ;
485+ if ( null == _errorMsg )
486+ {
487+ _errorMsg = errorMessage ;
488+ }
489+ return this ;
419490 }
420491
421492 void IDisposable . Dispose ( ) { }
@@ -425,40 +496,40 @@ void IDisposable.Dispose() { }
425496 #region number
426497 public static implicit operator Operand ( Int16 obj )
427498 {
428- return Operand . Create ( ( int ) obj ) ;
499+ return Operand . Create ( ( int ) obj ) ;
429500 }
430501 public static implicit operator Operand ( Int32 obj )
431502 {
432503 return Operand . Create ( obj ) ;
433504 }
434505 public static implicit operator Operand ( Int64 obj )
435506 {
436- return Operand . Create ( ( double ) obj ) ;
507+ return Operand . Create ( ( double ) obj ) ;
437508 }
438509 public static implicit operator Operand ( UInt16 obj )
439510 {
440- return Operand . Create ( ( double ) obj ) ;
511+ return Operand . Create ( ( double ) obj ) ;
441512 }
442513 public static implicit operator Operand ( UInt32 obj )
443514 {
444- return Operand . Create ( ( double ) obj ) ;
515+ return Operand . Create ( ( double ) obj ) ;
445516 }
446517 public static implicit operator Operand ( UInt64 obj )
447518 {
448- return Operand . Create ( ( double ) obj ) ;
519+ return Operand . Create ( ( double ) obj ) ;
449520 }
450521
451522 public static implicit operator Operand ( float obj )
452523 {
453- return Operand . Create ( ( double ) obj ) ;
524+ return Operand . Create ( ( double ) obj ) ;
454525 }
455526 public static implicit operator Operand ( double obj )
456527 {
457528 return Operand . Create ( obj ) ;
458529 }
459530 public static implicit operator Operand ( decimal obj )
460531 {
461- return Operand . Create ( ( double ) obj ) ;
532+ return Operand . Create ( ( double ) obj ) ;
462533 }
463534 #endregion
464535
@@ -499,7 +570,7 @@ public static implicit operator Operand(List<double> obj)
499570 abstract class Operand < T > : Operand
500571 {
501572 public T Value { get ; private set ; }
502- public Operand ( T obj )
573+ public Operand ( T obj ) : base ( )
503574 {
504575 Value = obj ;
505576 }
@@ -509,7 +580,7 @@ class OperandNumber : Operand<double>
509580 {
510581 public OperandNumber ( double obj ) : base ( obj ) { }
511582 public override OperandType Type => OperandType . NUMBER ;
512- public override int IntValue => ( int ) Value ;
583+ public override int IntValue => ( int ) Value ;
513584 public override double NumberValue => Value ;
514585 }
515586 class OperandBoolean : Operand < bool >
@@ -545,12 +616,8 @@ public OperandArray(List<Operand> obj) : base(obj) { }
545616 class OperandError : Operand
546617 {
547618 public override OperandType Type => OperandType . ERROR ;
548- public override bool IsError => true ;
549- private string _errorMsg ;
550- public override string ErrorMsg => _errorMsg ;
551- public OperandError ( string msg )
619+ public OperandError ( string msg ) : base ( msg )
552620 {
553- _errorMsg = msg ;
554621 }
555622 }
556623
0 commit comments