Javatpoint Logo
Javatpoint Logo

Type.GetNestedType() Method in C#

The Type.GetNestedType function belongs to the System.Reflection namespace that is used to get a nested type within a given type. Reflection is a powerful C# feature that allows programmers to see and interact with types' metadata during runtime.

Fundamentals of Reflection:

Before we go into Type.GetNestedType method, understanding the fundamentals of reflection is critical when using GetNestedType. Types are C# entities that represent classes, interfaces, structures, enums, and other concepts. The Type class is part of the System namespace and includes methods and properties for inspecting the metadata of these kinds.

Reflection is extensively used when the type structure is undefined at compile time, allowing developers to view and interact with types dynamically during runtime.

Purpose of Type.GetNestedType

The Type.GetNestedType function is particularly concerned with nested types inside a given type. A nested type is one that is declared within another type, such as a class or an interface. This function allows us to obtain a nested type using its name.

Syntax:

It has the following syntax:

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

Parameters:

name: The string with the name that represents the nested type to get.

bindingAttr: A bitmask is made up of one or more BindingFlags that describe how the search is performed. Or else zero will return null.

Example:

Let us take an example to illustrate the Type.GetNestedType function in C#.

FileName: GetNestedType1.cs

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

Output:

The name is null.
The Exception is Thrown: System.ArgumentNullException

Example 3:

Let us take another example to illustrate the Type.GetNestedType function in C#.

Output:

The name is null.
The Exception is Thrown: System.ArgumentNullException

The GetNestedType(String) method:

This method searches for the public nested type that matches the provided name.

Syntax:

It has the following syntax:

In this case, the string holding the nested type's name is required to be obtained.

Return Value:

If a public nested type with the provided name is found, this function produces an object representing it; otherwise, null.

Exception: If the name is null, this function is going to throw an ArgumentNullException.

Example:

Let us take another example to illustrate the Type.GetNestedType function in C#.

Output:

The NestedType of the current type is:  Person+Teachers

Example 5:

Let us take another example to illustrate the Type.GetNestedType function in C#.

Output:

The name is null.
The Exception is Thrown: System.ArgumentNullException






Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA




news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news