国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

表達式:使用API創建表達式樹(3)

2019-11-17 03:18:18
字體:
來源:轉載
供稿:網友

表達式:使用API創建表達式樹(3)

一、DebugInfoExPRession:發出或清除調試信息的序列點。 這允許調試器在調試時突出顯示正確的源代碼。

        static void Main(string[] args)        {            var asm = AppDomain.CurrentDomain.DefineDynamicAssembly(new AssemblyName("foo"), System.Reflection.Emit.AssemblyBuilderaccess.RunAndSave);            var mod = asm.DefineDynamicModule("mymod", true);            var type = mod.DefineType("baz", TypeAttributes.Public);            var meth = type.DefineMethod("go", MethodAttributes.Public | MethodAttributes.Static);            var sdi = Expression.SymbolDocument("TestDebug.cs");            var di = Expression.DebugInfo(sdi, 2, 2, 2, 12);            var gen = DebugInfoGenerator.CreatePdbGenerator();            var exp = Expression.Divide(Expression.Constant(2), Expression.Subtract(Expression.Constant(4), Expression.Constant(4)));            var block = Expression.Block(di, exp);            Expression.Lambda(block, new ParameterExpression[0]).CompileToMethod(meth, gen);            var newtype = type.CreateType();            asm.Save("tmp.dll");            newtype.GetMethod("go").Invoke(null, new object[0]);            Console.WriteLine(" ");        }

運行了下: 未經處理的異常: System.Reflection.TargetInvocationException: 調用的目標發生了異 常。 ---> System.DivideByZeroException: 嘗試除以零。 在 baz.go() 位置 TestDebug.cs:行號 2 --- 內部異常堆棧跟蹤的結尾 --- 在 System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor) 在 System.Reflection.RuntimeMethodInfo.UnsafeInvokeInternal(Object obj, Objec t[] parameters, Object[] arguments) 在 System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invoke Attr, Binder binder, Object[] parameters, CultureInfo culture) 在 System.Reflection.MethodBase.Invoke(Object obj, Object[] parameters) 在 Consoleapplication2.Program.Main(String[] args) 位置 d:/平臺/演示程序/Debu gInfoExpressionApplication2/ConsoleApplication2/Program.cs:行號 72那個 “在 baz.go() 位置 TestDebug.cs:行號 2”就是DebugInfoExpression產生的效果,試了幾種其他的方法,不能出現類似的效果,說Debug info 只會出現在編譯過的方法。DebugInfoExpression用的機會也不多,就沒深究了

二、DefaultExpression :表示類型或空表達式的默認值。有點類似泛型的默認值操作,沒什么難度:下面摘自MSDN

Expression defaultExpr = Expression.Default(                            typeof(byte)                        );// 顯示表達式Console.WriteLine(defaultExpr.ToString());// 創建表達式樹,并執行Console.WriteLine(    Expression.Lambda<Func<byte>>(defaultExpr).Compile()());// 顯示結果://// default(Byte)// 0


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 民丰县| 温宿县| 汕头市| 兰考县| 都匀市| 上栗县| 萨嘎县| 江城| 静海县| 平定县| 呼和浩特市| 青海省| 阿图什市| 安塞县| 都匀市| 桐乡市| 塔城市| 海门市| 江孜县| 永昌县| 霍州市| 阜新| 吉木萨尔县| 长治县| 治多县| 固阳县| 丰镇市| 绍兴县| 余姚市| 固镇县| 河津市| 永康市| 大竹县| 武城县| 容城县| 桂东县| 汝南县| 中牟县| 桐柏县| 永泰县| 南开区|