VS2010代码分析和CA1800:Microsoft性能

时间:2011-04-01 10:43:21

标签: c# visual-studio-2010

我有下面的代码,我知道这不是最佳的。我运行了代码分析,它给了我警告

CA1800 : Microsoft.Performance : 'customField', a variable, is cast to type 'DateCustomFieldRef' multiple times in method 'Customer.CustomerToUpdat(SearchResult)'. Cache the result of the 'as' operator or direct cast in order to eliminate the redundant castclass instruction. 

我真的不明白该怎么做。

CustomFieldRef[] customFields = customer.customFieldList;
for (int f = 3; f < customFields.Length; f++)
{
    CustomFieldRef customField = customFields[f];
    if (customField is DateCustomFieldRef)
        {
            DateCustomFieldRef dateField = (DateCustomFieldRef)customField;
                if (dateField.internalId != null && dateField.internalId == "created_date")
                    {
                            createdDate = dateField.value.ToString();
                        }
                }
                if (customField is StringCustomFieldRef)
                {
                    StringCustomFieldRef tradingNameField = (StringCustomFieldRef)customField;
                        if (businessNameField.internalId != null && businessNameField.internalId == "business_name")
                        {
                            businessName = businessNameField.value;
                    }
                }
        }
}

有人可以给我一个代码示例或进一步解释它的真正含义吗?

提前致谢。

2 个答案:

答案 0 :(得分:4)

问题出现在以下代码中:

if (customField is DateCustomFieldRef)
{
  DateCustomFieldRef dateField = (DateCustomFieldRef)customField;

这些是多个演员。

更好:

DateCustomFieldRef fieldasDate = customField as DateCustomFieldFRef

if (fieldasDate != null)
{
  blablabla using fieldasdate

这可以避免多次演员。

答案 1 :(得分:2)

这意味着您要多次投射(可能代价高昂)customField变量,并且只需投放一次就能更好。

您可以使用as运算符来实现这一点,因为as运算符执行强制转换并返回所需类型的实例,如果对象无法转换为所需类型,则返回NULL

像这样:

DateCustomFieldRef customField = customFields[f] as DateCustomFieldRef; // the as operator returns null if the casting did not succeed (that is, customFields[f] is not a DatecustomFieldRef instance

if (customField != null)
{
     DateCustomFieldRef dateField = customField;
     if (dateField.internalId != null && dateField.internalId == "created_date")
     {
           createdDate = dateField.value.ToString();
     }
}
else
{
    var stringField = customFields[f] as StringCustomFieldRef;
    if (stringField != null )
    {
        StringCustomFieldRef tradingNameField = stringField;
        if (businessNameField.internalId != null && businessNameField.internalId == "business_name")
        {
            businessName = businessNameField.value;
        }
    }
}

但是,我相信可能存在一个更好的解决方案(虽然我不知道你的项目,也不知道你的代码),但是不可能抽出一些东西吗?

也许您有一个CustomField基类,DateCustomFieldRefStringCustomFieldRef继承自Customfield基类。 如果是这种情况,您可以在CustomField基类中创建一个虚拟(或者甚至是抽象)方法,该方法在每个子类中被覆盖,实际上返回该字段的值。

像这样:

public class CustomField<T>
{

    public string Internalid
    {
        get;
        set;
    }

    public T Value
    { 
        get;
        set;
    }

    public virtual string GetStringRepresentation()
    {
        return Value.ToString();
    }
}

public class DateCustomField : CustomField<DateTime>
{
    public override string GetStringRepresentation()
    {
         return Value.ToShortDateString();
    }
}

您的代码可以看起来更简单:

foreach( CustomField f in customFields )
{
     if( f.InternalId == "created_date" )
     {
         createdDate = f.GetStringRepresentation();
     }
     if( f.InternalId == "business_name" )
     {
         businessName = f.GetStringRepresentation();
     }
}

(上面的代码可以变得更简单和干净,但你会得到漂移。)