Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Get a specific type nested within the current Type in C#
The GetNestedType() method in C# is used to retrieve a specific nested type (inner class) from within the current type. This method is part of the Type class and helps in reflection scenarios where you need to access inner classes dynamically.
Syntax
Following is the syntax for the GetNestedType() method −
public Type GetNestedType(string name) public Type GetNestedType(string name, BindingFlags bindingAttr)
Parameters
name − The name of the nested type to retrieve.
bindingAttr − Optional. Specifies how the search is conducted (public, non-public, etc.).
Return Value
Returns a Type object representing the nested type if found, or null if the nested type does not exist. Throws ArgumentNullException if the name parameter is null.
Using GetNestedType() to Retrieve Nested Classes
This example demonstrates how to get a specific nested type from a class −
using System;
public class Demo {
public static void Main() {
Type type1 = typeof(Subject);
try {
Type type2 = type1.GetNestedType("AdvSubject");
Console.WriteLine("NestedType = " + type2);
}
catch (ArgumentNullException e) {
Console.WriteLine("{0}", e.GetType());
}
}
}
public class Subject {
public class BasicSubject {
public void DisplayBasic() {
Console.WriteLine("This is BasicSubject");
}
}
public class AdvSubject {
public void DisplayAdvanced() {
Console.WriteLine("This is AdvSubject");
}
}
}
The output of the above code is −
NestedType = Subject+AdvSubject
Handling Null Parameter Exception
When passing null as the parameter name, the method throws an ArgumentNullException −
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("{0}", e.GetType());
}
}
}
public class Subject {
public class BasicSubject {
//
}
public class AdvSubject {
//
}
}
The output of the above code is −
System.ArgumentNullException
Using GetNestedType() with BindingFlags
This example shows how to access both public and non-public nested types using BindingFlags −
using System;
using System.Reflection;
public class Demo {
public static void Main() {
Type type1 = typeof(Subject);
// Get public nested type
Type publicType = type1.GetNestedType("PublicSubject", BindingFlags.Public);
Console.WriteLine("Public nested type: " + publicType?.Name);
// Get private nested type
Type privateType = type1.GetNestedType("PrivateSubject", BindingFlags.NonPublic);
Console.WriteLine("Private nested type: " + privateType?.Name);
// Try to get non-existent type
Type nonExistent = type1.GetNestedType("NonExistent");
Console.WriteLine("Non-existent type: " + (nonExistent == null ? "null" : nonExistent.Name));
}
}
public class Subject {
public class PublicSubject {
//
}
private class PrivateSubject {
//
}
}
The output of the above code is −
Public nested type: PublicSubject Private nested type: PrivateSubject Non-existent type: null
Conclusion
The GetNestedType() method is useful for reflection scenarios where you need to dynamically access nested classes within a type. It returns the specified nested type if found, or null if it doesn't exist, and throws ArgumentNullException for null parameters.
