How to programatically insert a property to a class in .cs source file?
.net, c#, code-generation, regex, string
Solution
A clean way of doing this, is by using T4 templates to generate partial classes.
T4 templates are a good way to generate code at compile time.
These generated files should not be modified by the developer, instead the developer creates another file with additional definitions of members of the partial class.
That way you can tweak and run the generator again and again without messing with the custom code.
Problem
I am creating an automation tool for inserting properties to existing class's source code. E.g. I have an existing source code like this: ``` public class MyClass { //class members goes here } ``` I want to modify it to become like this ``` public class MyClass { //class members goes here public string MyProp { get; set; } } ``` and save it to the same file. The class name, property type and property name will be known before hand and can be considered parameters of the operation. Any idea how to do this easily? Perhaps regex replace will work for this, but I don't know which expression to use that will be flexible regardless of the source code's new line, whitespace and identation policy. EDIT: What I'm looking for is simply automatically generating the source code, not manipulating classes during runtime