Search code examples
c#.netmetaprogrammingpreprocessor

Is there some industry "standard" for advanced meta programming in c#?


Every now and then, in various projects, I come across a meta programming situation in a C# project that cannot be resolved with generics, and would benefit from a more powerful meta programming tool. The solutions I usually employ are either to work around the problem with reflection, employ C++/cli, or introduce a custom xml/xslt compiler step.

Does this reflect the approaches taken by the C# community in general, or is there a worthwhile approach, such as a widely used 3rd party preprocessor, that I am unaware of?

I am not asking for a product recommendation, I'm asking about an established common solution to this common problem. "No, there isn't" might be the valid and correct answer.


Solution

  • There are many ways to use metaprogramming in .NET.

    • Compile-time tools: PostSharp, Fody, etc
    • Runtime tools: Castle DynamicProxy, Unity Interception, etc
    • Code generation tools: T4, Source Generators

    But none of them are standard.

    Also consider switching to a different programming language, for example, Nemerle. It is a language that supports hygienic macros. In my personal opinion, this is the ideal of metaprogramming.