简体   繁体   中英

check C# Action/Lambda/Delegate contains any code/statements

Can anyone tell me if there a way to see if an action contains any code?

Action x = new Action(()=>
             {

             });

should return false, while

Action x = new Action(()=>
             {
                var x = "i am a string" 
             });

should return true.

Perhaps using reflection?

Maybe this will help:

        Action x = new Action(() =>
        {
            var xx = "i am a string";
        });


        Action x1 = new Action(() =>
        {

        });

        MethodBody mb = x.Method.GetMethodBody();
        MethodBody mb1 = x1.Method.GetMethodBody();

        byte[] b = mb.GetILAsByteArray();
        byte[] b1 = mb1.GetILAsByteArray();

b1 (empty method body) has only 2 bytes, values 0 and 42 meaning nop and return , I think.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM