Object - IsTypeOf
A T extension method that query if '@this' is type of.
public class Class1 { } public class DerivedC1 : Class1 { } public static void Main() { DerivedC1 objDerivedC1 = new DerivedC1(); Class1 objClass1 = new Class1(); //C# Extension Method: Object - IsTypeOf Console.WriteLine("objDerivedC1 is type of Class1: {0}", objDerivedC1.IsTypeOf(typeof(Class1))); Console.WriteLine("objClass1 is type of Class1: {0}", objClass1.IsTypeOf(typeof(Class1))); }
View Source
using System; public static partial class Extensions { /// <summary> /// A T extension method that query if '@this' is type 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 of, false if not.</returns> public static bool IsTypeOf<T>(this T @this, Type type) { return @this.GetType() == type; } }