Answer the question
In order to leave comments, you need to log in
High-level macros in C#, are there ready-made solutions?
Closer to the point. There are tasks that cannot (impossible) be universalized through C #, you need to stupidly copy-paste the code with some changes. Yes, you can try, but there will be no less code. It's useless. The code rests on the elementary lexemes of the language, universalization further - code generation.
And here is an example that is already roasting in a chair - the implementation of INotifyPropertyChanged.
Link to an existing solution to the problem.
All that was offered: T4 or handles using the base class. But damn, why should I look at this code, even if it is automatically generated, if it pisses me off (no need to ask to gouge out my eyes or not to look at it).
Its most primitive implementation for one property includes at least 5 lines (primitive in the sense of a function, that is, just notify
of
a
value
change
)
: I see this code and I want to kill. If you write a code generator, then you can implement it like this:
There is project 1 - with which you are actively working, where you use all sorts of declarative things and enjoy life. The code generator works with the second one, and you, when you launch it.
The solution to the problem above could lie in an attribute declaration over an automatic property - that's it.
What do you want?
A platform for declarative coding add-ons (do not confuse (yes, you never know what you associate it with), I'm talking about complex smart code generation).
That is, write some lib that simply provides input and output data. That is, to write some kind of declarative addon (like for the thing above, for example), I just need to get an Assembly instance of the input assembly, and methods, like api, to make modifications to the source and structure of the output project.
Naturally, I want to ask if I want a bicycle. If not, then maybe someone would be willing to help in such a good cause with advice, and maybe with deed.
Added after 2 minutes
Also, if anyone has a desire, I think it would be very convenient to make integration with VS as an extension, but I have no such experience.
Answer the question
In order to leave comments, you need to log in
Not templates, but it might help you - look towards PostSharp and Fody.
For example https://github.com/Fody/PropertyChanged or https://doc.postsharp.net/inotifypropertychanged-add
https://github.com/tom-englert/AutoProperties.Fody
What prevents you from making not a template, but just a normal assembly, putting all conceivable and unthinkable variables into a separate class? And write a short program that will load this assembly, look for this class in it, patch and save the assembly through AssemblyBuilder or Chotham. This is managed code.
I may have misunderstood something, correct me if so.
upd.
he infuriates me (no need to ask to gouge out my eyes or not look at him).
Its most primitive implementation for one property includes at least 5 lines (primitive in the sense of a function, that is, just notify of a
value change ) : I see this code and I want to kill.
it doesn't look normal, see an anger management specialist.
Look for scaffolding for .net, but it's based on t4. This is how project type templates for vs.
The first thing that came up in a search was here .
On our project, we are doing code generation based on metadata in the database. You can physically read the files you need, generate your own code based on them and upload them to some place in the project.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question