Comparing Nested object properties using C#
2014-02-20 00:00
176 查看
public static IList<string> GetDifferingProperties(object source, object target) { var sourceType = source.GetType(); var sourceProperties = sourceType.GetProperties(); var targetType = target.GetType(); var targetProperties = targetType.GetProperties(); var result = new List<string>(); foreach (var property in (from s in sourceProperties from t in targetProperties where s.Name == t.Name && s.PropertyType == t.PropertyType && !Equals(s.GetValue(source, null), t.GetValue(target, null)) select new { Source = s, Target = t })) { // it's up to you to decide how primitive is primitive enough if (IsPrimitive(property.Source.PropertyType)) { result.Add(property.Source.Name); } else { foreach (var subProperty in GetDifferingProperties( property.Source.GetValue(source, null), property.Target.GetValue(target, null))) { result.Add(property.Source.Name + "." + subProperty); } } } return result; } private static bool IsPrimitive(Type type) { return type == typeof(string) || type == typeof(int); }
相关文章推荐
- Comparing Object Values Using Hash Codes
- Object Serialization using C#
- 步步为营VS 2008 + .NET 3.5(3) - C# 3.0新特性之Automatic Properties(自动属性)、Object Initializers(对象初始化器)、Collection Initializers(集合初始化器)和Ext
- [Ramda] Declaratively Map Predicates to Object Properties Using Ramda where
- Business Object Validation Using Attributes in C#
- 步步为营VS 2008 + .NET 3.5(3) - C# 3.0新特性之Automatic Properties(自动属性)、Object Initializers(对象初始化器)、Collection Initializers(集合初始化器)和Extension Methods(扩展方法)
- Initializing nested object properties z
- [Ramda] Declaratively Map Data Transformations to Object Properties Using Ramda evolve
- How To Display Hierarchical Data by Using Nested Repeater Controls and Visual C# .NET(ZT)
- Object Serialization using C# - The Code Project - C# Programming
- C# .NET using ManagementObjectSearcher提示缺少引用怎么办
- 步步为营VS 2008 + .NET 3.5(3) - C# 3.0新特性之Automatic Properties(自动属性)、Object Initializers(对象初始化器)、Collection Initializers(集合初始化器)和Ext
- Using a modal .NET dialog to display AutoCAD object properties
- Using a modeless .NET dialog to display AutoCAD object properties
- C# Internet Explorer DOM using ObjectFromLresult
- Mysql : C# sample code using ObjectDataSources
- C# 3.0新特性之Automatic Properties(自动属性)、Object Initializers(对象初始化器)、Collection Initializers(集合初始化器)和Extension Methods(扩展方法)
- 3、步步为营VS 2008 + .NET 3.5(3) - C# 3.0新特性之Automatic Properties(自动属性)、Object Initializers(对象初始化器)、Collection Initializers(集合初始化器)和Extension Methods(扩展方法)
- VS 2008 + .NET 3.5 - C# 3.0新特性之Automatic Properties(自动属性)、Object Initializers(对象初始化器)、Collection Initializers(集合初始化器)和Extension Methods(扩展方法)
- Printing out your W2 Form using C# and .NET