亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

帶有 Convert 和 TryCatch 的 C# 表達式

帶有 Convert 和 TryCatch 的 C# 表達式

C#
蠱毒傳說 2021-11-21 14:36:20
我需要將兩個輸入雙數相除,并使用 try catch 來處理 DivideByZeroException 等。ParameterExpression dInput1 = Expression.Parameter(typeof(double), "input1");ParameterExpression dInput2 = Expression.Parameter(typeof(double), "input2");ParameterExpression e_DBZ = Expression.Parameter(typeof(DivideByZeroException));ParameterExpression e_ = Expression.Parameter(typeof(Exception));var _calc = Expression.Divide(dInput1, dInput2);MethodInfo _consolewrite = typeof(Console).GetMethod("WriteLine",new Type[] { typeof(string) });MethodCallExpression _output = Expression.Call(null, _consolewrite, Expression.Convert(Expression.Constant(_calc,typeof(double)), typeof(string)));var _catch_DivideByZeroException = Expression.Catch(e_DBZ, Expression.Call(null, _consolewrite, Expression.Property(e_DBZ, "Message")));var _catch_Exception = Expression.Catch(e_, Expression.Call(null, _consolewrite, Expression.Property(e_DBZ, "Message")));var _calc_Block = Expression.Block(_calc, Expression.Constant(0));var _try = Expression.TryCatch(_calc_Block, _catch_DivideByZeroException, _catch_Exception);var _lambda = Expression.Lambda<Func<double,double, double>>(_try, dInput1,dInput2);Console.WriteLine(_lambda.Body);Console.WriteLine(_lambda.Compile().Invoke(1,0));但是 Expression.Convert 會拋出 System.ArgumentException。
查看完整描述

2 回答

?
慕尼黑5688855

TA貢獻1848條經驗 獲得超2個贊

我需要將兩個輸入雙數相除,并使用 try catch 來處理 DivideByZeroException 等。


ParameterExpression dInput1 = Expression.Parameter(typeof(double), "input1");

ParameterExpression dInput2 = Expression.Parameter(typeof(double), "input2");

ParameterExpression e_DBZ = Expression.Parameter(typeof(DivideByZeroException));

ParameterExpression e_ = Expression.Parameter(typeof(Exception));

var _calc = Expression.Divide(dInput1, dInput2);

MethodInfo _consolewrite = typeof(Console).GetMethod("WriteLine",new Type[] { typeof(string) });

MethodCallExpression _output = Expression.Call(null, _consolewrite, Expression.Convert(Expression.Constant(_calc,typeof(double)), typeof(string)));

var _catch_DivideByZeroException = Expression.Catch(e_DBZ, Expression.Call(null, _consolewrite, Expression.Property(e_DBZ, "Message")));

var _catch_Exception = Expression.Catch(e_, Expression.Call(null, _consolewrite, Expression.Property(e_DBZ, "Message")));

var _calc_Block = Expression.Block(_calc, Expression.Constant(0));

var _try = Expression.TryCatch(_calc_Block, _catch_DivideByZeroException, _catch_Exception);

var _lambda = Expression.Lambda<Func<double,double, double>>(_try, dInput1,dInput2);

Console.WriteLine(_lambda.Body);

Console.WriteLine(_lambda.Compile().Invoke(1,0));

但是 Expression.Convert 會拋出 System.ArgumentException。


查看完整回答
反對 回復 2021-11-21
?
慕森卡

TA貢獻1806條經驗 獲得超8個贊

這有什么問題?


try

{

  var e_DBZ = dInput1 / dInput2;

}


catch (Exception e)

{

  // Handle Divide by Zero error

  Console.WriteLine(e.Message);

}


查看完整回答
反對 回復 2021-11-21
  • 2 回答
  • 0 關注
  • 206 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號