Type.GetNestedType() Method in C#

The Type.GetNestedType() method in C# is used to retrieve a specific nested type that is declared within the current type. This method is part of the System.Reflection namespace and provides access to nested classes, interfaces, structs, or other types defined inside a parent type.

Syntax

Following are the overloads for the GetNestedType() method −

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

Parameters

  • name − The string containing the simple name of the nested type to get.

  • bindingAttr − A combination of enumeration values that specify how the search is conducted, or zero to return null if the type is not found.

Return Value

Returns a Type object representing the nested type that matches the specified requirements, or null if the nested type is not found.

Nested Type Structure Subject (Parent Type) BasicSubject AdvSubject GetNestedType("AdvSubject") returns the AdvSubject type

Using GetNestedType() to Retrieve Nested Types

Example

using System;

public class Demo {
   public static void Main() {
      Type type1 = typeof(Subject);
      try {
         Type type2 = type1.GetNestedType("AdvSubject");
         Console.WriteLine("NestedType = " + type2);
         
         Type type3 = type1.GetNestedType("BasicSubject");
         Console.WriteLine("NestedType = " + type3);
      }
      catch (ArgumentNullException e) {
         Console.WriteLine("{0}", e.GetType());
      }
   }
}

public class Subject {
   public class BasicSubject {
      public void ShowInfo() {
         Console.WriteLine("This is BasicSubject");
      }
   }
   
   public class AdvSubject {
      public void ShowInfo() {
         Console.WriteLine("This is AdvSubject");
      }
   }
}

The output of the above code is −

NestedType = Subject+AdvSubject
NestedType = Subject+BasicSubject

Handling ArgumentNullException

Example

using System;

public class Demo {
   public static void Main() {
      Type type1 = typeof(Subject);
      try {
         Type type2 = type1.GetNestedType(null);
         Console.WriteLine("NestedType = " + type2);
      }
      catch (ArgumentNullException e) {
         Console.WriteLine("Exception: {0}", e.GetType());
         Console.WriteLine("Message: {0}", e.Message);
      }
   }
}

public class Subject {
   public class BasicSubject {
      // Empty nested class
   }
   
   public class AdvSubject {
      // Empty nested class
   }
}

The output of the above code is −

Exception: System.ArgumentNullException
Message: Value cannot be null. (Parameter 'name')

Using BindingFlags with GetNestedType()

Example

using System;
using System.Reflection;

public class Demo {
   public static void Main() {
      Type type1 = typeof(Container);
      
      // Get public nested type
      Type publicType = type1.GetNestedType("PublicNested", BindingFlags.Public);
      Console.WriteLine("Public nested type: " + publicType);
      
      // Get private nested type
      Type privateType = type1.GetNestedType("PrivateNested", BindingFlags.NonPublic);
      Console.WriteLine("Private nested type: " + privateType);
      
      // Try to get private type with public binding (returns null)
      Type nullType = type1.GetNestedType("PrivateNested", BindingFlags.Public);
      Console.WriteLine("Private type with public binding: " + nullType);
   }
}

public class Container {
   public class PublicNested {
      // Public nested class
   }
   
   private class PrivateNested {
      // Private nested class
   }
}

The output of the above code is −

Public nested type: Container+PublicNested
Private nested type: Container+PrivateNested
Private type with public binding: 

Common Use Cases

  • Reflection-based frameworks − Used in serialization libraries and dependency injection containers to discover nested types.

  • Dynamic type loading − Useful when working with types loaded at runtime where nested type structure needs to be explored.

  • Code analysis tools − Employed by static analysis tools to examine type hierarchies and nested relationships.

Conclusion

The Type.GetNestedType() method provides a way to retrieve nested types defined within a parent type using reflection. It supports both simple name-based lookup and more complex searches using BindingFlags to control visibility and access levels of the nested types being retrieved.

Updated on: 2026-03-17T07:04:35+05:30

189 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements