本文介绍了为什么FxCop坚持这个?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我会写


BaseClass var;

.....

if(var is DerivedClass)

{

DerivedClass derivedVar =(DerivedClass)var;

DoSomething(derivedVar);

}


不幸的是公司政策要求FxCop和FxCop抱怨上面的

。 FxCop想要这样:


BaseClass var;

DerivedClass derivedVar;

....

derivedVar = var as DerivedClass;

if(derivedVar!= null)

{

DoSomething(derivedClass);

}

我认为在一个范围内定义一个变量是没有意义的

并不是真的需要只是为了安慰FxCop。 : - /

解决方案



嗯,这是一个品味问题,不是吗?我的意思是,让

来改变变量的范围是很烦人的,但是这样做两次

的效率很低(因为它是原来的代码)。


所以...选择你的毒药:不合适的变量范围或双倍工作

工作没有好处。





< snip>



好​​吧,如果你愿意的话,你可以随时添加一个额外的范围,

虽然这很难看。我怀疑它抱怨的原因是

,因为你实际上两次做同样的工作。

表现的差异可能微不足道,但这可能是

的原因...


-

Jon Skeet - < sk *** @ pobox.com>
博客:

如果回复群组,请不要给我发邮件

I would write

BaseClass var;
.....
if(var is DerivedClass)
{
DerivedClass derivedVar = (DerivedClass)var;
DoSomething(derivedVar);
}

Unfortunately company policies requires FxCop and FxCop complains about
the above. FxCop wants it this way:

BaseClass var;
DerivedClass derivedVar;
....
derivedVar = var as DerivedClass;
if(derivedVar != null)
{
DoSomething(derivedClass);
}
I think it''s kinda pointless to define a variable in a scope where it
isn''t really needed just to comfort FxCop. :-/

解决方案

Well, it''s a matter of taste, isn''t it? I mean, it''s annoying to have
to change the variable''s scope, but then it''s inefficient to do the
cast twice (as it was in the original code).

So... choose your poison: inappropriate variable scope or doing double
the work to no good purpose.



<snip>

Well, you can always add an extra level of scoping if you want,
although that''s pretty ugly. I suspect the reason it complains is
because you''re effectively doing the same work twice. The difference in
performance is likely to be negligible, but that''s likely to be the
reason...

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too


这篇关于为什么FxCop坚持这个?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-28 03:22