is关键字可以确定对象实例或表达式结果是否可转换为指定类型。基本语法:
class="brush:csharp;gutter:true;">expr is type
如果满足以下条件,则 is 语句为 true:
代码:
using System;
public class Class1 : IFormatProvider
{
public object GetFormat(Type t)
{
if (t.Equals(this.GetType()))
return this;
return null;
}
}
public class Class2 : Class1
{
public int Value { get; set; }
}
public class Example
{
public static void Main()
{
var cl1 = new Class1();
Console.WriteLine(cl1 is IFormatProvider); //True
Console.WriteLine(cl1 is Object); //True
Console.WriteLine(cl1 is Class1); //True
Console.WriteLine(cl1 is Class2); //True
Console.WriteLine();
var cl2 = new Class2();
Console.WriteLine(cl2 is IFormatProvider); //True
Console.WriteLine(cl2 is Class2); //True
Console.WriteLine(cl2 is Class1); //True
Console.WriteLine();
Class1 cl = cl2;
Console.WriteLine(cl is Class1); //True
Console.WriteLine(cl is Class2); //True
}
}
as运算符类似于转换运算。如果无法进行转换,则 as 会返回 null,而不是引发异常。基本语法:
expr as type
等效
expr is type ? (type)expr : (type)null
可以尝试转换,根据转换的成功与否判断类的派生关系。
参考至:
Type.IsSubclassOf 确定当前 Type 是否派生自指定的 Type。
[ComVisibleAttribute(true)] public virtual bool IsSubclassOf( Type c )
如果当前 Type 派生于 c,则为 True;否则为 false。 如果 当前Type 和 c 相等,此方法也返回 True。
但是IsSubclassOf方法不能用于确定接口是否派生自另一个接口,或是否类实现的接口。
Type.IsAssignableFrom 确定指定类型的实例是否可以分配给当前类型的实例。
public virtual bool IsAssignableFrom( Type c )
如果满足下列任一条件,则为 true:
代码:
using System;
public interface IInterface
{
void Display();
}
public class Class1 { }
public class Implementation :Class1, IInterface
{
public void Display()
{
Console.WriteLine("The implementation...");
}
}
public class Example
{
public static void Main()
{
Console.WriteLine("Implementation is a subclass of IInterface: {0}",
typeof(Implementation).IsSubclassOf(typeof(IInterface))); //False
Console.WriteLine("Implementation subclass of Class1: {0}",
typeof(Implementation).IsSubclassOf(typeof(Class1))); //True
Console.WriteLine("IInterface is assignable from Implementation: {0}",
typeof(IInterface).IsAssignableFrom(typeof(Implementation))); //True
Console.WriteLine("Class1 is assignable from Implementation: {0}",
typeof(Class1).IsAssignableFrom(typeof(Implementation))); //True
}
}
可以使用 Type.IsSubclassOf 判断类的派生, 使用 Type.IsAssignableFrom 判断类的派生和接口继承。
参考至: