Open In App

C# | Type.GetNestedType() Method

Improve
Improve
Like Article
Like
Save
Share
Report

Type.GetNestedType() Method is used to get a specific type nested within the current Type. There are 2 methods in the overload list of this method as follows:

  • GetNestedType(String, BindingFlags) Method
  • GetNestedType(String) Method

GetNestedType(String, BindingFlags) Method

This method is used to search for the specified nested type, using the specified binding constraints when overridden in a derived class.

Syntax: public abstract Type GetNestedType (string name, System.Reflection.BindingFlags bindingAttr);

Parameters:
name: The string containing the name of the nested type to get.
bindingAttr: A bitmask comprised of one or more BindingFlags that specify how the search is conducted Or Zero, to return null.

Return Value: This method returns an object representing the nested type which matches the specified requirements if found; otherwise, null.

Exception: This method throws ArgumentNullException if name is null.

Below programs illustrate the use of the above-discussed method:

Example 1:




// C# program to demonstrate the
// Type.GetNestedType(String, 
// BindingFlags) Method
using System;
using System.Globalization;
using System.Reflection;
  
// Defining class Empty
public class Empty { }
  
class GFG {
  
    // Main Method
    public static void Main()
    {
        // Declaring and initializing object of Type
        Type objType = typeof(Person);
  
        // try-catch block for handling Exception
        try {
  
            Type type = objType.GetNestedType("Student",
               BindingFlags.Public | BindingFlags.Instance);
  
            // Display the Result
            Console.Write("NestedType of current type is: ");
            Console.WriteLine(" {0}", type);
        }
  
        // catch ArgumentNullException here
        catch (ArgumentNullException e) 
        {
            Console.Write("name is null.");
            Console.Write("Exception Thrown: ");
            Console.Write("{0}", e.GetType(), e.Message);
        }
    }
}
  
public class Person {
  
    public class Student {
  
        // string name;
        // int roll;
        // string dept;
    }
  
    public class Teacher {
  
        // string name;
        // string dept;
        // int id;
    }
}


Output:

NestedType of current type is:  Person+Student

Example 2:




// C# program to demonstrate the
// Type.GetNestedType(String, 
// BindingFlags) Method
using System;
using System.Globalization;
using System.Reflection;
  
// Defining class Empty
public class Empty { }
  
class GFG {
  
    // Main Method
    public static void Main()
    {
        // Declaring and initializing object of Type
        Type objType = typeof(Person);
  
        // try-catch block for handling Exception
        try {
  
            Type type = objType.GetNestedType(null,
               BindingFlags.Public | BindingFlags.Instance);
  
            // Display the Result
            Console.Write("NestedType of current type is: ");
            Console.WriteLine(" {0}", type);
        }
  
        // catch ArgumentNullException here
        catch (ArgumentNullException e) 
        {
            Console.WriteLine("name is null.");
            Console.Write("Exception Thrown: ");
            Console.Write("{0}", e.GetType(), e.Message);
        }
    }
}
  
// Defining Person class
public class Person {
  
    // Defining class Student
    public class Student {
  
        // string name;
        // int roll;
        // string dept;
    }
  
    // Defining class Teacher
    public class Teacher {
  
        // string name;
        // string dept;
        // int id;
    }
}


Output:

name is null.
Exception Thrown: System.ArgumentNullException

GetNestedType(String) Method

This method is used to search for the public nested type with the specified name.

Syntax: public Type GetNestedType (string name);
Here, it takes the string containing the name of the nested type to get.

Return Value: This method returns an object representing the public nested type with the specified name if found; otherwise, null.

Exception: This method throws ArgumentNullException if name is null.

Example 1:




// C# program to demonstrate the
// Type.GetNestedType(String) Method
using System;
using System.Globalization;
using System.Reflection;
  
// Defining class Empty
public class Empty { }
  
class GFG {
  
    // Main Method
    public static void Main()
    {
        // Declaring and initializing object of Type
        Type objType = typeof(Person);
  
        // try-catch block for handling Exception
        try {
  
            Type type = objType.GetNestedType("Teacher");
  
            // Display the Result
            Console.Write("NestedType of current type is: ");
            Console.WriteLine(" {0}", type);
        }
  
        // catch ArgumentNullException here
        catch (ArgumentNullException e) 
        {
            Console.WriteLine("name is null.");
            Console.Write("Exception Thrown: ");
            Console.Write("{0}", e.GetType(), e.Message);
        }
    }
}
  
// Defining Person class
public class Person {
  
    // Defining class Student
    public class Student {
  
        // string name;
        // int roll;
        // string dept;
    }
  
    // Defining class Teacher
    public class Teacher {
  
        // string name;
        // string dept;
        // int id;
    }
}


Output:

NestedType of current type is:  Person+Teacher

Example 2:




// C# program to demonstrate the
// Type.GetNestedType(String) 
// Method
using System;
using System.Globalization;
using System.Reflection;
  
// Defining class Empty
public class Empty { }
  
class GFG {
  
    // Main Method
    public static void Main()
    {
        // Declaring and initializing object of Type
        Type objType = typeof(Person);
  
        // try-catch block for handling Exception
        try {
  
            Type type = objType.GetNestedType(null);
  
            // Display the Result
            Console.Write("NestedType of current type is: ");
            Console.WriteLine(" {0}", type);
        }
  
        // catch ArgumentNullException here
        catch (ArgumentNullException e)
        {
            Console.WriteLine("name is null.");
            Console.Write("Exception Thrown: ");
            Console.Write("{0}", e.GetType(), e.Message);
        }
    }
}
  
// Defining Person class
public class Person {
  
    // Defining class Student
    public class Student {
  
        // string name;
        // int roll;
        // string dept;
    }
  
    // Defining class Teacher
    public class Teacher {
  
        // string name;
        // string dept;
        // int id;
    }
}


Output:

name is null.
Exception Thrown: System.ArgumentNullException

Reference:



Last Updated : 10 Dec, 2019
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads