TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

C# Assembly Type

This C# example program uses the Assembly type and its GetTypes method. It uses Assembly.GetCallingAssembly.

Assembly. The Assembly type contains many Type instances.

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.

Static MethodType

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.

IndexOf

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.


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf