【问题标题】:check C# Action/Lambda/Delegate contains any code/statements检查 C# Action/Lambda/Delegate 是否包含任何代码/语句
【发布时间】:2009-09-30 05:11:25
【问题描述】:

谁能告诉我是否有办法查看一个动作是否包含任何代码?

Action x = new Action(()=>
             {

             });

应该返回false,而

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

应该返回真。

也许使用反射?

【问题讨论】:

    标签: c# reflection delegates lambda


    【解决方案1】:

    也许这会有所帮助:

            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(空方法体)只有 2 个字节,值 0 和 42 表示 nopreturn,我认为。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-10-02
      • 1970-01-01
      • 2019-03-18
      • 2020-01-17
      • 1970-01-01
      • 2010-12-17
      • 1970-01-01
      相关资源
      最近更新 更多