C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
In System.Reflection, we can use a static method such as GetCallingAssembly on the Assembly type. And on Assembly, we can get an array of types, or a specific, named type.
Example. This file includes the System.Reflection namespace. We call Assembly.GetCallingAssembly() to get the current assembly. And then with GetTypes, we receive an array of Type instances. We loop with foreach through these elements.
GetType: The GetType method next receives a string, and returns a Type pointer if one of that name is located.
C# program that uses Assembly type using System; using System.Reflection; class Box { } class Program { static void Main() { // Get assembly. Assembly assembly = Assembly.GetCallingAssembly(); // Get array of types. Type[] types = assembly.GetTypes(); foreach (Type t in types) { Console.WriteLine(t); } Console.WriteLine(); // Get type by name. string name = "Box"; Type type = assembly.GetType(name); Console.WriteLine(type); } } Output Box Program Box
In this example, we use no custom namespaces. But if there were namespaces present, you would need to specify them in the argument to GetType—for example, "ConsoleApplication1.Box".
Tip: In this case, you could develop a custom search algorithm, with GetTypes() and IndexOf, to find types without their namespaces.
Summary. Assembly has many functions. It yields, with GetTypes, an array of all the types in the metadata. With its static methods, such as Assembly.GetCallingAssembly(), we access an Assembly instance.