Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/304.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 以编程方式读取ildasm输出的最佳方式是什么_C#_Ildasm - Fatal编程技术网

C# 以编程方式读取ildasm输出的最佳方式是什么

C# 以编程方式读取ildasm输出的最佳方式是什么,c#,ildasm,C#,Ildasm,我正在尝试使ildasm输出更像json或xml,以便通过编程读取它 我打算通过逐行读取输出,然后将类和方法等添加到列表中,然后将其修改并重写为xml,然后读取 问题:有没有更聪明或更简单的方法来读取输出?有一种方法可以通过读取IL代码来获取类和方法的列表。 我所说的解决方案可能有点长,但它会起作用 IL只不过是.exe或.dll。首先尝试使用将其转换为C#或VB。下载此工具并将DLL打开到此文件夹。此工具可以将IL代码转换为C#或VB 转换后,将转换后的代码保存到txt文件中 然后阅读文本文件

我正在尝试使ildasm输出更像json或xml,以便通过编程读取它

我打算通过逐行读取输出,然后将类和方法等添加到列表中,然后将其修改并重写为xml,然后读取


问题:有没有更聪明或更简单的方法来读取输出?有一种方法可以通过读取IL代码来获取类和方法的列表。 我所说的解决方案可能有点长,但它会起作用

IL只不过是.exe或.dll。首先尝试使用将其转换为C#或VB。下载此工具并将DLL打开到此文件夹。此工具可以将IL代码转换为C#或VB

转换后,将转换后的代码保存到txt文件中

然后阅读文本文件并找到其中的类和方法

读取方法名称:

   MatchCollection mc = Regex.Matches(str, @"(\s)([A-Z]+[a-z]+[A-Z]*)+\(");
  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();

    }
阅读类名:

   MatchCollection mc = Regex.Matches(str, @"(\s)([A-Z]+[a-z]+[A-Z]*)+\(");
  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();

    }
逐行遍历文件,并检查该行是否有名称“Class”。如果它有名称,则拆分值并存储名称“Class”后面的值/文本,该名称只不过是ClassName

完整代码:

   MatchCollection mc = Regex.Matches(str, @"(\s)([A-Z]+[a-z]+[A-Z]*)+\(");
  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();

    }
static void Main(字符串[]args)
{
弦线;
List classLst=新列表();
List methodLst=新列表();
System.IO.StreamReader file=new System.IO.StreamReader(@“C:\Users\******\Desktop\TreeView.txt”);
string str=File.ReadAllText(@“C:\Users\******\Desktop\TreeView.txt”);
而((line=file.ReadLine())!=null)
{      
if(line.Contains(“类”)和&!line.Contains(“//”)
{
//用于查找类名
int si=行索引(“类”);
字符串followstring=line.Substring(si);
如果(!string.IsNullOrEmpty(followstring))
{
string[]spilts=followstring.Split(“”);
如果(分流长度>1)
{
Add(spilts[1].ToString());
}
}
}
}
MatchCollection mc=Regex.Matches(str,@“(\s)([A-Z]+[A-Z]+[A-Z]*)+\(”);
foreach(在mc中匹配m)
{
方法1.Add(m.ToString().Substring(1,m.ToString().Length-2));
//Console.WriteLine(m.ToString().Substring(1,m.ToString().Length-2));
}
file.Close();
Console.WriteLine(“**********类**********”);
foreach(classLst中的var项)
{
控制台写入线(项目);
}
Console.WriteLine(“**********类结束*************”);
Console.WriteLine(“**********方法**********”);
foreach(methodLst中的var项)
{
控制台写入线(项目);
}
Console.WriteLine(“**********方法结束**********”);
Console.ReadKey();
}
在这里,我将类名和方法名存储在一个列表中。您可以稍后将它们存储在XML或JSON中,如上所述


如果您遇到任何问题,请与我们联系。

请给出输出示例以及您想对其执行的操作。目前,我无法在复制和复制之间进行选择(或仅在广泛与不明确之间进行选择)。为什么要通过ildasm?似乎更容易直接读取二进制文件。Cecil是一个可能会有所帮助的库。
ildasm
输出不是要通过编程方式读取的,而不是通过
ilasm
。这种方式是疯狂的。