解析使用反射功能/方法内容 [英] Parsing function / method content using Reflection
问题描述
我的单元测试框架由TestFixtures,TestMethods和操作。行动就在里面TestMethod的额外的小容器,操作来源于写在我们公司内部的DLL。动作在里面一样,方法使用的:
My unit testing frameworks consists of TestFixtures, TestMethods and Actions. Action is additional smaller container inside TestMethod, Actions comes from internal Dll written in our company. Actions are used inside methods like that:
[Test]
void TestMethod1()
{
Run(new Sleep { Seconds = 10 } );
}
我必须写一个应用程序,它收集装置的所有信息,测试从DLL行动。我已经找到了如何使用类型/方法的属性来枚举通过反射测试装置和试验方法。
I have to write an application, which collect all the information about fixtures, tests and actions from DLL. I have found how to enumerate test fixtures and test methods by reflection using type / method attributes.
不过,我不知道如何枚举内部测试方法的行动。
But I have no idea how enumerate actions inside test methods.
能否请你帮忙吗? ?是否有可能使用反射都做
Could you please help? Is it possible to do using reflection at all?
更新:
见接受的答案。真的很酷库。您还可以看看这里( WPF:在MVVM TreeView控件绑定方法教程一步一步),如果你有兴趣,我怎么创造了夹具,测试和行动的实体模型,并在MVVM方式TreeView控件绑定。
UPDATED: See the accepted answer. Really cool library. Also you can look here ( WPF: Binding TreeView in MVVM way step by step tutorial ), if you are interested in how I created entity model for fixtures, tests and actions, and binded in MVVM way to TreeView.
推荐答案
谢谢,阿列克谢Levenkov!最后我发现用你的小费的解决方案。共享。从 https://github.com/>下载和参考Mono.Reflection.dll - 你唯一应该做的。jbevain / mono.reflection
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);
}
}
}
}
}
}
这篇关于解析使用反射功能/方法内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!