A detailed explanation of C# object properties
In scenarios such as unit testing or data verification, the need to compare object property values is very common. C# provides multiple methods to achieve this functionality, each with its own pros and cons.
Method 1: Use reflection
The reflection mechanism allows dynamic inspection of properties of objects and comparison of their values. Here is a reflection-based comparison method:
<code class="language-csharp">public bool PropertiesEqual(object comparisonObject) { Type sourceType = this.GetType(); Type destinationType = comparisonObject.GetType(); if (sourceType == destinationType) { PropertyInfo[] sourceProperties = sourceType.GetProperties(); foreach (PropertyInfo pi in sourceProperties) { if ((sourceType.GetProperty(pi.Name).GetValue(this, null) == null && destinationType.GetProperty(pi.Name).GetValue(comparisonObject, null) == null)) { // 两个属性都为 null,无需比较 } else if (!(sourceType.GetProperty(pi.Name).GetValue(this, null).ToString() == destinationType.GetProperty(pi.Name).GetValue(comparisonObject, null).ToString())) { // 只要一个属性值不同,则返回 false return false; } } } else { throw new ArgumentException("比较对象必须为同一类型。", "comparisonObject"); } return true; }</code>
Method 2: Using generics and LINQ
Generics and LINQ provide a simpler comparison method:
<code class="language-csharp">public static bool PublicInstancePropertiesEqual<T>(T self, T to, params string[] ignore) where T : class { if (self != null && to != null) { Type type = typeof(T); List<string> ignoreList = new List<string>(ignore); var unequalProperties = from pi in type.GetProperties(BindingFlags.Public | BindingFlags.Instance) where !ignoreList.Contains(pi.Name) && pi.GetUnderlyingType().IsSimpleType() && pi.GetIndexParameters().Length == 0 let selfValue = type.GetProperty(pi.Name).GetValue(self, null) let toValue = type.GetProperty(pi.Name).GetValue(to, null) where selfValue != toValue && (selfValue == null || !selfValue.Equals(toValue)) select selfValue; return !unequalProperties.Any(); } return self == to; } public static class TypeExtensions { public static bool IsSimpleType(this Type type) { return type.IsValueType || type.IsPrimitive || new[] { typeof(String), typeof(Decimal), typeof(DateTime), typeof(DateTimeOffset), typeof(TimeSpan), typeof(Guid) }.Contains(type) || (Convert.GetTypeCode(type) != TypeCode.Object); } public static Type GetUnderlyingType(this MemberInfo member) { switch (member.MemberType) { case MemberTypes.Event: return ((EventInfo)member).EventHandlerType; case MemberTypes.Field: return ((FieldInfo)member).FieldType; case MemberTypes.Method: return ((MethodInfo)member).ReturnType; case MemberTypes.Property: return ((PropertyInfo)member).PropertyType; default: throw new ArgumentException("输入的 MemberInfo 必须是 EventInfo、FieldInfo、MethodInfo 或 PropertyInfo 类型"); } } }</code>
Notes
Summary
Method selection depends on specific needs. For scenarios with high performance requirements, it is recommended to access properties directly; for scenarios that require type safety and scalability, the generic LINQ method is more suitable.
The above is the detailed content of How to Compare Object Properties in C#?. For more information, please follow other related articles on the PHP Chinese website!