在 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