E
E
Exvel2013-04-19 15:26:55
.NET
Exvel, 2013-04-19 15:26:55

[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.
By analogy, how the studio imports database data and generates table classes based on them so that you can change them, for example, through LINQ.

Answer the question

In order to leave comments, you need to log in

4 answer(s)
K
kk86, 2013-04-19
@kk86

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).

A
Alexey Romanovsky, 2013-04-22
@Alexus1024

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

A
Alex Shkor, 2013-04-19
@AlexShkor

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

B
bmforce, 2013-04-19
@bmforce

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 question

Ask a Question

731 491 924 answers to any question