Skip to content
Related Articles

Related Articles

C# | Type.GetNestedTypes() Method

View Discussion
Improve Article
Save Article
  • Last Updated : 07 Sep, 2022
View Discussion
Improve Article
Save Article

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

GetNestedTypes() Method

This method is used to return the public types nested in the current Type.
 

Syntax: public Type[] GetNestedTypes ();
Return Value: This method returns an array of Type objects representing the public types nested in the current Type (the search is not recursive), or an empty array of type  if no public types are nested in the current Type. 
 

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

csharp




// C# program to demonstrate the
// Type.GetNestedTypes() 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 {
 
            // Getting array of Method by
            // using GetMethods() Method
            Type[] type = objType.GetNestedTypes();
 
            // Display the Result
            Console.WriteLine("NestedType of current type is: ");
            for (int i = 0; i < type.Length; i++)
                Console.WriteLine("{0} ", type[i]);
        }
 
        // 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 
Person+Teacher

 

Example 2:
 

csharp




// C# program to demonstrate the
// Type.GetNestedTypes() 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(Animal);
 
        // try-catch block for handling Exception
        try {
 
            // Getting array of Method by
            // using GetMethods() Method
            Type[] type = objType.GetNestedTypes();
 
            // Display the Result
            Console.WriteLine("NestedType of current type is: ");
            for (int i = 0; i < type.Length; i++)
                Console.WriteLine(" {0}", type[i]);
        }
 
        // catch ArgumentNullException here
        catch (ArgumentNullException e)
        {
            Console.Write("name is null.");
            Console.Write("Exception Thrown: ");
            Console.Write("{0}", e.GetType(), e.Message);
        }
    }
}
 
public class Animal {
 
    public class Cat {
 
        // string breed;
        // string color;
        // string type;
    }
 
    public class Dog {
 
        // string breed;
        // string color;
        // string type;
    }
 
    public class Mouse {
 
        // string breed;
        // string color;
        // string type;
    }
 
    public interface Description {
 
        string getBreed();
        string getColor();
        string getType();
        bool isAlive();
    }
}

Output

NestedType of current type is: 
 Animal+Cat
 Animal+Dog
 Animal+Mouse
 Animal+Description

GetNestedTypes(BindingFlags) Method

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

Syntax: public abstract Type[] GetNestedTypes (System.Reflection.BindingFlags bindingAttr); 
Here, it takes 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 array of Type objects representing all the types nested in the current Type that match the specified binding constraints (the search is not recursive), or an empty array of type Type if no nested types are found that match the binding constraints. 
 

Example 1:
 

csharp




// C# program to demonstrate the
// Type.GetNestedTypes() 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(Animal);
 
        // try-catch block for handling Exception
        try {
 
            // Getting array of Method by
            // using GetMethods() Method
            Type[] type = objType.GetNestedTypes();
 
            // Display the Result
            Console.WriteLine("NestedType of current type is: ");
            for (int i = 0; i < type.Length; i++)
                Console.WriteLine(" {0}", type[i]);
        }
 
        // catch ArgumentNullException here
        catch (ArgumentNullException e)
        {
            Console.Write("name is null.");
            Console.Write("Exception Thrown: ");
            Console.Write("{0}", e.GetType(), e.Message);
        }
    }
}
 
public class Animal {
 
    public class Cat {
 
        // string breed;
        // string color;
        // string type;
    }
 
    public class Dog {
 
        // string breed;
        // string color;
        // string type;
    }
 
    public class Mouse {
 
        // string breed;
        // string color;
        // string type;
    }
 
    public interface Description {
 
        string getBreed();
        string getColor();
        string getType();
        bool isAlive();
    }
}

Output: 

NestedType of current type is: 
 Animal+Empty

 

Example 2:
 

csharp




// C# program to demonstrate the
// Type.GetNestedTypes() 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 {
 
            // Getting array of Method by
            // using GetMethods() Method
            Type[] type = objType.GetNestedTypes(BindingFlags.Public);
 
            // Display the Result
            Console.WriteLine("NestedType of current type is: ");
 
            for (int i = 0; i < type.Length; i++)
                Console.WriteLine(" {0}", type[i]);
        }
 
        // 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
 Person+Teacher

 

Reference:
 

 


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!