Type.GetNestedType()方法用於獲取嵌套在當前Type中的特定類型。此方法的重載列表中有2種方法,如下所示:
- GetNestedType(String,BindingFlags)方法
- GetNestedType(String)方法
GetNestedType(String, BindingFlags) Method
此方法用於在派生類中重寫時使用指定的綁定約束來搜索指定的嵌套類型。
用法: public abstract Type GetNestedType (string name, System.Reflection.BindingFlags bindingAttr);
參數:
name:包含要獲取的嵌套類型名稱的字符串。
bindingAttr:由一個或多個BindingFlags組成的位掩碼,用於指定如何進行搜索或為零,以返回null。
返回值:此方法返回一個表示嵌套類型的對象,該對象與指定的條件相匹配(如果找到);否則為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) Method
此方法用於搜索具有指定名稱的公共嵌套類型。
用法: public Type GetNestedType (string name);
Here, it takes the string containing the name of the nested type to get.
返回值:此方法返回一個對象,該對象表示具有指定名稱的公共嵌套類型(如果找到);否則為null。
異常:如果name為null,則此方法將引發ArgumentNullException。
示例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
參考:
相關用法
- C# DateTimeOffset.Add()用法及代碼示例
- C# String.Contains()用法及代碼示例
- C# Math.Sin()用法及代碼示例
- C# Math.Cos()用法及代碼示例
- C# Dictionary.Add()用法及代碼示例
- C# Math.Tan()用法及代碼示例
- C# Math.Abs()方法用法及代碼示例
- C# Math.Exp()用法及代碼示例
- C# Math.Abs()函數用法及代碼示例
注:本文由純淨天空篩選整理自RohitPrasad3大神的英文原創作品 C# | Type.GetNestedType() Method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。