当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


C# Type.GetNestedType()用法及代码示例


Type.GetNestedType()方法用于获取嵌套在当前Type中的特定类型。此方法的重载列表中有2种方法,如下所示:

    • GetNestedType(String,BindingFlags)方法
    • GetNestedType(String)方法

GetNestedType(String, BindingFlags) Method

此方法用于在派生类中重写时使用指定的绑定约束来搜索指定的嵌套类型。


用法: public abstract Type GetNestedType (string name, System.Reflection.BindingFlags bindingAttr);

参数
name:包含要获取的嵌套类型名称的字符串。
bindingAttr:由一个或多个BindingFlags组成的位掩码,用于指定如何进行搜索或为零,以返回null。

返回值:此方法返回一个表示嵌套类型的对象,该对象与指定的条件相匹配(如果找到);否则为null。

异常:如果name为null,则此方法将引发ArgumentNullException。

以下示例程序旨在说明上述方法的用法:

示例1:

// C# program to demonstrate the 
// Type.GetNestedType(String,  
// BindingFlags) 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 { 
  
            Type type = objType.GetNestedType("Student", 
               BindingFlags.Public | BindingFlags.Instance); 
  
            // Display the Result 
            Console.Write("NestedType of current type is: "); 
            Console.WriteLine(" {0}", type); 
        } 
  
        // 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

示例2:

// C# program to demonstrate the 
// Type.GetNestedType(String,  
// BindingFlags) 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 { 
  
            Type type = objType.GetNestedType(null, 
               BindingFlags.Public | BindingFlags.Instance); 
  
            // Display the Result 
            Console.Write("NestedType of current type is: "); 
            Console.WriteLine(" {0}", type); 
        } 
  
        // catch ArgumentNullException here 
        catch (ArgumentNullException e)  
        { 
            Console.WriteLine("name is null."); 
            Console.Write("Exception Thrown: "); 
            Console.Write("{0}", e.GetType(), e.Message); 
        } 
    } 
} 
  
// Defining Person class 
public class Person { 
  
    // Defining class Student 
    public class Student { 
  
        // string name; 
        // int roll; 
        // string dept; 
    } 
  
    // Defining class Teacher 
    public class Teacher { 
  
        // string name; 
        // string dept; 
        // int id; 
    } 
}
输出:
name is null.
Exception Thrown: System.ArgumentNullException

GetNestedType(String) Method

此方法用于搜索具有指定名称的公共嵌套类型。


用法: public Type GetNestedType (string name);
Here, it takes the string containing the name of the nested type to get.

返回值:此方法返回一个对象,该对象表示具有指定名称的公共嵌套类型(如果找到);否则为null。

异常:如果name为null,则此方法将引发ArgumentNullException。

示例1:

// C# program to demonstrate the 
// Type.GetNestedType(String) 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 { 
  
            Type type = objType.GetNestedType("Teacher"); 
  
            // Display the Result 
            Console.Write("NestedType of current type is: "); 
            Console.WriteLine(" {0}", type); 
        } 
  
        // catch ArgumentNullException here 
        catch (ArgumentNullException e)  
        { 
            Console.WriteLine("name is null."); 
            Console.Write("Exception Thrown: "); 
            Console.Write("{0}", e.GetType(), e.Message); 
        } 
    } 
} 
  
// Defining Person class 
public class Person { 
  
    // Defining class Student 
    public class Student { 
  
        // string name; 
        // int roll; 
        // string dept; 
    } 
  
    // Defining class Teacher 
    public class Teacher { 
  
        // string name; 
        // string dept; 
        // int id; 
    } 
}
输出:
NestedType of current type is:  Person+Teacher

示例2:

// C# program to demonstrate the 
// Type.GetNestedType(String)  
// 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 { 
  
            Type type = objType.GetNestedType(null); 
  
            // Display the Result 
            Console.Write("NestedType of current type is: "); 
            Console.WriteLine(" {0}", type); 
        } 
  
        // catch ArgumentNullException here 
        catch (ArgumentNullException e) 
        { 
            Console.WriteLine("name is null."); 
            Console.Write("Exception Thrown: "); 
            Console.Write("{0}", e.GetType(), e.Message); 
        } 
    } 
} 
  
// Defining Person class 
public class Person { 
  
    // Defining class Student 
    public class Student { 
  
        // string name; 
        // int roll; 
        // string dept; 
    } 
  
    // Defining class Teacher 
    public class Teacher { 
  
        // string name; 
        // string dept; 
        // int id; 
    } 
}
输出:
name is null.
Exception Thrown: System.ArgumentNullException

参考:



相关用法


注:本文由纯净天空筛选整理自RohitPrasad3大神的英文原创作品 C# | Type.GetNestedType() Method。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。