C# 中一些類關係的判定方法

来源:https://www.cnblogs.com/vaiyanzi/archive/2018/12/26/10179829.html
-Advertisement-
Play Games

1. IsAssignableFrom實例方法 判斷一個類或者介面是否繼承自另一個指定的類或者介面。 輸出結果: IDog was inherited from IAnimalDog was inherited from IAnimalDog was inherited from IDogCate ...


1.  IsAssignableFrom實例方法 判斷一個類或者介面是否繼承自另一個指定的類或者介面。

public interface IAnimal { }
public interface IDog : IAnimal { }
public class Dog : IDog { }
public class Cate : IAnimal { }
public class Parrot { }
    	
var iAnimalType = typeof(IAnimal);
var iDogType = typeof(IDog);
var dogType = typeof(Dog);
var cateType = typeof(Cate);
var parrotType = typeof(Parrot);

Console.WriteLine(iAnimalType.IsAssignableFrom(iDogType)
    ? $"{iDogType.Name} was inherited from {iAnimalType.Name}"
    : $"{iDogType.Name} was not inherited from {iAnimalType.Name}");

Console.WriteLine(iAnimalType.IsAssignableFrom(dogType)
    ? $"{dogType.Name} was inherited from {iAnimalType.Name}"
    : $"{dogType.Name} was not inherited from {iAnimalType.Name}");

Console.WriteLine(iDogType.IsAssignableFrom(dogType)
    ? $"{dogType.Name} was inherited from {iDogType.Name}"
    : $"{dogType.Name} was not inherited from {iDogType.Name}");

Console.WriteLine(iAnimalType.IsAssignableFrom(cateType)
    ? $"{cateType.Name} was inherited from {iAnimalType.Name}"
    : $"{cateType.Name} was not inherited from {iAnimalType.Name}");

Console.WriteLine(iAnimalType.IsAssignableFrom(parrotType)
    ? $"{parrotType.Name} inherited from {iAnimalType.Name}"
    : $"{parrotType.Name} not inherited from {iAnimalType.Name}");
Console.ReadKey();

輸出結果:

IDog was inherited from IAnimal
Dog was inherited from IAnimal
Dog was inherited from IDog
Cate was inherited from IAnimal
Parrot not inherited from IAnimal

2.IsInstanceOfType 判斷某個對象是否繼承自指定的類或者介面

Dog d=new Dog();
var result=typeof(IDog).IsInstanceOfType(d);
Console.WriteLine(result? $"Dog was inherited from IDog": $"Dog was not inherited from IDog");
Console.ReadKey();

輸出結果:

Dog was inherited from IDog

3.IsSubclassOf 判斷一個對象的類型是否繼承自指定的類,不能用於介面的判斷,這能用於判定類的關係

public interface IAnimal { }
public interface IDog : IAnimal { }
public class Dog : IDog { }
public class Husky : Dog { }
public class Cate : IAnimal { }
public class Parrot { }
Husky husky = new Husky();
var result = husky.GetType().IsSubclassOf(typeof(Dog));
Console.WriteLine(result ? $"Husky was inherited from Dog" : $"Husky was not inherited from Dog");

輸出結果:

Husky was inherited from Dog

這個方法不能用於介面,如果穿介面進去永遠返回的都是false

Dog dog = new Dog();
var dogResult = dog.GetType().IsSubclassOf(typeof(IDog));
Console.WriteLine(dogResult);

結果:

false


您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • 1. 簡單瞭解模塊 寫的每一個py文件都是一個模塊. 還有一些我們一直在使用的模塊 buildins 內置模塊. print, input random 主要是和隨機相關的內容 random() 隨機小數 uninform(a,b) 隨機小數 randint(a,b) 隨機整數 choice() 隨 ...
  • 2018-12-26 今天是我正式學習Python的第二天,也是我準備用博客來記錄我學習歷程的第一天。希望可以堅持下去,並且真正學到一些東西。 對字元串進行操作的方法: 1.對輸入字元是否是數字的判斷{str.isdecimal(),str.isdigit(),str.isnumeric()} 由上 ...
  • 利用線性基的合併,(直接暴力合併,複雜度62^2),當樹上路徑和來做,仿造java風格寫個類就好。。。(然後跑的巨慢,但是可以優化哈哈) cpp include using namespace std; const int N=2e4+7; / 我突然覺得,應該寫一個類。。 / struct Sha ...
  • 運行結果 ...
  • 本節將在第四節基礎上介紹如何實現IdentityServer4從資料庫獲取User進行驗證,並對Claim進行許可權設置。 一、新建Web API資源服務,命名為ResourceAPI (1)新建API項目,用來進行user的身份驗證服務。 (2)配置埠為5001 安裝Microsoft.Entit ...
  • 上節以對話形式,大概說了幾種客戶端授權模式的原理,這節重點介紹Hybrid模式在MVC下的使用。且為實現IdentityServer4從資料庫獲取User進行驗證,並對Claim進行許可權設置打下基礎(第五節介紹)。 本節內容比較多,且涉及一、二節的內容,如有不懂,可先熟悉一、二節知識。 一、新建授權 ...
  • C 中關於增強類功能的幾種方式 本文主要講解如何利用C 語言自身的特性來對一個類的功能進行豐富與增強,便於拓展現有項目的一些功能。 拓展方法 擴展方法 被定義為靜態方法,通過實例方法語法進行調用。方法的第一個參數指定該方法作用於哪個類型,並且該參數以 this 修飾符為首碼。僅當使用 using 指 ...
  • 使用System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile進行MD5加密時,會出現已過時 ...
一周排行
    -Advertisement-
    Play Games
  • 移動開發(一):使用.NET MAUI開發第一個安卓APP 對於工作多年的C#程式員來說,近來想嘗試開發一款安卓APP,考慮了很久最終選擇使用.NET MAUI這個微軟官方的框架來嘗試體驗開發安卓APP,畢竟是使用Visual Studio開發工具,使用起來也比較的順手,結合微軟官方的教程進行了安卓 ...
  • 前言 QuestPDF 是一個開源 .NET 庫,用於生成 PDF 文檔。使用了C# Fluent API方式可簡化開發、減少錯誤並提高工作效率。利用它可以輕鬆生成 PDF 報告、發票、導出文件等。 項目介紹 QuestPDF 是一個革命性的開源 .NET 庫,它徹底改變了我們生成 PDF 文檔的方 ...
  • 項目地址 項目後端地址: https://github.com/ZyPLJ/ZYTteeHole 項目前端頁面地址: ZyPLJ/TreeHoleVue (github.com) https://github.com/ZyPLJ/TreeHoleVue 目前項目測試訪問地址: http://tree ...
  • 話不多說,直接開乾 一.下載 1.官方鏈接下載: https://www.microsoft.com/zh-cn/sql-server/sql-server-downloads 2.在下載目錄中找到下麵這個小的安裝包 SQL2022-SSEI-Dev.exe,運行開始下載SQL server; 二. ...
  • 前言 隨著物聯網(IoT)技術的迅猛發展,MQTT(消息隊列遙測傳輸)協議憑藉其輕量級和高效性,已成為眾多物聯網應用的首選通信標準。 MQTTnet 作為一個高性能的 .NET 開源庫,為 .NET 平臺上的 MQTT 客戶端與伺服器開發提供了強大的支持。 本文將全面介紹 MQTTnet 的核心功能 ...
  • Serilog支持多種接收器用於日誌存儲,增強器用於添加屬性,LogContext管理動態屬性,支持多種輸出格式包括純文本、JSON及ExpressionTemplate。還提供了自定義格式化選項,適用於不同需求。 ...
  • 目錄簡介獲取 HTML 文檔解析 HTML 文檔測試參考文章 簡介 動態內容網站使用 JavaScript 腳本動態檢索和渲染數據,爬取信息時需要模擬瀏覽器行為,否則獲取到的源碼基本是空的。 本文使用的爬取步驟如下: 使用 Selenium 獲取渲染後的 HTML 文檔 使用 HtmlAgility ...
  • 1.前言 什麼是熱更新 游戲或者軟體更新時,無需重新下載客戶端進行安裝,而是在應用程式啟動的情況下,在內部進行資源或者代碼更新 Unity目前常用熱更新解決方案 HybridCLR,Xlua,ILRuntime等 Unity目前常用資源管理解決方案 AssetBundles,Addressable, ...
  • 本文章主要是在C# ASP.NET Core Web API框架實現向手機發送驗證碼簡訊功能。這裡我選擇是一個互億無線簡訊驗證碼平臺,其實像阿裡雲,騰訊雲上面也可以。 首先我們先去 互億無線 https://www.ihuyi.com/api/sms.html 去註冊一個賬號 註冊完成賬號後,它會送 ...
  • 通過以下方式可以高效,並保證數據同步的可靠性 1.API設計 使用RESTful設計,確保API端點明確,並使用適當的HTTP方法(如POST用於創建,PUT用於更新)。 設計清晰的請求和響應模型,以確保客戶端能夠理解預期格式。 2.數據驗證 在伺服器端進行嚴格的數據驗證,確保接收到的數據符合預期格 ...