当前位置: 首页>>代码示例>>C#>>正文


C# Type.GetNestedTypes方法代码示例

本文整理汇总了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);  
        }         
    }
}
开发者ID:.NET开发者,项目名称:System,代码行数:35,代码来源:Type.GetNestedTypes

示例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);
    }
}
开发者ID:.NET开发者,项目名称:System,代码行数:50,代码来源:Type.GetNestedTypes

输出:

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.


注:本文中的System.Type.GetNestedTypes方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。