我想解决一些fxcop问题,我对这个问题非常困惑。FxCop混淆了如何消除冗余castclass
在派生类中的二传手,我检查,如果分配应作出
if (!(value is TypeA))
{
throw new ArgumentException("value is not of TypeA type");
}
_action = (TypeA)value;
的FxCop抱怨:
'值',参数,被转换为类型“类型A '在方法中多次。缓存'as'运算符的结果或直接投射以消除冗余的castclass指令。
,但在this MSDN例如(is
定义)我看到这一点:
if (o is Class1)
{
Console.WriteLine("o is Class1");
a = (Class1)o;
}
这也正是我做同样的事情。那么,是否有解决方案?
所有我能想到的是:
TypeA tmpAction = value as TypeA;
if(tmpAction == null)
{
throw new ArgumentException();
}
_action = tmpAction;
是的,你认为是正确的。你觉得什么是错的? – 2012-01-01 22:07:35
我很困惑它是否正确,因为msdn与我做的一样 – Odys 2012-01-01 22:09:49
MSDN上有许多例子不会被认为是最佳实践。有些目的很简单,可能是通过跳过输入验证。 – TrueWill 2012-01-01 22:22:00