C# Object Type Comparison

前端 未结 4 1615
小蘑菇
小蘑菇 2021-01-30 02:04

How can I compare the types of two objects declared as type.

I want to know if two objects are of the same type or from the same base class.

Any help is apprecia

4条回答
  •  暖寄归人
    2021-01-30 02:46

    I tried out the following with a hierarchy using both interfaces and concrete classes. It walks up the base class chain for one of the types till it reaches "object" at which we check if the current destination type is assignable to the source type. We also check if the types have a common interface. if they do then they 'AreSame'

    Hope this helps.

     public interface IUser
    {
         int ID { get; set; }
         string Name { get; set; }
    }
    
    public class NetworkUser : IUser
    {
        public int ID
        {
            get;
            set;
        }
    
        public string Name
        {
            get;
            set;
        }
    }
    
    public class Associate : NetworkUser,IUser
    {
        #region IUser Members
    
        public int ID
        {
            get;
            set;
        }
    
        public string Name
        {
            get;
            set;
        }
    
        #endregion
    }
    
    public class Manager : NetworkUser,IUser
    {
        #region IUser Members
    
        public int ID
        {
            get;
            set;
        }
    
        public string Name
        {
            get;
            set;
        }
    
        #endregion
    }
    
    
    public class Program
    {
    
        public static bool AreSame(Type sourceType, Type destinationType)
        {
            if (sourceType == null || destinationType == null)
            {
                return false;
            }
    
            if (sourceType == destinationType)
            {
                return true;
            }
    
            //walk up the inheritance chain till we reach 'object' at which point check if 
        //the current destination type is assignable from the source type      
        Type tempDestinationType = destinationType;
            while (tempDestinationType.BaseType != typeof(object))
            {
                tempDestinationType = tempDestinationType.BaseType;
            }
            if( tempDestinationType.IsAssignableFrom(sourceType))
            {
                return true;
            }
    
            var query = from d in destinationType.GetInterfaces() join s in sourceType.GetInterfaces()
                        on d.Name equals s.Name
                        select s;
            //if the results of the query are not empty then we have a common interface , so return true 
        if (query != Enumerable.Empty())
            {
                return true;
            }
            return false;            
        }
    
        public static void Main(string[] args)
        {
    
            AreSame(new Manager().GetType(), new Associate().GetType());
        }
    }
    

提交回复
热议问题