The Type.GetNestedType method is part of the System.Reflection namespace and serves the purpose of retrieving a nested type within a specified type. Reflection stands as a robust functionality in C# that empowers developers to inspect and manipulate types' metadata while the program is executing.
Fundamentals of Reflection:
Before diving into the Type.GetNestedType method, it is crucial to have a solid grasp of reflection basics, which are essential for effectively utilizing GetNestedType. In C#, types serve as representations of various entities such as classes, interfaces, structures, enums, and more. The Type class belongs to the System namespace and provides a range of methods and properties designed for examining the metadata associated with these entities.
Reflection is commonly employed in scenarios where the type structure is not known at compile time, enabling programmers to inspect and manipulate types dynamically at runtime.
Purpose of Type.GetNestedType
The Type.GetNestedType method focuses on nested types within a specified type. A nested type is defined within another type, like a class or an interface. This method enables the retrieval of a nested type based on its name.
Syntax:
It has the following syntax:
Retrieve a nested type by providing the type's name and the binding attribute using the GetNestedType method.
Parameters:
name: The string containing the name that identifies the nested type to retrieve.
The bitmask consists of one or more BindingFlags that specify the method of search execution. Otherwise, a value of zero will result in a null return.
Example:
Let's consider a scenario to demonstrate the implementation of the Type.GetNestedType method in C# programming language.
FileName: GetNestedType1.cs
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:
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
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:
The name is null.
The Exception is Thrown: System.ArgumentNullException
Example 3:
Let's consider a different instance to demonstrate the Type.GetNestedType method in C#.
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:
The name is null.
The Exception is Thrown: System.ArgumentNullException
The GetNestedType(String) method:
This function looks for the public inner class that corresponds to the given name.
Syntax:
It has the following syntax:
Public Type GetNestedType (string name);
In this scenario, it is necessary to retrieve the string that holds the name of the nested type.
Return Value:
If a nested type with the specified name is discovered as public, this method generates an object that represents it. If not, it returns null.
In case the name parameter is null, this function will raise an ArgumentNullException.
Example:
Let's consider another instance to demonstrate the Type.GetNestedType method in C#.
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:
The NestedType of the current type is: Person+Teachers
Example 5:
Let's consider a different scenario to demonstrate the Type.GetNestedType method in C#.
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:
The name is null.
The Exception is Thrown: System.ArgumentNullException