FxCop自定义规则实施 [英] FxCop Custom Rule Implementation

查看:114
本文介绍了FxCop自定义规则实施的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨朋友们,



我想检查数据库层或业务层不是使用FxCop从Controller直接调用的。



我想检查所有方法是否都使用FxCop尝试/ catch。



请帮帮我。

Hi Friends,

I want to check DB Layer or Business Layer is not directly invoked from Controller using FxCop.

I want to check whether all the method has try/catch using FxCop.

Please help me.

推荐答案

关于第一个问题,我只能部分回答,而且我担心这不是最重要的部分 - 如何创建自定义规则。请参阅此CodeProject文章:

使用FXCOP编写自己的自定义规则的7个步骤 [ ^ ]。



不幸的是,你的第一条规则本身可以很难实施,甚至不可能实现。你甚至可以严格定义它吗?也许,我错过了一些东西;你可以;那是你的运气。好吧,一些想法:如果您只是定义并使用一些.NET属性对它们进行分类,您可以轻松确定哪个程序集或甚至单独的类型属于哪个层。但是如何检查这些属性是否被滥用。但另一种想法更难:直接调用的检查应该涉及所有方法的主体的检查:是否完成了调用调用。在这里反射是不够的,你还需要一些反编译。我甚至知道在哪里学习反编译到CIL:源代码ILSpy:

http:// en。 wikipedia.org/wiki/.NET_Reflector [ ^ ],

http://ilspy.net/ [ ^ ],

http://sourceforge.net/projects/sharpdevelop/files/ILSpy/2.0/ILSpy_Master_2.1.0.1603_RTW_Source.zip/download [ ^ ]。



无论如何,这是相当困难的。



关于第二个问题,相比之下,答案是相当的简单:没有什么可以检查:哟你永远不应该在每种方法中都使用try / catch。这完全会破坏结构异常处理的目的。老实说,滥用异常处理比完全不编程更糟糕。你需要停止做你正在做的事情并确定你对这部分技术和实践的理解。我应该帮助你吗?好的,只是我过去答案的一些链接:

C#构造函数中的异常导致来电者分配失败? [ ^ ],

异常详情:System.Runtime.InteropServices .COMException:由于以下错误,检索CLSID为{0006F03A-0000-0000-C000-000000000046}的组件的COM类工厂失败:... [ ^ ],

处理异常类库(dll) [ ^ ]。



-SA
As to the first question, I can answer only partially, and I'm afraid that would be not the most essential part — how to create custom rules. Please see this CodeProject article:
7 Steps to Write Your Own Custom Rule using FXCOP[^].

Unfortunately, your first rule itself could be very difficult to implement, or maybe even impossible. Can you even strictly define it? Maybe, I'm missing something; and you can; then it's your luck. Well, couple of ideas: you can easily determine which assembly or even a separate type belongs to which layer, if you simply define and use some .NET attributes classifying them. But how to check up if those attributes are misused. But another think is more difficult: the check of "directly invoked" should involve the check ups of the bodies of all methods: is come invocation call done or not. And here reflection is not enough, you would also need some decompilation. I even know where to learn decompilation onto CIL: source-code ILSpy:
http://en.wikipedia.org/wiki/.NET_Reflector[^],
http://ilspy.net/[^],
http://sourceforge.net/projects/sharpdevelop/files/ILSpy/2.0/ILSpy_Master_2.1.0.1603_RTW_Source.zip/download[^].

Anyway, this is quite difficult.

As to the second question, the answer, in contrast, is quite simple: there is nothing to check up: you should never ever use try/catch in every method. This would totally defeat the purpose of structures exception handling. Honestly, misusing exception handling that badly is much worse than not doing any programming at all. You need to stop doing what you are doing and fix your understanding of this part of technology and then the practice. Should I even help you with that? OK, just some links to my past answers:
Does Exception in C# Constructor Cause Caller Assignment to Fail?[^],
Exception Details: System.Runtime.InteropServices.COMException: Retrieving the COM class factory for component with CLSID {0006F03A-0000-0000-C000-000000000046} failed due to the following error:...[^],
Handling exceptions in class library (dll)[^].

—SA


这篇关于FxCop自定义规则实施的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