C# オブジェクトのプロパティの詳細な説明
単体テストやデータ検証などのシナリオでは、オブジェクトのプロパティ値を比較する必要があることがよくあります。 C# には、この機能を実現するための複数の方法が用意されていますが、それぞれに長所と短所があります。
方法 1: リフレクションを使用する
リフレクション メカニズムにより、オブジェクトのプロパティを動的に検査し、その値を比較できます。以下はリフレクションベースの比較方法です:
<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>
方法 2: ジェネリックスと 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 中国語 Web サイトの他の関連記事を参照してください。