I'm trying to make ildasm output more like json or xml so that its somewhat easy to programmatically read it.
The way I intended to do it by reading the output line by line and then adding the classes and methods etc to lists and then modify and rewrite it as xml and then read it.
Question: Are there any smarter or simpler ways to read the output?
There is a way to get a list of classes and methods by reading IL Code. The solution which i am telling might be a bit long but it will work.
IL is nothing but .exe or .dll . First try to convert this to C# or VB by using ILSpy . Download this tool and open your DLL into this. This tool can convert your IL Code into C# or VB.
After converting , save your converted code into a txt file.
Then read the text file and find the classes and methods inside it.
To read Method Names :
MatchCollection mc = Regex.Matches(str, @"(\s)([A-Z]+[a-z]+[A-Z]*)+\(");
To read Class Names :
Iterate through the file line by line and check whether the line has name "Class" . if it has the name then Split the values and store the value/text which comes after the name "Class" which is nothing but ClassName.
Complete Code :
static void Main(string[] args)
{
string line;
List<string> classLst = new List<string>();
List<string> methodLst = new List<string>();
System.IO.StreamReader file = new System.IO.StreamReader(@"C:\Users\******\Desktop\TreeView.txt");
string str = File.ReadAllText(@"C:\Users\*******\Desktop\TreeView.txt");
while ((line = file.ReadLine()) != null)
{
if (line.Contains("class")&&!line.Contains("///"))
{
// for finding class names
int si = line.IndexOf("class");
string followstring = line.Substring(si);
if (!string.IsNullOrEmpty(followstring))
{
string[] spilts = followstring.Split(' ');
if(spilts.Length>1)
{
classLst.Add(spilts[1].ToString());
}
}
}
}
MatchCollection mc = Regex.Matches(str, @"(\s)([A-Z]+[a-z]+[A-Z]*)+\(");
foreach (Match m in mc)
{
methodLst.Add(m.ToString().Substring(1, m.ToString().Length - 2));
//Console.WriteLine(m.ToString().Substring(1, m.ToString().Length - 2));
}
file.Close();
Console.WriteLine("******** classes ***********");
foreach (var item in classLst)
{
Console.WriteLine(item);
}
Console.WriteLine("******** end of classes ***********");
Console.WriteLine("******** methods ***********");
foreach (var item in methodLst)
{
Console.WriteLine(item);
}
Console.WriteLine("******** end of methods ***********");
Console.ReadKey();
}
Here I am storing the class names and method names in a list. you can later store them in XML or JSON as you described above.
Ping us if you face any problem.