简体   繁体   中英

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.

This is an acknowledged gap, that for years has been often shelved because .NET has very good runtime reflection/emit APIs, allowing for runtime meta-programming solutions; however, this has multiple problems, including:

  1. complexity
  2. start-up time
  3. security implications
  4. not working in all scenarios (some runtimes/platforms prohibit runtime emit)
  5. problems with IL linkers removing code that "isn't used" (because at build time nothing touches those APIs, but at runtime they are used via runtime reflection/emit)

Because of this, in the C# 9 timeframe (but not specific to C# 9), "generators" looks to finally be becoming a "thing". This provides a Roslyn-based build-time meta-programming layer that can be extended in similar ways to how 3rd-party libraries can ship "analyzers" today (generators and analyzers are fundamentally similar).

It might be the "industry standard" that you are looking for, but it is in its infancy. Intro post, from April 2020 .

No, there is not. Mostly because it is a fringe field. NOT saying it is not useful, just saying that like 90% to 95% of the people simply do not do it.

There are 2 technolgies used "widely" (partially because some .NET tools, mostly in EntityFramework) did use them):

I would say out of the use cases, 95% is T4, and that is basically the old Ef 4 editor, now slowly being replaced by client generators for various API's - yes, meta programming is that rare. I find it partially a shame. I have used code generators at times (odata, T4 based routing for ASP.NET back in the day), but in most cases other developers look at it with big eyes and have never even heard of the concept. Shame.

So, definitely no standard out there.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM