C#判斷某個類是否派生某個類或是否實現了某個接口


is和as

is關鍵字可以確定對象實例或表達式結果是否可轉換為指定類型。基本語法:

expr is type

如果滿足以下條件,則 is 語句為 true:

  • expr 是與 type 具有相同類型的一個實例。
  • expr 是派生自 type 的類型的一個實例。 換言之,expr 結果可以向上轉換為 type 的一個實例。
  • expr 具有屬於 type 的一個基類的編譯時類型,expr 還具有屬於 type 或派生自 type 的運行時類型。 變量的編譯時類型是其聲明中定義的變量類型。 變量的運行時類型是分配給該變量的實例類型。
  • expr 是實現 type 接口的類型的一個實例。

代碼:

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.IsAssignableFrom

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:

  • c 且當前實例表示相同類型。
  • c 是從當前實例直接或間接派生的。 c 它繼承自的當前實例; 如果直接從當前實例派生 c 如果它繼承自一個或多個從繼承類的當前實例的一系列的當前實例中間接派生。
  • 當前實例是一個 c 實現的接口。
  • c 是一個泛型類型參數,並且當前實例表示 c 的約束之一。

代碼:

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 判斷類的派生和接口繼承。
參考至:


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM