C # determine whether a class is derived from a class or whether an interface is implemented,

Source: Internet
Author: User

C # determine whether a class is derived from a class or whether an interface is implemented,
Is and

The is keyword determines whether the object instance or expression result can be converted to a specified type. Basic Syntax:

expr is type

If the following conditions are met, the is statement is true:

  • Expr is an instance of the same type as type.
  • Expr is an instance of the type derived from type. In other words, the expr result can be converted to an instance of type.
  • Expr has the compile-time type of a base class of type, and expr also has the runtime type of type or derived from type. The variable type is defined in the Declaration during compilation. The runtime type of a variable is the instance type assigned to the variable.
  • Expr is an instance of the type that implements the type interface.

Code:

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   }}

The as operator is similar to the conversion operation. If the conversion fails, as returns null instead of an exception. Basic Syntax:

expr as type

Equivalent

expr is type ? (type)expr : (type)null

You can try conversion to determine the derivative relationship of the class based on whether the conversion is successful or not.
Refer:

  • Https://docs.microsoft.com/zh-cn/dotnet/csharp/language-reference/keywords/is
  • Https://docs.microsoft.com/zh-cn/dotnet/csharp/language-reference/keywords/as
Type. IsSubclassOf and Type. IsAssignableFrom

Type. IsSubclassOf determines whether the current Type is derived from the specified Type.

[ComVisibleAttribute(true)]public virtual bool IsSubclassOf(Type c)

True if the current Type is derived from c; otherwise false. If the current Type is equal to c, this method returns True.
However, the IsSubclassOf method cannot be used to determine whether an interface is derived from another interface or whether it is an interface implemented by a class.

Type. IsAssignableFrom determines whether the specified Type of instance can be allocated to the current Type of instance.

public virtual bool IsAssignableFrom(Type c)

True if any of the following conditions is met:

  • C. The current instance is of the same type.
  • C is derived directly or indirectly from the current instance. C. It inherits from the current instance. If c is derived directly from the current instance, c is indirectly derived from a series of Current instances of one or more current instances of the inherited class.
  • The current instance is a c-implemented interface.
  • C is a generic type parameter, and the current instance represents one of the constraints of c.

Code:

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   }}

You can use Type. IsSubclassOf to determine the class derivation and use Type. IsAssignableFrom to determine the class derivation and interface inheritance.
Refer:

  • Https://msdn.microsoft.com/zh-cn/library/office/system.type.issubclassof
  • Https://msdn.microsoft.com/zh-cn/library/office/system.type.isassignablefrom

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.