C#_判断2个对象的值是否相等

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;

namespace ConsoleApplication1
{

    class Person
    {

        public Person()
        {
            list = new HashSet<int>();
        }
        public int age { set; get; }
        public string name { set; get; }

        public ICollection<int> list { set; get; }

        public override int GetHashCode()
        {
        int prime = 31;
        int result = 1;
        result = prime * result + ((list == null) ? 0 : list.GetHashCode());
        result = prime * result + age;
        result = prime * result + ((name == null) ? 0 : name.GetHashCode());
        return result;
        }

        public override bool Equals(object obj)
        {
            if (this == obj)
                return true;
            if (obj == null)
                return false;
            if (this.GetType() != obj.GetType())
                return false;
            Person other = (Person)obj;
            if (list == null)
            {
                if (other.list != null)
                    return false;
            }
            //else if (!list.Equals(other.list))
            //    return false;

            else if (!(list.Except(other.list).Count().Equals(0) && other.list.Except(list).Count().Equals(0)))
                return false;
            if (age != other.age)
                return false;
            if (name == null)
            {
                if (other.name != null)
                    return false;
            }
            else if (!name.Equals(other.name))
                return false;
            return true;
        }
    }
    class Program
    {
        static void Main(string[] args)
        {

            //HashSet<int> s1 = new HashSet<int>();
            //HashSet<int> s2 = new HashSet<int>();
            //s1.Add(1);
            //s1.Add(2);
            //s1.Add(3);
            //s2.Add(1);
            //s2.Add(2);
           
            //var list3 = s2.Except(s1).ToList();

            //Console.WriteLine(list3.Count().Equals(0));

            //foreach (var i in list3)
            //{ 
            //Console.WriteLine(i.ToString());
            //}
            Person p1 = new Person();

            Person p2 = new Person();

            p1.name = "aaa";
            p1.age = 20;
            p1.list.Add(2);
            p1.list.Add(1);
           
     
            p2.name = "aaa";
            p2.age = 20;
            p2.list.Add(1);
            p2.list.Add(2);
          
            Console.WriteLine(p1.Equals(p2));

            //var t = p1.GetType();
            //var p1Model = Activator.CreateInstance(t);
            
            //var pros = t.GetProperties();

            //int total = pros.Count();
            //Console.WriteLine("total: "+total);
            //var t2 = p2.GetType();
            //var p2Model = Activator.CreateInstance(t2);
            //var pros2 = t2.GetProperties();

 

            //bool flag = false;

            //for (int i = 0; i < total; i++)
            //{
            //    Console.WriteLine(pros[i].GetValue(p1, null) + "---" + pros2[i].GetValue(p2, null));
            //    if (pros[i].GetValue(p1,null).ToString() != pros2[i].GetValue(p2,null).ToString())
            //    {
            //        Console.WriteLine("In if"+pros[i].GetValue(p1, null) + "---" + pros2[i].GetValue(p2, null));   
            //        flag = true;
            //    }
            //}
            //Console.WriteLine(flag);



            //Person p1 = new Person();

            //Person p2 = new Person();

            //p1.name = "aaa";
            //p1.age = 21;

            //p2.name = "aaa";
            //p2.age = 21;

            //Console.WriteLine(p1.Equals(p2));

            //var p1Type = p1.GetType();
            //var p2Type = p2.GetType();

            //var p1Properties = p1Type.GetProperties();
            //var p2Properties = p2Type.GetProperties();

            //var type = typeof(Person);



            //var result = p1Properties.All(p => p2Properties.Where(q => q.Name == p.Name).FirstOrDefault().GetValue(p2).Equals(p.GetValue(p1)));
            //Console.WriteLine(result);
             Console.ReadKey();

        }
    }
}
原文地址:https://www.cnblogs.com/MarchThree/p/3857521.html