lundi 18 septembre 2017

get all methods from C# source code

I am looking forward to get all method names used in C# using following code:

string project_name = comboBox1.GetItemText(comboBox1.SelectedItem);
                string project_path = Data_Directory + '\\' + project_name;
                string[] files = Directory.GetFiles(project_path, "*.cs", SearchOption.AllDirectories);
                string file_path = project_path + '\\'+ "function_extration.txt";              

            StreamWriter sw = File.CreateText(file_path);
            sw.Close();
            if (File.Exists(file_path))
            {

                CompilerParameters parameters = new CompilerParameters()
                {
                    GenerateExecutable = false,
                    GenerateInMemory = true
                };

                var provider = new CSharpCodeProvider();
                foreach (string path_file in files)
                {
                    string source = File.ReadAllText(path_file);
                    Console.Out.WriteLine(source);
                    CompilerResults results = provider.CompileAssemblyFromSource(parameters, source);

                    if (results.Errors.HasErrors)
                    {
                        foreach (var error in results.Errors)
                            Console.Out.WriteLine(error.ToString());
                        return;
                    }
                    var assembly = results.CompiledAssembly;
                    var types = assembly.GetTypes();
                    sw = File.AppendText(file_path);
                    foreach (Type type in types)
                    {
                        sw.WriteLine(type.Name.ToString());
                        sw.WriteLine(type.GetProperties().ToString());
                        sw.WriteLine(type.GetMethods().ToString());
                    }
                }
            }

I am getting the following error: c:\Users\username\AppData\Local\Temp\2rdqotiv.0.cs(3,14) : error CS0234: The type or namespace name 'Linq' does not exist in the namespace 'System' (are you missing an assembly reference?)

Can anyone please help me to solve this error? I am using MVS 15.





Aucun commentaire:

Enregistrer un commentaire