當前位置: 首頁>>代碼示例>>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;未經允許,請勿轉載。