C#方法为变量

时间:2015-11-17 09:03:01

标签: c# variables selenium-webdriver

我在C#Visual Studio中使用Selenium测试代码。我有几个测试类和一个Utility类。

using ...
namespace Test
{
    [TestClass]
    public void Test1()
    {
        //CODE
    }
    [TestClass]
    public void Test2()
    {
        //CODE
    }
    [TestClass]
    public void Test3()
    {
        //CODE
    }
    [TestClass]
    public void Utility()
    {
        IWebDriver driver;
        int a,b,c;
        string name, password;
        srting[] info = {"a","b","c"};
        //other variables
    }
}

我的问题很简单(几乎没有相似的问题,但它们对我来说不会像Dynamically assign method / Method as variable中的decaration那样起作用)。是否有可能在类Utility中从我的变量创建方法,以便在其他类中将它们用作变量而不必一直使用它们?或者我可以做其他事情吗?

请求帮助 Janer

1 个答案:

答案 0 :(得分:1)

四人帮所青睐的一个解决方案是"赞成组合而非继承"这需要:

[TestClass]
public void Test3()
{
    //CODE
}
[TestClass]
public void Utility()
{
    IWebDriver driver;
    int a,b,c;
    string name, password;
    srting[] info = {"a","b","c"};
    //other variables
}

并将其转换为:

[TestClass]
public void Test3()
{
    var utility = new Utility();
    //do the test
}

新的实用程序类看起来像这样:

 public class Utility()
 {
    IWebDriver driver {get;set;}
    int a {get;set; int b {get;set;} int c {get;set;};
    string name {get;set;} string password {get;set;}
    string[] info {get{return new string[]{"a","b","c"}};
    //other variables
 }