Background
I have a few scripts that run as part of my build process that look at the various source code files and generate some more source code for me.
The scripts use CodeDom to generate them and they read the .cs
files using a simple text reader.
Question
One of the scripts is looking for use of a specific class attribute called PageMenuItem
and its purpose is to build a static list of page menu items.
It does this right now by reading all of the .cs
files and looks for "PageMenuItem"
attributes, then it counts the number of arguments and tries to figure out which constructor is being used so it can pull apart the various pieces of information.
There are 7 constructors for PageMenuItem
with various parameters, so it is getting very difficult to determine from the .cs
source code which constructor is being used and therefore how to parse out the information.
Instead of trying to parse the text myself, I would like to simply construct a PageMenuItem
object in memory and then use its properties.
So, I need a way of taking the attribute declaration from the .cs
file and construct a new instance of PageMenuItem
from it.
Is that possible?
Another way of asking this question:
Given this string:
string myCodeStatement = "[MyAttribute(\"asdf\", \"foo\")]";
How can I create an object of the type MyAttribute
so that I can work with that object? I have full access to the source code that defines MyAttribute
.