Check if a given item can be assigned to a specific type. - CSharp System

CSharp examples for System:Type

Description

Check if a given item can be assigned to a specific type.

Demo Code


using System.Globalization;
using System.Collections.Generic;
using System.Collections;
using System;/* w  w w. j  a  v  a2 s  .c  om*/

public class Main{
        /// <summary>
      /// Check if a given item can be assigned to a specific type.
      /// </summary>
      /// <param name="item"></param>
      /// <param name="type"></param>
      /// <returns></returns>
      public static bool IsCompatibleType (object item, Type type)
      {
         if (item == null || type == null)
            return false;
         return type.IsAssignableFrom (item.GetType ());
      }
        /// <summary>
      /// 
      /// </summary>
      /// <param name="item"></param>
      /// <param name="other"></param>
      /// <returns></returns>
      public static bool IsCompatibleType (object item, object other)
      {
         if (item == null || other == null)
            return false;
         return other.GetType ().IsAssignableFrom (item.GetType ());
      }
}

Related Tutorials