I'm looking at simplifying the creation of input files for an application
ie. I have a custom class
public class CustomClass
{
public string field1;
public string field2;
public string field3;
...
public static void FunctionA() {...}
}
I want to be able to write the members of this class(fields/properties/functions) to a file right after I do a build. This content would be in html or X markup form.
I also want to be able to have it write different inputs to the same file based on the current existing fields. I've been looking at custom attributes to do this but I don't know if this is feasible or even the right approach.
My custom attribute signature would look something like
[SampleAttribute("C:\tmp\writeToThisFile.txt"]
[SampleFields("A", "B", "C", "A1"]
[SampleFields("X", "Y", "Z", "A2"]
public class CustomClass
..
Th开发者_Go百科e file C:\tmp\writeToThisFile.txt would contain something like this:
field1 | field2 | field3 | FunctionA
A | B | C | A1
X | Y | Z | A2
I don't want to have to write/maintain another app that scans the code and writes to the file (post build event).
I'm looking for existing C# or Visual studio behavior that might facilitate this approach.
I think the best in your case will be to create a simple addin, that will walk the solution recursivelly and check the code artifacts in each file.
What you will need to do: 1. Create a simple addin (New Project => Other Project Types => Extensibillity => Visual Studio Add-In).
Attach to the OnBuildBegin and start iterating over the DTE.Solution.Projects (you will have an instance of the DTE right away, after you create your project).
Each project item (if it is a code file) will have a code model hierarchy that contains the namespaces in that file, each namespace contains the classes, each class contains methods, properties, fields etc. You basically drilldown in the file to find all the elements that you need and you build a string builder with the information you want. Hope this will help you get started.
I would say write a code that perform actions you want and compile it into EXE file. That EXE, let's say, uses a reflection to find values it needs and generates desired output, let's say a report.
Go to your main project properties and in PostbuildEvent field insert DOS script that calls previously created EXE.
So every time your main project compiles, the script will run, so your EXE will run.
精彩评论