Is object a type : Type : Reflection C# Examples


C# Examples » Reflection » Type »

 

Is object a type









    
using  System;
using  System.IO;

class  MainClass
{
        public  static  void  Main()  
        {
                Object  someObject  =  new  StringReader("This  is  a  StringReader");

                
                if  (IsType(someObject,  "System.IO.TextReader"))  
                {
                        Console.WriteLine("GetType:  someObject  is  a  TextReader");
                }
        }
        public  static  bool  IsType(object  obj,  string  type)  
        {
                Type  t  =  Type.GetType(type,  true,  true);

                return  t  ==  obj.GetType()  ||  obj.GetType().IsSubclassOf(t);
        }
        
}
    
   
  
   



Output

GetType: someObject is a TextReader


HTML code for linking to this page:

Follow Navioo On Twitter

C# Examples

 Navioo Reflection
» Type