Type.GetNestedType()方法用于获取嵌套在当前Type中的特定类型。此方法的重载列表中有2种方法,如下所示:
- GetNestedType(String,BindingFlags)方法
- GetNestedType(String)方法
GetNestedType(String,BindingFlags)方法
当在派生类中重写时,此方法用于使用指定的绑定约束来搜索指定的嵌套类型。
Syntax: public abstract Type GetNestedType (string name, System.Reflection.BindingFlags bindingAttr);
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 Or Zero, to return null.
返回值:此方法返回一个表示嵌套类型的对象,该对象与指定的条件相匹配(如果找到);否则,返回false。否则为null。
异常:如果name为null,则此方法将引发ArgumentNullException。
下面的程序说明了上述方法的用法:
范例1:
// C# program to demonstrate the
// Type.GetNestedType(String,
// BindingFlags) 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 {
Type type = objType.GetNestedType("Student",
BindingFlags.Public | BindingFlags.Instance);
// Display the Result
Console.Write("NestedType of current type is: ");
Console.WriteLine(" {0}", type);
}
// 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;
}
}
NestedType of current type is: Person+Student
范例2:
// C# program to demonstrate the
// Type.GetNestedType(String,
// BindingFlags) 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 {
Type type = objType.GetNestedType(null,
BindingFlags.Public | BindingFlags.Instance);
// Display the Result
Console.Write("NestedType of current type is: ");
Console.WriteLine(" {0}", type);
}
// catch ArgumentNullException here
catch (ArgumentNullException e)
{
Console.WriteLine("name is null.");
Console.Write("Exception Thrown: ");
Console.Write("{0}", e.GetType(), e.Message);
}
}
}
// Defining Person class
public class Person {
// Defining class Student
public class Student {
// string name;
// int roll;
// string dept;
}
// Defining class Teacher
public class Teacher {
// string name;
// string dept;
// int id;
}
}
name is null.
Exception Thrown: System.ArgumentNullException
GetNestedType(String)方法
此方法用于搜索具有指定名称的公共嵌套类型。
Syntax: public Type GetNestedType (string name);
Here, it takes the string containing the name of the nested type to get.
Return Value: This method returns an object representing the public nested type with the specified name if found; otherwise, null.
Exception: This method throws ArgumentNullException if name is null.
范例1:
// C# program to demonstrate the
// Type.GetNestedType(String) 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 {
Type type = objType.GetNestedType("Teacher");
// Display the Result
Console.Write("NestedType of current type is: ");
Console.WriteLine(" {0}", type);
}
// catch ArgumentNullException here
catch (ArgumentNullException e)
{
Console.WriteLine("name is null.");
Console.Write("Exception Thrown: ");
Console.Write("{0}", e.GetType(), e.Message);
}
}
}
// Defining Person class
public class Person {
// Defining class Student
public class Student {
// string name;
// int roll;
// string dept;
}
// Defining class Teacher
public class Teacher {
// string name;
// string dept;
// int id;
}
}
NestedType of current type is: Person+Teacher
范例2:
// C# program to demonstrate the
// Type.GetNestedType(String)
// 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 {
Type type = objType.GetNestedType(null);
// Display the Result
Console.Write("NestedType of current type is: ");
Console.WriteLine(" {0}", type);
}
// catch ArgumentNullException here
catch (ArgumentNullException e)
{
Console.WriteLine("name is null.");
Console.Write("Exception Thrown: ");
Console.Write("{0}", e.GetType(), e.Message);
}
}
}
// Defining Person class
public class Person {
// Defining class Student
public class Student {
// string name;
// int roll;
// string dept;
}
// Defining class Teacher
public class Teacher {
// string name;
// string dept;
// int id;
}
}
name is null.
Exception Thrown: System.ArgumentNullException
参考:
- https://docs.microsoft.com/zh-cn/dotnet/api/system.type.getnestedtype?view=netframework-4.8