Type.Getnestedtype() Method In C#

The Type.GetNestedType function belongs to the System.Reflection namespace that is used to get a nested type within a given type. Reflection is a powerful C# feature that allows programmers to see and interact with types' metadata during runtime.

Fundamentals of Reflection:

Before we go into Type.GetNestedType method, understanding the fundamentals of reflection is critical when using GetNestedType. Types are C# entities that represent classes, interfaces, structures, enums, and other concepts. The Type class is part of the System namespace and includes methods and properties for inspecting the metadata of these kinds.

Reflection is extensively used when the type structure is undefined at compile time, allowing developers to view and interact with types dynamically during runtime.

Purpose of Type.GetNestedType

The Type.GetNestedType function is particularly concerned with nested types inside a given type. A nested type is one that is declared within another type, such as a class or an interface. This function allows us to obtain a nested type using its name.

Syntax:

It has the following syntax:

public abstract Type GetNestedType(string names,System.Reflection.BindingFlags bindingAttr);

Parameters:

name: The string with the name that represents the nested type to get.

bindingAttr: A bitmask is made up of one or more BindingFlags that describe how the search is performed. Or else zero will return null.

Example:

Let us take an example to illustrate the Type.GetNestedType function in C#.

FileName: GetNestedType1.cs

Example

using System; 
using System.Globalization; 
using System.Reflection; 
// Defining class Empty 
public class Empty { } 
class GetNestedType{ 
	// Main Method 
	public static void Main() 
	{ 
		// Array Declaration
		Type objTypes = typeof(PersonClass); 
		//Try block for exception handling
		try { 
			Type types= objTypes.GetNestedType("StudentClass",
			BindingFlags.Public | BindingFlags.Instance); 
			// Result 
			Console.Write("NestedType of the current type is: "); 
			Console.WriteLine(" {0}", types); 
		} 
		// catch block 
		catch (ArgumentNullException ex) 
		{ 
			Console.Write("name is null."); 
			Console.Write("The Exception Thrown is thrown: "); 
			Console.Write("{0}", ex.GetType(), ex.Message); 
		} 
	} 
} 
public class PersonClass{ 
	public class StudentClass{ 
		// string names;
		// string dept;
		// int id
	} 
	public class TeacherClass{ 
		// string names;
		// string dept;
		// int id
	} 
}

Output:

Output

NestedType of the current type is: PersonClass+StudentClass

Example 2:

Let us take another example to illustrate the Type.GetNestedType function in C#.

Filename: NestedType2.cs

Example

using System; 
using System.Globalization; 
using System.Reflection; 
// An empty class declaration
public class Empty { } 

class NestedType{ 

	// Main Method 
	public static void Main() 
	{ 
		//Declaring the object type
		Type objTy = typeof(Person); 

		// try block for handling exceptions
		try { 
			Type typeObj= objTy.GetNestedType(null, 
			BindingFlags.Public | BindingFlags.Instance); 
			// Display the Result 
			Console.Write("Nested type of the current data type: "); 
			Console.WriteLine(" {0}", typeObj); 
		} 
		// the catch argument
		catch (ArgumentNullException ex) 
		{ 
			Console.WriteLine("The name is null."); 
			Console.Write("The Exception is Thrown: "); 
			Console.Write("{0}", ex.GetType(), ex.Message); 
		} 
	} 
} 

// The Person Class
public class Person { 
	// Defining class Student 
	public class Student { 
		// string names; 
		// int rollNum; 
		// string deptName; 
	} 
	// class
	public class Teacher { 

		// string names; 
		// string deptName; 
		// int id's; 
	} 
}

Output:

Output

The name is null.
The Exception is Thrown: System.ArgumentNullException

Example 3:

Let us take another example to illustrate the Type.GetNestedType function in C#.

Example

