Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/267.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 我怎样才能完美地缩短这个C代码?_C#_Oop_Refactoring - Fatal编程技术网

C# 我怎样才能完美地缩短这个C代码?

C# 我怎样才能完美地缩短这个C代码?,c#,oop,refactoring,C#,Oop,Refactoring,我想消除一些重复的代码。有人能把这段代码做得更短更好吗 switch (now.site) { case item.SITE.AMAZON: try { price = driver.FindElement(By.XPath("//*[@id=\"priceblock_ourprice\"]")).Text; fetched = true; } catch {

我想消除一些重复的代码。有人能把这段代码做得更短更好吗

switch (now.site)
{

    case item.SITE.AMAZON:
        try
        {
            price = driver.FindElement(By.XPath("//*[@id=\"priceblock_ourprice\"]")).Text;
            fetched = true;
        }
        catch
        {
            try
            {
                price = driver.FindElement(By.XPath("//*[@id=\"priceblock_dealprice\"]")).Text;
                fetched = true;
            }
            catch
            {
                fetched = false;
            }
        }
        break;
    case item.SITE.ALI:
        try
        {
            price = driver.FindElement(By.XPath("//*[@id=\"j-sku-discount-price\"]")).Text;
            fetched = true;
        }
        catch
        {
            try
            {
                price = driver.FindElement(By.XPath("//*[@id=\"j-sku-price\"]")).Text;
                fetched = true;
            }
            catch
            {
                fetched = false;
            }
        }
        break;
    }
}

您可以创建可用路径字符串的字典。我不知道什么类型的网站。我猜是字符串


它允许您轻松添加新站点。站点可以有任意数量的备用路径。

您可以创建可用路径字符串的字典。我不知道什么类型的网站。我猜是字符串


它允许您轻松添加新站点。这些站点可以有任意数量的备用路径。

以下是一些关于如何美化代码的建议-

为所有XPath查询声明常量。 在switch语句之外声明一个变量,以根据case语句捕获价格值。 使用一个try-catch语句创建一个函数,该语句接受2个xPath字符串和Price的out参数,并返回一个布尔值以指示查询是否成功。 删除每个case下的try-catch,并调用从常量集中传递正确xPath字符串的函数

const string priceblock_ourprice = "//*[@id=\"priceblock_ourprice\"]";
const string priceblock_dealprice = "//*[@id=\"priceblock_dealprice\"]";
const string j_sku_discount_price = "//*[@id=\"j-sku-discount-price\"]";
const string j_sku_price = "//*[@id=\"j-sku-price\"]";

public void YourPrimaryFunction
{  
    decimal price;  
    switch (now.site)  
    {  
        case item.SITE.AMAZON:  
            fetched = FetchPrice(priceblock_ourprice, priceblock_dealprice, out price);
            break;  
        case item.SITE.ALI:  
            fetched = FetchPrice(j_sku_discount_price, j_sku_price, out price);
            break;
    }
}

private bool FetchPrice(string xPathPrim, string xPathFallBack, decimal out price)
{
    try
    {
        price = driver.FindElement(By.XPath(xPathPrim)).Text;
        return true;
    }
    catch
    {
        try
        {
            price = driver.FindElement(By.XPath(xPathFallBack)).Text;
            return true;
        }
        catch
        {
            return false;
        }
    }
}

以下是一些关于如何美化代码的建议-

为所有XPath查询声明常量。 在switch语句之外声明一个变量,以根据case语句捕获价格值。 使用一个try-catch语句创建一个函数,该语句接受2个xPath字符串和Price的out参数,并返回一个布尔值以指示查询是否成功。 删除每个case下的try-catch,并调用从常量集中传递正确xPath字符串的函数

const string priceblock_ourprice = "//*[@id=\"priceblock_ourprice\"]";
const string priceblock_dealprice = "//*[@id=\"priceblock_dealprice\"]";
const string j_sku_discount_price = "//*[@id=\"j-sku-discount-price\"]";
const string j_sku_price = "//*[@id=\"j-sku-price\"]";

