1. 程式人生 > >C#基礎 Type IsSubclassOf 兩個類是否為繼承的關係

C#基礎 Type IsSubclassOf 兩個類是否為繼承的關係

慈心積善融學習,技術願為有情學。善心速造多好事,前人栽樹後乘涼。我今於此寫經驗,願見文者得啟發。

code

using System;

namespace ConsoleApp
{
    public class Person
    { }

    public class CSharper : Person
    { }

    class Program
    {
        static void Main(string[] args)
        {
            Type tpPerson = typeof(Person);
            Type
tpStudent = typeof(CSharper); // CSharper是Person的父類嗎? bool res = tpPerson.IsSubclassOf(tpStudent); Console.WriteLine(res); // Person是CSharper的父類嗎? res = tpStudent.IsSubclassOf(tpPerson); Console.WriteLine(res); Console.
ReadKey(); } } }

result

False
True

resource

感恩曾經幫助過 心少樸 的人。 C#優秀,值得學習。.net core具有跨平臺的能力,值得關注。 Console,WinForm,WPF,ASP.NET,Azure WebJob,WCF,Unity3d,UWP可以適當地瞭解。 注:此文是自學筆記所生,質量中下等,故要三思而後行。新手到此,不可照搬,應先研究其理象數,待能變通之時,自然跳出深坑。