我正在尝试使 ildasm 输出更像 json 或 xml,以便以编程方式读取它有点容易。
我打算这样做的方式是逐行读取输出,然后将类和方法等添加到列表中,然后将其修改并重写为 xml,然后读取它。
问题:有没有更聪明或更简单的方法来读取输出?
有一种方法可以通过阅读 IL 代码来获取类和方法的列表。我所说的解决方案可能有点长,但它会起作用。
IL 只不过是 .exe 或 .dll 。首先尝试使用ILSpy将其转换为 C# 或 VB 。下载此工具并在其中打开您的 DLL。该工具可以将您的 IL 代码转换为 C# 或 VB。
转换后,将转换后的代码保存为 txt 文件。
然后阅读文本文件并找到其中的类和方法。
要阅读方法名称:
MatchCollection mc = Regex.Matches(str, @"(\s)([A-Z]+[a-z]+[A-Z]*)+\(");
阅读类名:
逐行遍历文件并检查该行是否具有名称"Class"。如果它具有名称,则拆分值并存储名称“Class”之后的值/文本,这只不过是ClassName。
完整代码:
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();
}
在这里,我将类名和方法名存储在一个列表中。您可以稍后将它们存储在 XML 或 JSON 中,如上所述。
如果您遇到任何问题,请联系我们。