本文整理汇总了C#中System.Type.GetNestedTypes方法的典型用法代码示例。如果您正苦于以下问题:C# Type.GetNestedTypes方法的具体用法?C# Type.GetNestedTypes怎么用?C# Type.GetNestedTypes使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Type
的用法示例。
在下文中一共展示了Type.GetNestedTypes方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Main
//引入命名空间
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
{
// Get the Type object corresponding to MyClass.
Type myType=typeof(MyClass);
// Get an array of nested type objects in MyClass.
Type[] nestType=myType.GetNestedTypes();
Console.WriteLine("The number of nested types is {0}.", nestType.Length);
foreach(Type t in nestType)
Console.WriteLine("Nested type is {0}.", t.ToString());
}
catch(Exception e)
{
Console.WriteLine("Error"+e.Message);
}
}
}
示例2: Main
//引入命名空间
using System;
using System.Reflection;
// Create a class with 2 nested public and 2 nested protected classes.
public class MyTypeClass
{
public class Myclass1
{
}
public class Myclass2
{
}
protected class MyClass3
{
}
protected class MyClass4
{
}
}
public class TypeMain
{
public static void Main()
{
Type myType = (typeof(MyTypeClass));
// Get the public nested classes.
Type[] myTypeArray = myType.GetNestedTypes(BindingFlags.Public);
Console.WriteLine("The number of nested public classes is {0}.", myTypeArray.Length);
// Display all the public nested classes.
DisplayTypeInfo(myTypeArray);
Console.WriteLine();
// Get the nonpublic nested classes.
Type[] myTypeArray1 = myType.GetNestedTypes(BindingFlags.NonPublic|BindingFlags.Instance);
Console.WriteLine("The number of nested protected classes is {0}.", myTypeArray1.Length);
// Display all the nonpublic nested classes.
DisplayTypeInfo(myTypeArray1);
}
public static void DisplayTypeInfo(Type[] myArrayType)
{
// Display the information for all the nested classes.
foreach (var t in myArrayType)
Console.WriteLine("The name of the nested class is {0}.", t.FullName);
}
}
输出:
The number of public nested classes is 2. The name of the nested class is MyTypeClass+Myclass1. The name of the nested class is MyTypeClass+Myclass2. The number of protected nested classes is 2. The name of the nested class is MyTypeClass+MyClass3. The name of the nested class is MyTypeClass+MyClass4.