C# Get Classes by an assembly

Hello  buddies,

today i want to show you a code snippet where you can get all class names of an internal or external library or an assembly. You have to add the library as an Reference.

I am developing in the company a framework, where i need the names of classes of an external library which i added as reference.

We need a using, called System.Reflection.Assembly.

The code to get classes of an assembly:

static void Main(string[] args)
{
ListOfClasses("Company.LibraryNameOrAssemblyName"); //call the method with a library. which you add as reference.
Console.Read();
}
public static string[] ListOfClasses(string name)
{
 
Assembly assembly = Assembly.ReflectionOnlyLoad(name); //create an new object of Assembly type.
string[] classes = new string[assembly.GetTypes().Length]; //get count of types 
 
int counter = 0; //counter for array index, loop through available types
foreach (var classtype in assembly.GetTypes())
{
classes[counter] = classtype.FullName; //add Name of class to array.
counter++; //increase index counter
}
return classes; // return result as array
 
}

If you execute the code you will get back an array filled with classnames from the library.

c# assembly classes

C# get classes of assembly

 

Hope, someone will find it helpfull.

Comments

comments

Kommentar verfassen