Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6841141
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T23:55:48+00:00 2026-05-26T23:55:48+00:00

Is there a way to create compile time errors if a certain method is

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T23:55:49+00:00Added an answer on May 26, 2026 at 11:55 pm

    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:

    warnif count > 0 
    from m in Application.Methods         
    where m.IsUsing("System.Configuration.ConfigurationManager.get_AppSettings()")
    select m
    

    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:

    warnif count > 0 
    from m in Application.Namespaces.WithNameLike("regex").ChildMethods()
    where m.IsUsing("System.Configuration.ConfigurationManager.get_AppSettings()")
    select m
    

    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):

    enter image description here

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there a way to find out the project path at compile time? I
Is there a way to create C# objects and call methods from unmanaged C++,
Is there a way to create register a handler that will be called exactly
Is there a way to implement a compile-time type dictionary via C++ templates? e.g.
Is there any way to create Map or Set type data structures at compile
Assume I have created a compiled re: x = re.compile('^\d+$') Is there a way
Is there a way to create a JButton with your own button graphic and
Is there any way to create a virtual drive in (My) Computer and manipulate
Is there any way to create the query parameters for doing a GET request
Is there a way to create an html link using h:outputLink, other JSF tag

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.