阻止 .NET 代码调用特定方法?
如果调用某个方法,是否有办法创建编译时错误?
举个例子,我想做的是阻止某个项目中的代码调用 System.Configuration.ConfigurationManager.AppSettings()。有没有办法标记项目或类文件,以便在调用该方法时引发编译时错误?
我认为不存在,所以我的想法是,执行此操作的唯一方法是生成一个 FxCop 规则来标记这些调用并以这种方式执行此操作,但我对其他想法持开放态度。
我正在使用.NET 3.5。不确定 4.0 代码合约是否可以做到这一点。
更新
我专门谈论框架方法,而不是我自己的方法,因此我不能将它们标记为已过时。
此时我不关心反思。
另一个例子是 System.Web.HttpUtility.HtmlEncode,我想找到它并用 Microsoft 的 AntiXss 库替换它,但我想在我的构建服务器上集成某种检查过程来检查新代码。
Is there a way to create compile time errors if a certain method is called?
As an example, what I'd like to do is prevent code in a certain project from calling System.Configuration.ConfigurationManager.AppSettings(). Is there a way to tag the project or class file to raise a compile time error IF that method is called?
I don't think there is, so my thought is that the only way to do this is to generate an FxCop rule that would flag these calls and do it that way, but I am open to other ideas.
I am using .NET 3.5. Not sure if 4.0 code contracts can do this.
Updates
I am specifically talking about framework methods, not my own, so I cannot mark them as Obsolete.
At this point I don't care about reflection.
Another example is System.Web.HttpUtility.HtmlEncode, which I want to find and replace with Microsoft's AntiXss library, but I'd like to integrate some sort of check process on my build server that would check new code as well.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以在 FxCop 中编写自己的规则,所以也许这是一个选择。
然而,在这种情况下,对于现有的代码库,您可能会发现编辑器中的 Ctrl-F(查找)“ConfigurationManager.AppSettings”可能会以更少的努力完成同样好的工作......但关于未来的实施您可能会考虑向开发团队发送电子邮件备忘录的更人性化的途径...
正如上面的评论者所强调的那样,对于具有类似目标的其他人来说值得注意的是,如果这是指您自己的代码库中的函数(这个问题不是参考),您可以使用
[Obsolete]
属性。You are able to write your own rules in FxCop so perhaps that's an option.
However, in this case, for existing code-base you might find that a Ctrl-F (Find) in your editor for "ConfigurationManager.AppSettings" might do an equally good job with far less effort... With regards to future enforcement though you might consider a more human route of an email memo to the development team...
As highlighted by a commenter above and worth noting for others with similar goals, is that if this were referring to a function in your own code-base (which this question is not referring to), you could use the
[Obsolete]
attribute.工具 NDepend 可用于此目的(免责声明:我是该工具的开发人员之一< /em>)。
您可以编写一些针对 LINQ 查询 (CQLinq) 的代码规则来检查任何类型的依赖关系,例如方法调用。每次成功编译后,可以在 Visual Studio 中检查代码规则,或者规则也可以在构建过程中检查。
这样的 CQLinq 代码规则可以如下所示:
该规则可以随心所欲地专门化,以禁止例如与正则表达式匹配的命名空间,以包含调用
get_AppSettings()
getter 方法的方法:来自 NDepend < a href="http://www.ndepend.com/Doc_Matrix.aspx" rel="nofollow noreferrer">依赖矩阵 或 依赖关系图,您还可以右键单击依赖关系(矩阵单元格或图形箭头)并生成代码规则警告是否存在依赖项(然后根据需要专门化生成的规则):
The tool NDepend can be used for that (Disclaimer: I am one of the developers of the tool).
You can write some Code Rules over LINQ Queries (CQLinq) to check any kind of dependency, like a method call for example. Code rules can be checked in Visual Studio after each successful compilation, or rules can be checked at build process time as well.
Such a CQLinq code rule can look like:
The rule can be specialized at whim, to forbid for example namespaces that match a regex, to contain methods that call the
get_AppSettings()
getter method:From the NDepend dependency matrix or dependency graph, you can also right click a dependency (matrix cell or graph arrow) and generate a code rule that warns if the dependency exist (and then specialize the rule generated if you need):