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

  • Home
  • SEARCH
  • 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 575577
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T13:57:20+00:00 2026-05-13T13:57:20+00:00

Is it possible in C# to write MSIL code that will add a preprocessor

  • 0

Is it possible in C# to write MSIL code that will add a preprocessor directive to the code, e.g., #warning, if a certain condition is met? Or maybe this can be done with reflection, I don’t know.

I’m trying to write a custom attribute that, if applied incorrectly to a class’s method or property, will generate a compiler warning. Using the existing Obsolete attribute won’t work because then just using my custom attribute causes the warning, and I don’t want that. I want the custom attribute constructor to check for a condition, and if that condition is true then cause a compilation warning.

Update: after reading back over my question, I think what I’m asking for is impossible just because I’m mixing compile-time and runtime constraints. I think I’ll end up going with a post-build task to check the just-built DLL and have it spit out error messages if the condition is true.

  • 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-13T13:57:20+00:00Added an answer on May 13, 2026 at 1:57 pm

    I saw this question coming from your previous thread. To mis-quote the great Jamie Zawinski: “Some people, when confronted with a problem, think “I know, I’ll use an attribute.” Now they have two problems”.

    An attribute is merely out-of-band data, compiled into an assembly’s metadata. It cannot affect program execution or tool behavior, unless the program or the tool is explicitly programmed to recognize the specific attribute. It needs to do so using Reflection.

    What you need to do is write your own tool. It should execute after an assembly is built, using the Post-Build step for a project. It needs to load the assembly and use Reflection to iterate the types in the assembly. For each type, iterate the methods with Type.GetMethods() and use MethodInfo.GetCustomAttributes() to discover and construct an attribute that might have been programmed.

    You can use Type.GetInterfaces() to discover which interfaces are implemented by the type. You can now complain when you see that a method is present that implements an interface method but is missing an attribute that says so. And your ultimate goal: you can complain when you see a method with an attribute that says it implements an interface method but the type no longer inherits it.

    Use Environment.ExitCode to make the tool fail the build if you see anything objectionable. This takes care of enforcement. Btw: programmers really hate to break the build. That might well encourage them to use the attribute religiously. Or it might encourage them to edit the post build step.

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

Sidebar

Related Questions

Is it possible to write code in a Flex application that will only be
Is it possible to write a program that will change the phone numbers a
Is it possible to write code like this? public void DoSomeOperation().Log() { }.Log() Above
Is it possible to write a doctest unit test that will check that an
Is it possible to write a lambda expression that will iterate through array of
Is it possible to write GLSL ES fragment shaders under iOS that generate multiple
Is it possible to write this query in codeigniter active records? $Main_Nav_Query = mysql_query(SELECT
if this possible to write a com control or activex in C# and use
I was wondering if i could possible write an app, that could be a
Possible Duplicate: Write a function that returns the longest palindrome in a given string

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.