pratik godha
pratik godha

Reputation: 73

How do i Get Classes name of classes in entire solution in c#

ProcessStartInfo info = new ProcessStartInfo(@"D:\\ss\\Class1.cs");

i have a this type of code but i want to get "class name" like how many classes in my solution code will count the no of class and get the name by code in c#.

Upvotes: 0

Views: 1084

Answers (2)

Behroz Sikander
Behroz Sikander

Reputation: 4039

If i am not wrong, you want to search all the classes that exist in the solution and get the class name.

Here is the pseudo code

1- Search all the solution for *.cs files.

string[] files = System.IO.Directory.GetFiles(path, "*.txt");

2- Loop through all the *.cs files and search for the following pattern

namespace [WhatEver the name is]
{
    public class [Class Name]

Here is the code that you can use

foreach(string path in files)
{
    string[] lines = System.IO.File.ReadAllLines(path);
    for (int i=0; i < lines.Count(); i++)
            {
                if(lines[i].ToLower().Contains("namespace"))
                {
                    int j = i + 2;
                    if(lines[j].ToLower().Contains("public class"))
                    {
                       string lClassname = lines[j].Replace("public class","");
                       //Save the class name here
                       break;
                    }
                }
            }
}

Upvotes: 0

Marc Gravell
Marc Gravell

Reputation: 1062780

What you are trying to do will not involve ProcessStartInfo. There are two ways to approach this: if it is compiled, then load the assembly and use reflection to inspect the assembly (someAssembly.GetTypes() would be a good start). Alternatively, for inspecting source code (non-compiled), the Roslyn CTP can be used to load a project / solution / class file, and inspect the analysis tree.

Upvotes: 3

Related Questions