Object - IsTypeOrInheritsOf

A T extension method that query if '@this' is type or inherits of.

Try it

public class Class1 { }
public class DerivedC1 : Class1 { }

public static void Main()
{
	DerivedC1 objDerivedC1 = new DerivedC1();
	Class1 objClass1 = new Class1();
	
	//C# Extension Method: Object - IsTypeOrInheritsOf
	Console.WriteLine("objDerivedC1 is type or inherits of Class1: {0}", objDerivedC1.IsTypeOrInheritsOf(typeof(Class1)));
	Console.WriteLine("objClass1 is type or inherits of Class1: {0}", objClass1.IsTypeOrInheritsOf(typeof(Class1)));
}

View Source
using System;

public static partial class Extensions
{
    /// <summary>
    ///     A T extension method that query if '@this' is type or inherits of.
    /// </summary>
    /// <typeparam name="T">Generic type parameter.</typeparam>
    /// <param name="this">The @this to act on.</param>
    /// <param name="type">The type.</param>
    /// <returns>true if type or inherits of, false if not.</returns>
    public static bool IsTypeOrInheritsOf<T>(this T @this, Type type)
    {
        Type objectType = @this.GetType();

        while (true)
        {
            if (objectType.Equals(type))
            {
                return true;
            }

            if ((objectType == objectType.BaseType) || (objectType.BaseType == null))
            {
                return false;
            }

            objectType = objectType.BaseType;
        }
    }
}