ปรับ api เข้ากับ โครงสร้างใหม่
This commit is contained in:
parent
8851ac6db0
commit
811fa781b4
20 changed files with 1397 additions and 73 deletions
51
Extensions/ObjectExtension.cs
Normal file
51
Extensions/ObjectExtension.cs
Normal file
|
|
@ -0,0 +1,51 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace BMA.EHR.Recurit.Exam.Service.Extensions
|
||||
{
|
||||
public static class ObjectExtension
|
||||
{
|
||||
public static bool DeepCompare(this object obj, object another)
|
||||
{
|
||||
if (ReferenceEquals(obj, another)) return true;
|
||||
if ((obj == null) || (another == null)) return false;
|
||||
//Compare two object's class, return false if they are difference
|
||||
if (obj.GetType() != another.GetType()) return false;
|
||||
|
||||
var result = true;
|
||||
//Get all properties of obj
|
||||
//And compare each other
|
||||
foreach (var property in obj.GetType().GetProperties())
|
||||
{
|
||||
var objValue = property.GetValue(obj);
|
||||
var anotherValue = property.GetValue(another);
|
||||
if (!objValue.Equals(anotherValue)) result = false;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
public static bool Compare(this object obj, object another)
|
||||
{
|
||||
if (ReferenceEquals(obj, another)) return true;
|
||||
if ((obj == null) || (another == null)) return false;
|
||||
if (obj.GetType() != another.GetType()) return false;
|
||||
|
||||
//properties: int, double, DateTime, etc, not class
|
||||
if (!obj.GetType().IsClass) return obj.Equals(another);
|
||||
|
||||
var result = true;
|
||||
foreach (var property in obj.GetType().GetProperties())
|
||||
{
|
||||
var objValue = property.GetValue(obj);
|
||||
var anotherValue = property.GetValue(another);
|
||||
//Recursion
|
||||
if (!objValue.DeepCompare(anotherValue)) result = false;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue