现在有个名叫obj的对象,我要判断它是否类型Class1,但不想用类名的字串来比较,如何办?
if (obj is Class1) ...;
//这种比较会有问题,如果obj是Class2,而Class2的基类是Class1,上面也会通过

解决方案 »

  1.   

    if (obj.GetType == typeof(Class1))
    {
      
    }
      

  2.   


    if (obj!=null && obj.GetType() == typeof(Class1))
    {
        //同一类型 
    }
    else
    {
        //类型不同
    }
      

  3.   

    if (obj.GetType() == typeof(Class))
    {}
      

  4.   

    if (obj!=null && obj.GetType() == typeof(Class1))
      

  5.   

    objcet obj;
    YourType convertobj = obj as YourType;
    if(convertojb != null)
    {
    //这样可以判断可不可以转换
    }
      

  6.   

    用as应该不可以,因为as和楼主说的is有相似的问题.