Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/273.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# 这是“固定”吗;PostSharp抱怨CA1800:不要不必要地“卡斯特”;最好的?_C#_.net_Casting_Postsharp - Fatal编程技术网

C# 这是“固定”吗;PostSharp抱怨CA1800:不要不必要地“卡斯特”;最好的?

C# 这是“固定”吗;PostSharp抱怨CA1800:不要不必要地“卡斯特”;最好的?,c#,.net,casting,postsharp,C#,.net,Casting,Postsharp,这个问题是关于铸造中的“is”和“as”,以及关于CA1800 PostSharp规则。我想知道我认为的解决方案是否是最好的,或者是否有我看不到的问题 我有这个代码(命名为原始代码,并减少到最低相关)。函数validateSubscriptionLicenseProducts尝试通过强制转换并稍后检查一些内容(在//Do anywhere中)来验证订阅许可证(可能有3种类型:标准、信用和时间限制) PostSharp抱怨CA1800:不要不必要地卡斯特。原因是我对同一类型铸造了两次相同的对象。此

这个问题是关于铸造中的“is”“as”,以及关于CA1800 PostSharp规则。我想知道我认为的解决方案是否是最好的,或者是否有我看不到的问题

我有这个代码(命名为原始代码,并减少到最低相关)。函数validateSubscriptionLicenseProducts尝试通过强制转换并稍后检查一些内容(在//Do anywhere中)来验证订阅许可证(可能有3种类型:标准、信用和时间限制)

PostSharp抱怨CA1800:不要不必要地卡斯特。原因是我对同一类型铸造了两次相同的对象。此代码在最佳情况下将施放2次(如果是标准许可证),在最坏情况下将施放4次(如果是时限许可证)。我知道有可能使规则失效(这是我的第一种方法),因为这里对性能没有太大影响,但我正在尝试一种最好的方法

 //Version Original Code
  //Min 2 casts, max 4 casts
  //PostSharp Complains about CA1800:DoNotCastUnnecessarily
  private void ValidateSubscriptionLicenceProducts(SubscriptionLicence licence)
        {
   if (licence is StandardSubscriptionLicence)
            {               
                // All products must have the same products purchased
                List<StandardSubscriptionLicenceProduct> standardProducts = ((StandardSubscriptionLicence)licence).SubscribedProducts;
                //Do whatever
            }
            else if (licence is CreditSubscriptionLicence)
            {               
                // All products must have a valid Credit entitlement & Credit interval
                List<CreditSubscriptionLicenceProduct> creditProducts = ((CreditSubscriptionLicence)licence).SubscribedProducts;
                //Do whatever
            }
            else if (licence is TimeLimitedSubscriptionLicence)
            {                
                // All products must have a valid Time entitlement
                // All products must have a valid Credit entitlement & Credit interval
                List<TimeLimitedSubscriptionLicenceProduct> creditProducts = ((TimeLimitedSubscriptionLicence)licence).SubscribedProducts;
                //Do whatever 
            }
            else
                throw new InvalidSubscriptionLicenceException("Invalid Licence type");

   //More code...


        }
//版本原始代码
//最少2次,最多4次
//PostSharp抱怨CA1800:不必要
private void验证订阅许可证产品(订阅许可证)
{
if(许可证为标准订购许可证)
{               
//所有产品必须购买相同的产品
列出标准产品=((标准订购许可证)许可证)。订购产品;
//做任何事
}
否则如果(许可证为CreditSubscriptionLicense)
{               
//所有产品必须具有有效的信用权利和信用间隔
列出creditProducts=((CreditSubscriptionLicense)许可证)。订购产品;
//做任何事
}
否则,如果(许可证是有时限的订阅许可证)
{                
//所有产品必须具有有效的时间授权
//所有产品必须具有有效的信用权利和信用间隔
列表creditProducts=((时限认购许可证)许可证)。认购产品;
//做任何事
}
其他的
抛出新的InvalidSubscriptionLicenceException(“无效许可证类型”);
//更多代码。。。
}
这是使用“as”改进的1版。不要抱怨CA1800,但问题是它将始终播放3次(如果将来我们有30或40种类型的许可证,它可能会表现不佳)

//版本改进1
//最少3次浇铸,最多3次浇铸
private void验证订阅许可证产品(订阅许可证)
{
StandardSubscriptionLicense StandardLicense=作为StandardSubscriptionLicense的切片;
CreditSubscriptionLicense CreditLicense=作为CreditSubscriptionLicense的许可证;
TimeLimitedSubscriptionLicence timeLicence=作为TimeLimitedSubscriptionLicence的许可;
if(Slicence==null)
{               
//所有产品必须购买相同的产品
列出标准产品=Slicence.SubscribedProducts;
//做任何事
}
else if(Clicence==null)
{               
//所有产品必须具有有效的信用权利和信用间隔
List creditProducts=clience.SubscribedProducts;
//做任何事
}
else if(Tlicence==null)
{                
//所有产品必须具有有效的时间授权
//所有产品必须具有有效的信用权利和信用间隔
List creditProducts=tlice.SubscribedProducts;
//做任何事
}
其他的
抛出新的InvalidSubscriptionLicenceException(“无效许可证类型”);
//更多代码。。。
}
但后来我想到了一个最好的。这是我正在使用的最终版本

    //Version Improve 2
// Min 1 cast, Max 3 Casts
// Do not complain about CA1800:DoNotCastUnnecessarily
private void ValidateSubscriptionLicenceProducts(SubscriptionLicence licence)
        {
            StandardSubscriptionLicence standardLicence = null;
            CreditSubscriptionLicence creditLicence = null;
            TimeLimitedSubscriptionLicence timeLicence = null;

            if (StandardSubscriptionLicence.TryParse(licence, out standardLicence))
            {
                // All products must have the same products purchased
                List<StandardSubscriptionLicenceProduct> standardProducts = standardLicence.SubscribedProducts;
    //Do whatever
            }
            else if (CreditSubscriptionLicence.TryParse(licence, out creditLicence))
            {
                // All products must have a valid Credit entitlement & Credit interval
                List<CreditSubscriptionLicenceProduct> creditProducts = creditLicence.SubscribedProducts;
                //Do whatever
            }
            else if (TimeLimitedSubscriptionLicence.TryParse(licence, out timeLicence))
            {
                // All products must have a valid Time entitlement
                List<TimeLimitedSubscriptionLicenceProduct> timeProducts = timeLicence.SubscribedProducts;
                //Do whatever
            }
            else
                throw new InvalidSubscriptionLicenceException("Invalid Licence type");

            //More code...

        }


    //Example of TryParse in CreditSubscriptionLicence
  public static bool TryParse(SubscriptionLicence baseLicence, out CreditSubscriptionLicence creditLicence)
        {
            creditLicence = baseLicence as CreditSubscriptionLicence;
            if (creditLicence != null)
                return true;
            else
                return false;
        }
//版本改进2
//最少1次,最多3次
//不要抱怨CA1800:不要不必要地抱怨
private void验证订阅许可证产品(订阅许可证)
{
StandardSubscriptionLicence StandardLicense=null;
CreditSubscriptionLicense CreditLicense=null;
TimeLimitedSubscriptionLicence timeLicence=null;
if(StandardSubscriptionLicense.TryParse(许可证,非标准许可证))
{
//所有产品必须购买相同的产品
列出标准产品=标准许可证。订购产品;
//做任何事
}
else if(CreditSubscriptionLicense.TryParse(许可证,外信用证))
{
//所有产品必须具有有效的信用权利和信用间隔
列出creditProducts=CreditLicense.SubscribedProducts;
//做任何事
}
else if(TimeLimitedSubscriptionLicence.TryParse(许可证,过期))
{
//所有产品必须具有有效的时间授权
列出timeProducts=timeLicence.SubscribedProducts;
//做任何事
}
其他的
抛出新的InvalidSubscriptionLicenceException(“无效许可证类型”);
//更多代码。。。
}
//CreditSubscriptionLicence中的TryParse示例
公共静态bool TryParse(订阅许可证baselicense,out-CreditSubscriptionLicence-creditlicense)
{
CreditLicense=作为CreditSubscriptionLicense的基本许可证;
如果(CreditLicense!=null)
返回true;
其他的
返回false;
}
它需要更改StandardSubscriptionLicence和CreditSub类
    //Version Improve 2
// Min 1 cast, Max 3 Casts
// Do not complain about CA1800:DoNotCastUnnecessarily
private void ValidateSubscriptionLicenceProducts(SubscriptionLicence licence)
        {
            StandardSubscriptionLicence standardLicence = null;
            CreditSubscriptionLicence creditLicence = null;
            TimeLimitedSubscriptionLicence timeLicence = null;

            if (StandardSubscriptionLicence.TryParse(licence, out standardLicence))
            {
                // All products must have the same products purchased
                List<StandardSubscriptionLicenceProduct> standardProducts = standardLicence.SubscribedProducts;
    //Do whatever
            }
            else if (CreditSubscriptionLicence.TryParse(licence, out creditLicence))
            {
                // All products must have a valid Credit entitlement & Credit interval
                List<CreditSubscriptionLicenceProduct> creditProducts = creditLicence.SubscribedProducts;
                //Do whatever
            }
            else if (TimeLimitedSubscriptionLicence.TryParse(licence, out timeLicence))
            {
                // All products must have a valid Time entitlement
                List<TimeLimitedSubscriptionLicenceProduct> timeProducts = timeLicence.SubscribedProducts;
                //Do whatever
            }
            else
                throw new InvalidSubscriptionLicenceException("Invalid Licence type");

            //More code...

        }


    //Example of TryParse in CreditSubscriptionLicence
  public static bool TryParse(SubscriptionLicence baseLicence, out CreditSubscriptionLicence creditLicence)
        {
            creditLicence = baseLicence as CreditSubscriptionLicence;
            if (creditLicence != null)
                return true;
            else
                return false;
        }
private void ValidateSubscriptionLicenceProducts(SubscriptionLicence licence)
{
    if(!licence.ValidateProducts())
        throw new Exception("Failed to validate products");
}