C#:study(16)--运行期类型判别
- 使用is测试类型
格式:expr is type - 使用AS
功能:强制类型转换,但强制转换失败不引发异常。
格式:expr as type //将expr强制转换为type,如果转换成功,返回类型type的引用,否则返回空引用。 - 使用typeof
格式:typeof(type)
通过typeof为一个给定的类型得到一个System.Type对象。使得到的对象可以检索类型信息。实例:
1 using System;
2 using System.IO;
3 class UseType
4 {
5 public static void Main()
6 {
7 Type t = typeof(StreamReader);
8 Console.WriteLine(t.FullName);
9 }
10 }

浙公网安备 33010602011771号