C# 使用枚举和字符串值

C# 使用枚举和字符串值,c#,.net,selenium,nunit,specflow,C#,.net,Selenium,Nunit,Specflow,我正在使用specflow和selenium,我正在制作一个表格来组织网站中的用户输入 | Input1 | Input2 | Input3 | | Opt1 | OptX | Opt2 | 我为输入建立了一个类: public class ObjectDTO { public string Input1; public Input2Type Input2; public string Input3; } 以及一个特定opt的枚举(因为它是静态输入) 当

我正在使用specflow和selenium,我正在制作一个表格来组织网站中的用户输入

| Input1 | Input2 | Input3 |
| Opt1   | OptX   | Opt2   |
我为输入建立了一个类:

public class ObjectDTO 
{
    public string Input1;
    public Input2Type Input2;
    public string Input3;
}
以及一个特定opt的枚举(因为它是静态输入)

当我得到输入时,我尝试实例化对象:

ObjectDTO stocks = table.CreateInstance<ObjectDTO>();
ObjectDTO stocks=table.CreateInstance();

但是它说“找不到值为OptX的枚举”。

不幸的是,table.CreateInstance()是一个相当简单的方法,对于枚举(以及其他方法)来说失败。最后,我为表编写了一个扩展方法,该方法使用反射来查询它试图创建的对象实例。这种方法的优点是,该方法只覆盖表列中指定的实例值,但是,您必须为表的每一行调用它,并传入一个已实例化的实例。可以很容易地修改它,使其与CreateInstance方法一样工作,但就我的目的而言,它工作得更好

public static class TableExtensions
{
  public static void FillInstance(this Table table, TableRow tableRow, object instance) {
    var propertyInfos = instance.GetType().GetProperties();
    table.Header.Each(header => Assert.IsTrue(propertyInfos.Any(pi => pi.Name == header), "Expected to find the property [{0}] on the object of type [{1}]".FormatWith(header, instance.GetType().Name)));
    var headerProperties = table.Header.Select(header => propertyInfos.Single(p => p.Name == header)).ToList();
    foreach (var propertyInfo in headerProperties) {
      object propertyValue = tableRow[propertyInfo.Name];
      var propertyType = propertyInfo.PropertyType;
      if (propertyType.IsGenericType && propertyType.GetGenericTypeDefinition() == typeof(Nullable<>)) {
        propertyType = propertyType.GetGenericArguments().Single();
      }

      var parse = propertyType.GetMethod("Parse", new[] { typeof(string) });
      if (parse != null) {
        // ReSharper disable RedundantExplicitArrayCreation
        try {
          propertyValue = propertyType.Name.Equals("DateTime") ? GeneralTransformations.TranslateDateTimeFrom(propertyValue.ToString()) : parse.Invoke(null, new object[] { propertyValue });
        } catch (Exception ex) {
          var message = "{0}\r\nCould not parse value: {2}.{3}(\"{1}\")".FormatWith(ex.Message, propertyValue, parse.ReturnType.FullName, parse.Name);
          throw new Exception(message, ex);
        }
        // ReSharper restore RedundantExplicitArrayCreation
      }

      propertyInfo.SetValue(instance, propertyValue, null);
    }
  }
}

不幸的是,table.CreateInstance()是一个相当幼稚的方法,对于枚举(以及其他)来说失败了。最后,我为表编写了一个扩展方法,该方法使用反射来查询它试图创建的对象实例。这种方法的优点是,该方法只覆盖表列中指定的实例值,但是,您必须为表的每一行调用它,并传入一个已实例化的实例。可以很容易地修改它,使其与CreateInstance方法一样工作,但就我的目的而言,它工作得更好

public static class TableExtensions
{
  public static void FillInstance(this Table table, TableRow tableRow, object instance) {
    var propertyInfos = instance.GetType().GetProperties();
    table.Header.Each(header => Assert.IsTrue(propertyInfos.Any(pi => pi.Name == header), "Expected to find the property [{0}] on the object of type [{1}]".FormatWith(header, instance.GetType().Name)));
    var headerProperties = table.Header.Select(header => propertyInfos.Single(p => p.Name == header)).ToList();
    foreach (var propertyInfo in headerProperties) {
      object propertyValue = tableRow[propertyInfo.Name];
      var propertyType = propertyInfo.PropertyType;
      if (propertyType.IsGenericType && propertyType.GetGenericTypeDefinition() == typeof(Nullable<>)) {
        propertyType = propertyType.GetGenericArguments().Single();
      }

      var parse = propertyType.GetMethod("Parse", new[] { typeof(string) });
      if (parse != null) {
        // ReSharper disable RedundantExplicitArrayCreation
        try {
          propertyValue = propertyType.Name.Equals("DateTime") ? GeneralTransformations.TranslateDateTimeFrom(propertyValue.ToString()) : parse.Invoke(null, new object[] { propertyValue });
        } catch (Exception ex) {
          var message = "{0}\r\nCould not parse value: {2}.{3}(\"{1}\")".FormatWith(ex.Message, propertyValue, parse.ReturnType.FullName, parse.Name);
          throw new Exception(message, ex);
        }
        // ReSharper restore RedundantExplicitArrayCreation
      }

      propertyInfo.SetValue(instance, propertyValue, null);
    }
  }
}

OptX
是您提到的自定义属性的值,您需要一种机制来匹配该值并获取相应的枚举,这可能有助于从specflow-TechTalk.specflow.Table
OptX
中获得一个类,它是您提到的自定义属性的值。您需要一种机制来匹配该值并获取相应的枚举,这可能有助于从specflow-TechTalk.specflow.Table中获得一个类
ObjectDTO objectDTO;
foreach (var tableRow in table.Rows)
{
  objectDTO = table.FillInstance(tableRow, new ObjectDTO());
  //Do individual row processing/testing here...
}