public void YourPrimaryFunction
{  
    decimal price;  
    switch (now.site)  
    {  
        case item.SITE.AMAZON:  
            fetched = FetchPrice(priceblock_ourprice, priceblock_dealprice, out price);
            break;  
        case item.SITE.ALI:  
            fetched = FetchPrice(j_sku_discount_price, j_sku_price, out price);
            break;
    }
}

private bool FetchPrice(string xPathPrim, string xPathFallBack, decimal out price)
{
    try
    {
        price = driver.FindElement(By.XPath(xPathPrim)).Text;
        return true;
    }
    catch
    {
        try
        {
            price = driver.FindElement(By.XPath(xPathFallBack)).Text;
            return true;
        }
        catch
        {
            return false;
        }
    }
}

看起来你真的想得到一个十进制值。让我们使用这个假设并创建一个返回小数的方法?表示成功,即值或失败,即空

此方法的目的是隐藏Selenium库似乎异常编码这一令人不快的事实

我还将其创建为一个扩展方法,这样对driver.FindElement的调用就会被一些看起来很熟悉的东西所取代——driver.finddecimalmabe

现在,我采取与奥利弗一样的方法,使用字典:

private static Dictionary<string, string[]> __pricePaths = new Dictionary<string, string[]>
{
    { item.SITE.AMAZON, new [] { "//*[@id=\"priceblock_ourprice\"]", "//*[@id=\"priceblock_dealprice\"]" } },
    { item.SITE.ALI, new [] { "//*[@id=\"j-sku-discount-price\"]", "//*[@id=\"j-sku-price\"]" } },
};

如果price变量有一个值,则为success,但如果为null,则调用未成功。

似乎您确实在尝试获取十进制值。让我们使用这个假设并创建一个返回小数的方法?表示成功,即值或失败,即空

此方法的目的是隐藏Selenium库似乎异常编码这一令人不快的事实

我还将其创建为一个扩展方法,这样对driver.FindElement的调用就会被一些看起来很熟悉的东西所取代——driver.finddecimalmabe

现在,我采取与奥利弗一样的方法,使用字典:

private static Dictionary<string, string[]> __pricePaths = new Dictionary<string, string[]>
{
    { item.SITE.AMAZON, new [] { "//*[@id=\"priceblock_ourprice\"]", "//*[@id=\"priceblock_dealprice\"]" } },
    { item.SITE.ALI, new [] { "//*[@id=\"j-sku-discount-price\"]", "//*[@id=\"j-sku-price\"]" } },
};

如果price变量有一个值,则为success,但如果为null,则调用未成功。

请不要使用异常进行编码。例外情况是指特殊情况。如果您试图在普通代码中使用它们,那么它们也可以被称为System.Normal。是否有机会使用它们,以便我们可以提供一个干净的解决方案?我至少应该能够复制、粘贴和编译您的代码。真正的网络冲浪会导致许多例外情况。我同意你的看法,如果C元素存在或者可以毫无例外地单击,那么捕获抛出的特定异常,而不是每个异常。这是个坏习惯。然后把代码抽象出来,这样你只需要处理一次。好的,谢谢你找到特殊例外的权利。请不要使用例外来编码。例外情况是指特殊情况。如果您试图在普通代码中使用它们,那么它们也可以被称为System.Normal。是否有机会使用它们,以便我们可以提供一个干净的解决方案?我至少应该能够复制、粘贴和编译您的代码。真正的网络冲浪会导致许多例外情况。我同意你的看法,如果C元素存在或者可以毫无例外地单击,那么捕获抛出的特定异常,而不是每个异常。这是个坏习惯。然后把代码抽象出来,这样你只需要处理一次。好的,谢谢你找到特殊的例外是正确的,太棒了!谢谢。。!!精彩的谢谢。。!!
private static Dictionary<string, string[]> __pricePaths = new Dictionary<string, string[]>
{
    { item.SITE.AMAZON, new [] { "//*[@id=\"priceblock_ourprice\"]", "//*[@id=\"priceblock_dealprice\"]" } },
    { item.SITE.ALI, new [] { "//*[@id=\"j-sku-discount-price\"]", "//*[@id=\"j-sku-price\"]" } },
};
decimal? price =
    __pricePaths[now.site]
        .Select(path => driver.FindDecimalMaybe(path))
        .Where(x => x.HasValue)
        .FirstOrDefault();