C# 枚举无法将类型字符串隐式转换为枚举

C# 枚举无法将类型字符串隐式转换为枚举,c#,selenium-webdriver,specflow,C#,Selenium Webdriver,Specflow,我有一个枚举中的URL列表。我想将枚举值作为参数传递给我的方法,并使用它使用SeleniumWeb驱动程序浏览到URL 错误CS0029无法将类型“string”隐式转换为“automation.Enums.UrlEnums.Url”automation F:\Projects\C\#\automation\Helpers\NavigateHelper.cs 22 Active 我的代码片段是: public class UrlEnums { public enum Url {

我有一个枚举中的URL列表。我想将枚举值作为参数传递给我的方法,并使用它使用SeleniumWeb驱动程序浏览到URL

错误CS0029无法将类型“string”隐式转换为“automation.Enums.UrlEnums.Url”automation F:\Projects\C\#\automation\Helpers\NavigateHelper.cs 22 Active

我的代码片段是:

public class UrlEnums
{
    public enum Url
    {
        [Description("https://www.companysite1.com")] 
        company1,
        [Description("https://www.companysite2.com")] 
        company2
    }
}

public void NavigateTo(UrlEnums.Url url)
{
    switch (url)
    {
        case "Thriller":
            Driver.Navigate().GoToUrl(url);
            //Driver.Navigate().GoToUrl("https://www.companysite1.com");
        break;

        case "Comedy":
            Driver.Navigate().GoToUrl(url);
        break;
    }
}
如何使用枚举作为参数传递并转到URL? 此枚举列表将很快增长并具有更多URL

我的SpecFlow场景是:

Feature: WelcomePage
    In order to view the film categories
    As a user I should be able to click on a category
    I should see the welcome page for the selected category

@smoke 
Scenario Outline: NavigateToFilmCategory
    Given I navigate to <Category> page
    Then I should see the welcome page <WelcomePage>

Examples: TestData
| Category    | WelcomePage     |
| Thriller    | Thriller Films  |
| Comedy      | Comedy Films    |
| Action      | Action Films    |
| Adventure   | Adventure Films |
功能:欢迎访问第页
以查看电影类别
作为一个用户,我应该能够点击一个类别
我应该看到所选类别的欢迎页面
@冒烟
场景大纲:导航电影类别
假设我导航到第页
然后我会看到欢迎页面
示例:TestData
|类别|欢迎第页|
|惊悚片|惊悚片|
|喜剧|喜剧电影|
|动作片|
|冒险电影|

谢谢,

在这种情况下,不需要使用switch case,因为您对不同的值执行相同的操作。如果使用反射查找url值,则可以对该特定url执行操作(导航)

我希望这有助于解决你的问题

public enum UrlEnum
{
    [Description("https://www.companysite1.com")]
    company1,

    [Description("https://www.companysite2.com")]
    company2
}

public void NavigateTo(UrlEnum url)
{
    MemberInfo info = typeof(UrlEnum).GetMember(url.ToString()).First();
    DescriptionAttribute description = info.GetCustomAttribute<DescriptionAttribute>();
    if (description != null)
    {
        //do something like
        Driver.Navigate().GoToUrl(description.Description);
    }
    else
    {
        //do something
    }
}
public enum UrlEnum
{
[说明(”https://www.companysite1.com")]
公司1,
[说明(”https://www.companysite2.com")]
公司2
}
public void NavigateTo(UrlEnum url)
{
MemberInfo=typeof(UrlEnum).GetMember(url.ToString()).First();
DescriptionAttribute description=info.GetCustomAttribute();
if(说明!=null)
{
//做点像
Driver.Navigate().gotour(description.description);
}
其他的
{
//做点什么
}
}

一个好的解决方案应该是这样的:

public class UrlEnums
{
    public enum Url
    {
        [Description("https://www.companysite1.com")] 
        company1,
        [Description("https://www.companysite2.com")] 
        company2
    }
}

public void NavigateTo(UrlEnums.Url url)
{

     Driver.Navigate().GoToUrl(GetEnumDescription(url));

}

    public static string GetEnumDescription(Enum value)
    {
        FieldInfo fi = value.GetType().GetField(value.ToString());

        DescriptionAttribute[] attributes =
            (DescriptionAttribute[])fi.GetCustomAttributes(
            typeof(DescriptionAttribute),
            false);

        if (attributes != null &&
            attributes.Length > 0)
            return attributes[0].Description;
        else
            return value.ToString();
    }


这就是您要查找的内容。

您正在将
Url
enum与交换机中的字符串进行比较。将
Url
Url
进行比较,您的错误就会消失。我不明白“颤栗”和“喜剧”与您的枚举有什么关系。您的代码也没有意义。您的switch语句在不同情况下没有什么不同。另外,您正在检查“Thriller”和“Comicle”,但您的示例
Url
enum都不包含与此相关的内容。你需要编辑你的问题并提供一个答案,因为在这种状态下它是无法回答的。我认为用户缺乏对枚举的理解。如果要在枚举上设置开关,则需要实际使用它。IE case Company1从我的specflow场景中,当我导航到类别时,类别将作为参数传递。如果类别为Thriller,则转到相应的url;如果类别为喜剧,则转到返回属性[0]行的If语句中的喜剧url。说明我收到错误:错误CS1061“DescriptionAttribute”不包含“Description”的定义,并且找不到接受“DescriptionAttribute”类型的第一个参数的扩展方法“Description”(是否缺少using指令或程序集引用?);