using System; 
using System. Globalization; 
using System.Reflection; 
// An empty class declaration
public class Empty { } 
class NestedType{ 
	// Main Method 
	public static void Main() 
	{ 
		//Declaring the object type
		Type objTy = typeof(Person); 
		// try block for handling exceptions
		try { 
			Type typeObj= objTy.GetNestedType(null, 
			BindingFlags.Public | BindingFlags.Instance); 
			// Display the Result 
			Console.Write("Nested type of the current data type: "); 
			Console.WriteLine(" {0}", typeObj); 
		} 
		// the catch argument
		catch (ArgumentNullException ex) 
		{ 
			Console.WriteLine("The name is null."); 
			Console.Write("The Exception is Thrown: "); 
			Console.Write("{0}", ex.GetType(), ex.Message); 
		} 
	} 
} 
// The Person Class
public class Person { 
	// Defining class Student 
	public class Student { 
		// string names; 
		// int rollNum; 
		// string deptName; 
	} 
	// class
	public class Teacher { 

		// string names; 
		// string deptName; 
		// int id's; 
	} 
}

Output:

Output

The name is null.
The Exception is Thrown: System.ArgumentNullException

The GetNestedType(String) method:

This method searches for the public nested type that matches the provided name.

Syntax:

It has the following syntax:

Example

Public Type GetNestedType (string name);

In this case, the string holding the nested type's name is required to be obtained.

Return Value:

If a public nested type with the provided name is found, this function produces an object representing it; otherwise, null.

Exception: If the name is null, this function is going to throw an ArgumentNullException.

Example:

Let us take another example to illustrate the Type.GetNestedType function in C#.

Example

using System; 
using System.Globalization; 
using System.Reflection; 

// An empty class
public class Empty { } 

class GetNestedType{ 

	// Main Method 
	public static void Main() 
	{ 
		// an object type declaration
		Type objType = typeof(Person); 
		// exception handling of the try block
		try { 
			Type typeObj= objType.GetNestedType("Teachers"); 
			// Display the Result 
			Console.Write("The NestedType of the current type is: "); 
			Console.WriteLine(" {0}", typeObj); 
		} 
		// catch statement for exception
		catch (ArgumentNullException ex) 
		{ 
			Console.WriteLine("The name is null."); 
			Console.Write("The Exception is Thrown: "); 
			Console.Write("{0}", ex.GetType(), ex.Message); 
		} 
	} 
} 
// the person class
public class Person { 
	// Defining class Student 
	public class Students { 
		// string names; 
		// int rollNum; 
		// string deptName; 
	} 
	// Defining the Teachers class
	public class Teachers { 
		// string names; 
		// string deptName; 
		// int id's; 
	} 
}

Output:

Output

The NestedType of the current type is:  Person+Teachers

Example 5:

Let us take another example to illustrate the Type.GetNestedType function in C#.

Example

using System; 
using System.Globalization; 
using System.Reflection; 

// Defining class Empty 
public class Empty { } 

class GetNestedType{ 

	// Main Method 
	public static void Main() 
	{ 
		// the object and its type declaration
		Type objTy = typeof(Person); 

		// try block for exceptions
		try { 

			Type tp = objTy.GetNestedType(null); 

			// The output statement
			Console.Write("The NestedType of the current type is: "); 
			Console.WriteLine(" {0}", tp); 
		} 

		// The catch argument for exception handling
		catch (ArgumentNullException ex) 
		{ 
			Console.WriteLine("The name is null."); 
			Console.Write("The Exception is Thrown:"); 
			Console.Write("{0}", ex.GetType(), ex.Message); 
		} 
	} 
} 

// the person class 
public class Person { 

	// Defining class Student 
	public class Faculty { 

		// string names; 
		// int rollNum; 
		// string deptName; 
	} 

	// the class declaration
	public class Teacher { 

		// string names; 
		// string deptName; 
		// int id's; 
	} 
}

Output:

Output

The name is null.
The Exception is Thrown: System.ArgumentNullException

Input Required

This code uses input(). Please provide values below: