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。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。