溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

MethodCallExpression和Expression.Compile()正確的使用方法

發(fā)布時間:2021-06-29 14:48:20 來源:億速云 閱讀:630 作者:chen 欄目:編程語言

這篇文章主要介紹“MethodCallExpression和Expression.Compile()正確的使用方法”,在日常操作中,相信很多人在MethodCallExpression和Expression.Compile()正確的使用方法問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”MethodCallExpression和Expression.Compile()正確的使用方法”的疑惑有所幫助!接下來,請跟著小編一起來學(xué)習(xí)吧!

            Expression<Func<int,int>> expmethod = ep=>ep+50;            
            //LambdaExpression lbda = Expression.Lambda<Func<int,int>>(expmethod.Body,Expression.Parameter(typeof(int)));
            //var lbdadel = lbda.Compile();
            //Console.WriteLine("Expression<Func<int,int>> expmethod = ep=>ep+50 = " + lbdadel.Invoke(4));//.DynamicInvoke(4));
            //結(jié)果運(yùn)行時發(fā)現(xiàn):
            //var lbdadel = lbda.Compile();這一句出現(xiàn):System.InvalidoperationException類型的未經(jīng)處理的異常在System.Core.dll中發(fā)生
            //其他信息:從作用域""引用了"System.Int32"類型的變量"ep",但該變量未定義

            //其實(shí)這樣將Expression<Func<...>> exp = ep=>ep+50;這種表達(dá)式再用Expression.Lambda(...)創(chuàng)建Lambda表達(dá)式的做法是不正確的,
            //因?yàn)镋xpression<Func<...>> exp = ep=>ep+50 所定義的exp本來就是一個LambdaExpression,這個時候只能用LambdaExpression類
            //的Compile()直接進(jìn)行編譯才是正確的做法
            var lbdadel = expmethod.Compile();
            Console.WriteLine("Expression<Func<int,int>> expmethod = ep=>ep+50 = " + lbdadel.DynamicInvoke(4));

以上作法是針對Expression<Func/Action>> exp = exp=>xxxxxx;這種形式的表達(dá)式進(jìn)行Compile()再進(jìn)行調(diào)用從而得出結(jié)果值,如果是一個類中的方法,我有這個類的對象變量,如何通過表達(dá)式形式進(jìn)行調(diào)用呢?

class ClassMethodCallExpression:IMethodCallExpression
    {

        public int methodfun(int arg) //接口中的方法的實(shí)現(xiàn)
        {
            return arg + 5;
        }
    }

IMethodCallExpression imce = new ClassMethodCallExpression();
            //Func<MethodCallExpression,int> exp = (ex=>ex.methodfun)
            //MethodInfo無法new出來對象,怎么創(chuàng)建一個MethodInfo呢???
            MethodInfo mi = typeof(Console).GetMethod("WriteLine", new Type[] { typeof(string) });
            ConstantExpression ce = Expression.Constant("3");
            MethodCallExpression mce = Expression.Call(mi, ce);//針對靜態(tài)方法
            Action a = Expression.Lambda<Action>(mce).Compile();  
            a();//也就是Console.WriteLine方法的執(zhí)行

            MethodInfo mi2 = typeof(ClassMethodCallExpression).GetMethod("methodfun",new Type[]{typeof(int)});
            ConstantExpression ce2 = Expression.Constant(5);
            MethodCallExpression mce2 = Expression.Call(Expression.Constant(imce),mi2, ce2);//針對非靜態(tài)方法
            //Expression.Call方法中,無Expression instance則表示將調(diào)用的是靜態(tài)方法
            //當(dāng)MehtodCallExpression準(zhǔn)備調(diào)用實(shí)例方法時,可以通過Expression.Constant(實(shí)例對象變量)創(chuàng)建ConstantExpression,然后將這個ConstantExpression當(dāng)作instance傳參
            //具體以及更復(fù)雜的例子參考 https://blog.csdn.net/weixin_34402090/article/details/85786004
            // MethodCallExpression Expression.Call(Expression instance,MehtodInfo,params Expression[] arguments)或者
            //Expression.Call(MehtodInfo,params Expression[])
            var a2 = Expression.Lambda<Func<int>>(mce2).Compile();
            //MethodCallExpression使用步驟(如果是靜態(tài)方法當(dāng)然不需要目標(biāo)對象變量)
            //1:定義好要用到的目標(biāo)對象變量(因?yàn)樽罱K要執(zhí)行的方法在它所屬的類中)
            //2:定義好要用到的參數(shù)表達(dá)式或常量表達(dá)式等
            //3:通過typeof(目標(biāo)對象所在的類).GetMethod獲得MethodInfo
            //4:利用Expression.Lambda<Action或Func>(MethodCallExpression).compile().Invoke或直接調(diào)用compile好的委托,注意:一定要用Action或Func,
            //也就是說不能Expression.Lambda(...),必須要用Expression.Lambda<Action/Func>形式
            Console.WriteLine("a2=" + a2());

到此,關(guān)于“MethodCallExpression和Expression.Compile()正確的使用方法”的學(xué)習(xí)就結(jié)束了,希望能夠解決大家的疑惑。理論與實(shí)踐的搭配能更好的幫助大家學(xué)習(xí),快去試試吧!若想繼續(xù)學(xué)習(xí)更多相關(guān)知識,請繼續(xù)關(guān)注億速云網(wǎng)站,小編會繼續(xù)努力為大家?guī)砀鄬?shí)用的文章!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI