Type.GetNestedTypes()方法用於獲取嵌套在當前Type中的類型。此方法的重載列表中有2種方法,如下所示:
- GetNestedTypes()方法
- GetNestedTypes(BindingFlags)方法
GetNestedTypes() Method
此方法用於返回嵌套在當前Type中的公共類型。
用法: public Type[] GetNestedTypes ();
返回值:此方法返回一個Type對象數組,該數組表示嵌套在當前Type中的公共類型(搜索不是遞歸的);如果當前類型中沒有嵌套的公共類型,則返回Type類型的空數組。
以下示例程序旨在說明上述方法的用法:
示例1:
// C# program to demonstrate the
// Type.GetNestedTypes() 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 {
// Getting array of Method by
// using GetMethods() Method
Type[] type = objType.GetNestedTypes();
// Display the Result
Console.WriteLine("NestedType of current type is: ");
for (int i = 0; i < type.Length; i++)
Console.WriteLine("{0} ", type[i]);
}
// 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 Person+Teacher
示例2:
// C# program to demonstrate the
// Type.GetNestedTypes() 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(Animal);
// try-catch block for handling Exception
try {
// Getting array of Method by
// using GetMethods() Method
Type[] type = objType.GetNestedTypes();
// Display the Result
Console.WriteLine("NestedType of current type is: ");
for (int i = 0; i < type.Length; i++)
Console.WriteLine(" {0}", type[i]);
}
// catch ArgumentNullException here
catch (ArgumentNullException e)
{
Console.Write("name is null.");
Console.Write("Exception Thrown: ");
Console.Write("{0}", e.GetType(), e.Message);
}
}
}
public class Animal {
public class Cat {
// string breed;
// string color;
// string type;
}
public class Dog {
// string breed;
// string color;
// string type;
}
public class Mouse {
// string breed;
// string color;
// string type;
}
public interface Descreption {
string getBreed();
string getColor();
string getType();
bool isAlive();
}
}
輸出:
NestedType of current type is: Animal+Cat Animal+Dog Animal+Mouse Animal+Descreption
GetNestedTypes(BindingFlags) Method
此方法用於在派生類中重寫時使用指定的綁定約束來搜索嵌套在當前Type中的類型。
用法: public abstract Type[] GetNestedTypes (System.Reflection.BindingFlags bindingAttr);
Here, it takes a bitmask comprised of one or more BindingFlags that specify how the search is conducted or, Zero, to return null.
返回值:此方法返回一個Type對象數組,該數組表示當前Type中嵌套的所有與指定綁定約束匹配的類型(搜索不是遞歸的);如果未找到與綁定約束匹配的嵌套類型,則返回Type類型的空數組。
示例1:
// C# program to demonstrate the
// Type.GetNestedTypes() 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(Animal);
// try-catch block for handling Exception
try {
// Getting array of Method by
// using GetMethods() Method
Type[] type = objType.GetNestedTypes(BindingFlags.NonPublic);
// Display the Result
Console.WriteLine("NestedType of current type is: ");
for (int i = 0; i < type.Length; i++)
Console.WriteLine(" {0}", type[i]);
}
// catch ArgumentNullException here
catch (ArgumentNullException e)
{
Console.Write("name is null.");
Console.Write("Exception Thrown: ");
Console.Write("{0}", e.GetType(), e.Message);
}
}
}
public class Animal {
public class Cat {
// string breed;
// string color;
// string type;
}
private class Empty { }
public interface Descreption
{
string getBreed();
string getColor();
string getType();
bool isAlive();
}
}
輸出:
NestedType of current type is: Animal+Empty
示例2:
// C# program to demonstrate the
// Type.GetNestedTypes() 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 {
// Getting array of Method by
// using GetMethods() Method
Type[] type = objType.GetNestedTypes(BindingFlags.Public);
// Display the Result
Console.WriteLine("NestedType of current type is: ");
for (int i = 0; i < type.Length; i++)
Console.WriteLine(" {0}", type[i]);
}
// 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 Person+Teacher
參考:
相關用法
- 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.GetNestedTypes() Method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。