Parsing function / method content using Reflection

前端 未结 3 470
一整个雨季
一整个雨季 2021-01-19 07:25

My unit testing frameworks consists of TestFixtures, TestMethods and Actions. Action is additional smaller container inside TestMethod, Actions comes from internal Dll writt

相关标签:
3条回答
  • 2021-01-19 08:18

    Instead of using reflection, why don't you roll out your own method that will log all Action executions.

    void ExecuteAction(Action action)
    {
       //Log TestFixture, TestMethod, Action
    
       //Execute actual action
    }
    
    [Test]
    void TestMethod1()
    {
        ExecuteAction(Run(new Sleep { Seconds = 10 } ));
    }
    

    ExecuteAction method can be in a base or helper class

    0 讨论(0)
  • 2021-01-19 08:19

    Yes to extent.

    Reflection will give you method body, than you need to disassemble the IL to read method body and obtain any information you want.

    var bytes = mi.GetMethodBody().GetILAsByteArray();
    

    One of possible tools to disassembe is Cecil

    Check out Traverse a c# method and anazlye the method body for more links.

    0 讨论(0)
  • 2021-01-19 08:24

    Thanks, Alexei Levenkov! Finally I have found a solution using your tip. Sharing. The only thing you should do -> download and reference Mono.Reflection.dll from https://github.com/jbevain/mono.reflection.

    using System;
    using System.Linq;
    using System.Reflection;
    using MINT;
    using MbUnit.Framework;
    using Mono.Reflection;
    
    namespace TestDll
    {
        internal class Program
        {
            private static void Main(string[] args)
            {
                const string DllPath = @"d:\SprinterAutomation\Actions.Tests\bin\x86\Debug\Actions.Tests.dll";
                Assembly assembly = Assembly.LoadFrom(DllPath);
    
                // enumerating Fixtures
                foreach (Type fixture in assembly.GetTypes().Where(t => t.GetCustomAttributes(typeof(TestFixtureAttribute), false).Length > 0)) 
                {
                    Console.WriteLine(fixture.Name);
                    // enumerating Test Methods
                    foreach (var testMethod in fixture.GetMethods().Where(m => m.GetCustomAttributes(typeof(TestAttribute), false).Length > 0))
                    {
                        Console.WriteLine("\t" + testMethod.Name);
                        // filtering Actions
                        var instructions = testMethod.GetInstructions().Where(
                            i => i.OpCode.Name.Equals("newobj") && ((ConstructorInfo)i.Operand).DeclaringType.IsSubclassOf(typeof(BaseAction)));
    
                        // enumerating Actions!
                        foreach (Instruction action in instructions)
                        {
                            var constructroInfo = action.Operand as ConstructorInfo;
                            Console.WriteLine("\t\t" + constructroInfo.DeclaringType.Name);
                        }
                    }
                }
    
            }
        }
    }
    
    0 讨论(0)
提交回复
热议问题