在 C# 中獲取當前型別內巢狀的特定型別
要獲取當前型別中巢狀的特定型別,程式碼如下所示 -
示例
using System;
public class Demo {
public static void Main() {
Type type1 = typeof(Subject);
try {
Type type2 = type1.GetNestedType("AdvSubject");
Console.Write("NestedType = "+ type2);
}
catch (ArgumentNullException e) {
Console.Write("{0}", e.GetType(), e.Message);
}
}
}
public class Subject {
public class BasicSubject {
//
}
public class AdvSubject {
//
}
}輸出
這將產生以下輸出 -
NestedType = Subject+AdvSubject
示例
我們來看另一個示例 -
using System;
public class Demo {
public static void Main() {
Type type1 = typeof(Subject);
try {
Type type2 = type1.GetNestedType(null);
Console.Write("NestedType = "+ type2);
}
catch (ArgumentNullException e) {
Console.Write("{0}", e.GetType(), e.Message);
}
}
}
public class Subject {
public class BasicSubject {
//
}
public class AdvSubject {
//
}
}輸出
這將產生以下輸出 -
System.ArgumentNullException
廣告
資料結構
網路
RDBMS
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP