如何在文本文件中存储和执行类方法列表



我正在C#中运行Selenium测试,我正在寻找一种将测试列表外部化为文本文件的方法,这样就可以轻松地编辑它们,而无需接触代码。问题是如何将文本文件的每一行都作为一个方法调用?操作似乎是最好的解决方案,但我在将文本字符串转换为操作时遇到了问题。我愿意接受所有关于如何最好地做到这一点的建议。谢谢J.

注意:尽管使用反射和调用是解决方案,但当我有不同数量参数的方法时,它就不起作用了。

using McAfeeQA.Core.Log;
using OpenQA.Selenium;
using OpenQA.Selenium.Firefox;
using OpenQA.Selenium.Interactions;
using System;
namespace SeleniumProject
{
class Program
{
    static void Main(string[] args)
    {
        try
        {
            // Read through all tests and run one after another
            // Global.Tests is a list of strings that contains all the tests
            foreach (string testcase in Global.tests)
            {
                // How to run each of the below commented methods in a foreach loop???
            } 
            //Tests.CreateNewAdminUser("admin123", "password", "admin");
            //Navigation.Login(Global.language, Global.username, Global.password);
            //Tests.testPermissionSets();
            //Navigation.Logoff();
        }
        catch (Exception ex)
        {
            Console.WriteLine("Program exception: " + ex.ToString());
        }
    }
}
}

不完美但简化:

private void StoreMetod(string FileName, Type classType)
{
    using (var fileSt = new System.IO.StreamWriter(FileName))
    {
        foreach (var Method in classType.GetType().GetMethods())
        {
            fileSt.Write(Method.Name);
            fileSt.Write("t");
            fileSt.Write(Method.ReturnType == null ? "" : Method.ReturnType.FullName );
            fileSt.Write("t");
            foreach (var prm in Method.GetParameters())
            {
                //ect...
            }
        }
    }
}
private void LoadMethod(string FileName, Object  instant)
{
    using (var fileSt = new System.IO.StreamReader (FileName))
    {
        while (!fileSt.EndOfStream)
        {
            var lineMethodArg = fileSt.ReadLine().Split('t');
            var methodName = lineMethodArg[0];
            var typeReturnName = lineMethodArg[1];
            //set parameters, Return type Ect...
            var objectReturn = instant.GetType().GetMethod(methodName).Invoke(instant, {prms} );
        }
    }
}

最新更新