C# Type GetNestedType(String, BindingFlags)

Description

Type GetNestedType(String, BindingFlags) when overridden in a derived class, searches for the specified nested type, using the specified binding constraints.

Syntax

Type.GetNestedType(String, BindingFlags) has the following syntax.


public abstract Type GetNestedType(
  string name,
  BindingFlags bindingAttr
)

Parameters

Type.GetNestedType(String, BindingFlags) has the following 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.
  • bindingAttr - -or-
  • bindingAttr - Zero, to return null.

Returns

Type.GetNestedType(String, BindingFlags) method returns An object representing the nested type that matches the specified requirements, if found; otherwise, null.

Example


// w  ww . j av a  2s .c om
using System;
using System.Reflection;
public class MyClass
{
    public class NestClass
    {
        public static int myPublicInt = 0;
    }
    public struct NestStruct
    {
        public static int myPublicInt = 0;
    }
}

public class MyMainClass
{
    public static void Main() 
    {
        try
        {
            Type myType=typeof(MyClass);

            Type nestType=myType.GetNestedType("NestClass",BindingFlags.DeclaredOnly);
            Console.WriteLine(nestType);
        }
        catch(Exception e)
        {
            Console.WriteLine("Error"+e.Message);  
        }         
    }
}

The code above generates the following result.





















Home »
  C# Tutorial »
    System »




Array
BitConverter
Boolean
Byte
Char
Console
ConsoleKeyInfo
Convert
DateTime
DateTimeOffset
Decimal
Double
Enum
Environment
Exception
Guid
Int16
Int32
Int64
Math
OperatingSystem
Random
SByte
Single
String
StringComparer
TimeSpan
TimeZone
TimeZoneInfo
Tuple
Tuple
Tuple
Type
UInt16
UInt32
UInt64
Uri
Version