首頁 >後端開發 >C++ >如何在 C# 中比較物件屬性?

如何在 C# 中比較物件屬性?

Patricia Arquette
Patricia Arquette原創
2025-01-22 21:56:11385瀏覽

How to Compare Object Properties in C#?

C# 物件屬性比較詳解

在單元測試或資料驗證等場景中,比較物件屬性值的需求十分常見。 C# 提供多種方法實現此功能,各有優劣。

方法一:使用反射

反射機制允許動態檢查物件的屬性並比較其值。以下是一個基於反射的比較方法:

<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>

方法二:使用泛型和 LINQ

泛型和 LINQ 提供了更簡潔的比較方法:

<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>

注意事項

  • 效能: 反射比直接存取屬性效能較低。
  • 型別安全: 泛型方法確保型別安全,減少錯誤。
  • 可擴充性: 擴充方法方便整合到其他程式碼中。
  • 限制: 兩種方法都可能無法處理包含複雜物件或循環參考的屬性。

總結

方法選擇取決於具體需求。效能要求高的場景,建議直接存取屬性;需要類型安全性和可擴展性的場景,泛型 LINQ 方法更合適。

以上是如何在 C# 中比較物件屬性?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn