Why does FxCop flag GC.KeepAlive() as a violation?

馋奶兔 提交于 2020-01-24 16:56:06

问题


what is so bad about GC.KeepAlive() that FxCop flags this as a violation?


回答1:


Probably because it's considered bad practice to be calling it, just like it's a generally a bad idea to call GC.Collect -- it has generally negative consequences on the collector and/or is a possible indication of a design flaw on your end.

You should be able to reconfigured FxCop to not consider calls to the method a problem if you need to call it, however. There are valid reasons to do so, after all.




回答2:


It's not that GC.KeepAlive is particularly bad, but rather that it's not necessary if you convert to SafeHandle usage, and you really, really ought to convert to SafeHandle usage. If you're not sure why, you might want to consider taking a look at http://blogs.msdn.com/b/bclteam/archive/2005/03/16/396900.aspx and http://blogs.msdn.com/b/bclteam/archive/2006/06/23/644343.aspx.



来源:https://stackoverflow.com/questions/4661868/why-does-fxcop-flag-gc-keepalive-as-a-violation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!