Answer the question
In order to leave comments, you need to log in
[C# / Visual Studio] Class code generation and instant use in the editor
Is there a way in nature to generate a class code from a certain string (whose formatting rules I know) and immediately use it in the Visual Studio editor?
For example, I have a line in my own class description language:
[Bobo:string][Fofo:int][Jojo:bool]
Let this line be stored in some studio file in the project. Now I want the studio to automatically (or on demand) generate a class based on this line.
class SomeClass
{
public string Bobo;
public int Fofo;
public bool Jojo;
}
The problem itself is not in the execution of the generation, but in the fact that the generated code can be used immediately. Answer the question
In order to leave comments, you need to log in
I must say right away that I didn’t really understand your task, so I can “catch up with blizzards”.
Directly from your line with custom grammar, it is unlikely that it will be possible and it is the studio that ... Look for additions.
If you remove the restriction of custom grammar, then T4 (Text Template Transformation Toolkit) can help. Attention! I'm not sure that the generation will work if you compile not from the studio, but with MSBuild.
If you can not in the studio, but at runtime, then you can wind up a self-written generator based on CodeDom , Mono.Cecil or analogues ...
Another option is to write a library with such types in the Nemerle languageand use later from the client. Within Nemerle, you can easily describe your DSL (domain specific language).
On bare T4, I would load your string from a file (or from all files in a given directory), parse and generate a file with classes.
This will work when you save the changes in your file, if you edit from the studio, or manually through the "Run custom tool" on the t4 file
As far as I understand, snippets are suitable for your task.
Of course, they won’t make a class from your string, but you can solve the problem with their help
msdn.microsoft.com/en-us/library/ms242312.aspx
Visual Studio 2010 and earlier had the ability to write macros in Visual Basic. I used them myself to solve similar problems and had no special problems. In Studio 2012, macros have been removed and it is suggested to use the Visual Studio Add-in as an alternative.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question