我需要特定类型的变量,它们的类是:
public class User
{
public int? Id { get; set; }
public string Password { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string Email { get; set; }
public List<AccessibleSite> Sites { get; set; }
}
public class AccessibleSite
{
public int Id { get; set; }
public string Name { get; set; }
public List<Role> Roles { get; set; }
public bool IsChecked { get; set; }
}
public class Role
{
public int Id { get; set; }
public string Name { get; set; }
public bool IsChecked { get; set; }
public string UserFriendlyName { get; set; }
}
用户可以拥有多个AccessibleSite
,并且每个网站都有与之关联的Role
列表。
User
类型有两个对象。我需要找到两个列表之间的区别。
到目前为止,我编写的代码只是正确地比较非List对象。
static class extentions
{
public static List<Variance> DetailedCompare<T>(this T val1, T val2)
{
List<Variance> variances = new List<Variance>();
List<FieldInfo> fi = val1.GetType().GetRuntimeFields().ToList<FieldInfo>();
foreach (FieldInfo f in fi)
{
Variance v = new Variance();
v.Prop = f.Name;
v.valA = f.GetValue(val1);
v.valB = f.GetValue(val2);
if (v.valA is System.Collections.IList && v.valA.GetType().IsGenericType)
{
foreach (var listItem in v.valA.DetailedCompare(v.valB))
{
variances.Add(listItem);
}
}
else
{
if ((v.valA != null && v.valB != null) && !v.valA.Equals(v.valB))
variances.Add(v);
}
}
return variances;
}
class Variance
{
public string Prop { get; set; }
public object valA { get; set; }
public object valB { get; set; }
public string ChangedFromText
{
get
{
return "value of " + Prop + " has changed from " + valA + " to " + valB;
}
}
列表中的项目需要根据ID进行比较。如果内容已经更改,则需要进行比较。
我查看了以下solution但是需要我创建两个不同的类,每个类用于AccessibleSite和Roles。 我们有什么方法可以只使用一个解决方案来编写它。递归可能吗? TIA
答案 0 :(得分:1)
这样的解决方案非常复杂。
这是我未经测试的解决方案。我会留给你测试和调整它:
此class属性用于指定应将类中的哪个字段或属性视为类的实例的Id值。
[AttributeUsage(AttributeTargets.Class)]
public class IdPropertyAttribute : Attribute
{
public string IdProperty { get; private set; }
public IdPropertyAttribute(string idProperty) { this.IdProperty = idProperty; }
}
TExtensions类包含两个扩展方法和一些支持方法,用于反映和分析两个对象图之间的值。
public static class TExtensions
{
public static Variance DetailedCompare<T>(this T val1, T val2)
{
return val1.DetailedCompare(val2, null);
}
public static Variance DetailedCompare<T>(this T val1, T val2, string fieldName)
{
return typeof(IEnumerable).IsAssignableFrom(typeof(T))
? TExtensions.enumerableCompare(val1, val2, fieldName)
: typeof(T).IsPrimitive
? TExtensions.valueCompare(val1, val2, fieldName)
: TExtensions.objectCompare(val1, val2, fieldName);
}
private static Variance valueCompare<T>(T val1, T val2, string fieldName)
{
return val1 != null && val2 != null && !val1.Equals(val2)
? new Variance.ValueVariance<T>() { Prop = fieldName??"<root>", valA = val1, valB = val2 }
: null;
}
private static Variance objectCompare<T>(T val1, T val2, string fieldName)
{
var variance = new Variance.EnumerableVariance() {Prop = fieldName};
List<FieldInfo> fi = val1.GetType().GetRuntimeFields().ToList<FieldInfo>();
foreach (FieldInfo f in fi)
{
var subVariance = f.GetValue(val1).DetailedCompare(f.GetValue(val2), f.Name);
if (subVariance != null) variance.variances.Add(subVariance);
}
return variance;
}
private static Variance enumerableCompare<T>(T val1, T val2, string fieldName)
{
return typeof(IEnumerable<>).IsAssignableFrom(typeof(T))
? TExtensions.homogeneousEnumerableCompare<T>(val1, val2, fieldName)
: TExtensions.heterogeneousEnumerableCompare<T>(val1, val2, fieldName);
}
private static Variance heterogeneousEnumerableCompare<T>(T val1, T val2, string fieldName)
{
throw new NotImplementedException();
}
private static Variance homogeneousEnumerableCompare<T>(T val1, T val2, string fieldName)
{
var subType = typeof(T).GetGenericArguments()[0];
return typeof(KeyValuePair<,>).IsAssignableFrom(subType)
? TExtensions.homogeneousKeyValueEnumerableCompare(subType, val1, val2, fieldName)
: subType.IsPrimitive
? homogeneousValueEnumerableCompare(subType, val1, val2, fieldName)
: homogeneousObjectEnumerableCompare(subType, val1, val2, fieldName);
}
private static Variance homogeneousObjectEnumerableCompare<T>(Type subType, T val1, T val2, string fieldName)
{
var subMethod = typeof(TExtensions)
.GetMethod("typedHomogeneousObjectEnumerableCompare", BindingFlags.Static, null, new []{typeof(T), typeof(T), typeof(String)}, null)
.MakeGenericMethod(new []{typeof(T), subType});
return (Variance) subMethod.Invoke(null, new object[] {val1, val2, fieldName});
}
private static Variance typedHomogeneousObjectEnumerableCompare<T, TSubType>(T val1, T val2, string fieldName)
{
var idAttribute = typeof(TSubType).GetCustomAttribute<IdPropertyAttribute>(true);
return idAttribute == null
? TExtensions.keyLessTypedHomogeneousObjectEnumerableCompare<T, TSubType>(val1, val2, fieldName)
: TExtensions.keyedTypedHomogeneousObjectEnumerableCompare<T, TSubType>(idAttribute, val1, val2, fieldName);
}
private static Variance keyLessTypedHomogeneousObjectEnumerableCompare<T, TSubType>(T val1, T val2, string fieldName)
{
var list1 = (IEnumerable<TSubType>) val1;
var list2 = (IEnumerable<TSubType>) val2;
return list1.Count() != list2.Count()
? new Variance.KeylessObjectEnumerableVariance() { Prop = fieldName, listACount = list1.Count(), listBCount = list2.Count() }
: null;
}
private static Variance keyedTypedHomogeneousObjectEnumerableCompare<T, TSubType>(IdPropertyAttribute idAttribute, T val1, T val2, string fieldName)
{
var idMember = typeof(TSubType).GetMember(idAttribute.IdProperty).FirstOrDefault();
if (idMember == null) throw new IdMemberNotFoundException(idAttribute.IdProperty, typeof(TSubType).FullName);
var subMethod = typeof(TExtensions)
.GetMethod("subTypedKeyedTypedHomogeneousObjectEnumerableCompare", BindingFlags.Static, null, new []{typeof(T), typeof(T), typeof(String)}, null)
.MakeGenericMethod(new []{typeof(T), typeof(TSubType), (idMember is PropertyInfo ? ((PropertyInfo) idMember).PropertyType : ((FieldInfo) idMember).FieldType)});
return (Variance) subMethod.Invoke(null, new object[] {val1, val2, fieldName, idMember});
}
private static Variance subTypedKeyedTypedHomogeneousObjectEnumerableCompare<T, TSubType, TSubTypeKey>(T val1, T val2, string fieldName, MemberInfo idMember)
{
return subTypedKeyedTypedHomogeneousObjectEnumerableCompareWithKeyFunc<T, TSubType, TSubTypeKey>
(
val1,
val2,
fieldName,
idMember is PropertyInfo
? new Func<TSubType, TSubTypeKey>(item=>(TSubTypeKey) ((PropertyInfo) idMember).GetValue(item))
: item=>(TSubTypeKey) ((FieldInfo) idMember).GetValue(item)
);
}
private static Variance subTypedKeyedTypedHomogeneousObjectEnumerableCompareWithKeyFunc<T, TSubType, TSubTypeKey>(T val1, T val2, string fieldName, Func<TSubType, TSubTypeKey> getKey)
{
var set1 = ((IEnumerable<TSubType>) val1).ToDictionary(a=>getKey(a));
var set2 = ((IEnumerable<TSubType>) val2).ToDictionary(a=>getKey(a));
return TExtensions.DictionaryCompare<TSubTypeKey, TSubType>(set1, set2, fieldName);
}
private static Variance homogeneousValueEnumerableCompare<T>(Type subType, T val1, T val2, string fieldName)
{
var subMethod = typeof(TExtensions)
.GetMethod("typedHomogeneousValueEnumerableCompare", BindingFlags.Static, null, new []{typeof(T), typeof(T), typeof(String)}, null)
.MakeGenericMethod(new []{typeof(T), subType});
return (Variance) subMethod.Invoke(null, new object[] {val1, val2, fieldName});
}
private static Variance typedHomogeneousValueEnumerableCompare<T, TSubType>(T val1, T val2, string fieldName)
{
var variance = new Variance.EnumerableVariance();
var list1 = ((IEnumerable<TSubType>) val1).ToList();
var list2 = ((IEnumerable<TSubType>) val1).ToList();
foreach(var item in list1) if (!list2.Contains(item)) variance.variances.Add(new Variance.ValueRemovedVariance<TSubType>() {Prop = fieldName, value = item});
foreach(var item in list2) if (!list1.Contains(item)) variance.variances.Add(new Variance.ValueAddedVariance<TSubType>() {Prop = fieldName, value = item});
return variance;
}
private static Variance homogeneousKeyValueEnumerableCompare<T>(Type subType, T val1, T val2, string fieldName)
{
var keyType = subType.GetGenericArguments()[0];
var valueType = subType.GetGenericArguments()[1];
var subMethod = typeof(TExtensions)
.GetMethod("typedHomogeneousKeyValueEnumerableCompare", BindingFlags.Static, null, new []{typeof(T), typeof(T), typeof(String)}, null)
.MakeGenericMethod(new []{typeof(T), keyType, valueType});
return (Variance) subMethod.Invoke(null, new object[] {val1, val2, fieldName});
}
private static Variance typedHomogeneousKeyValueEnumerableCompare<T, TSubTypeKey, TSubTypeValue>(T val1, T val2, string fieldName)
{
var set1 = ((IEnumerable<KeyValuePair<TSubTypeKey, TSubTypeValue>>) val1).ToDictionary(a=>a.Key, a=>a.Value);
var set2 = ((IEnumerable<KeyValuePair<TSubTypeKey, TSubTypeValue>>) val2).ToDictionary(a=>a.Key, a=>a.Value);
return TExtensions.DictionaryCompare<TSubTypeKey, TSubTypeValue>(set1, set2, fieldName);
}
private static Variance DictionaryCompare<TSubTypeKey, TSubTypeValue>(Dictionary<TSubTypeKey, TSubTypeValue> set1, Dictionary<TSubTypeKey, TSubTypeValue> set2, string fieldName)
{
var variance = new Variance.EnumerableVariance();
foreach(var key in set1.Keys)
{
var subVariance = !set2.ContainsKey(key)
? new Variance.KeyedObjectRemovedVariance<TSubTypeKey>() {Prop = fieldName, key = key}
: set1[key].DetailedCompare(set2[key], fieldName + "[" + key.ToString() + "]");
if (subVariance != null) variance.variances.Add(subVariance);
}
foreach(var key in set2.Keys) if (!set1.ContainsKey(key)) variance.variances.Add(new Variance.KeyedObjectRemovedVariance<TSubTypeKey>() {Prop = fieldName, key = key});
return variance;
}
}
需要此异常来指示已使用IdAttribute修饰了类,但在分析期间未在类中找到指定的字段或属性。
public class IdMemberNotFoundException : ApplicationException
{
public IdMemberNotFoundException(string memberName, string typeName) : base(memberName + " was not found in type " + typeName + ".") {}
}
方差类已经分解为一个基类,它有几个子类来表示可能出现的各种类型的差异。
public abstract class Variance
{
public string Prop { get; set; }
public string GetChangedFromText() { return this.GetChangedFromText(null); }
protected abstract string GetChangedFromText(string parent);
public class ValueVariance<T> : Variance
{
public object valA { get; set; }
public object valB { get; set; }
protected override string GetChangedFromText(string parent)
{
return "value of " + (parent??"<root>") + "." + this.Prop + " has changed from " + this.valA + " to " + this.valB;
}
}
public class EnumerableVariance : Variance
{
public List<Variance> variances = new List<Variance>();
protected override string GetChangedFromText(string parent)
{
StringBuilder returnString = new StringBuilder();
foreach(var variance in this.variances)
{
returnString.Append(variance.GetChangedFromText(this.Prop));
}
return returnString.ToString();
}
}
public class KeylessObjectEnumerableVariance : Variance
{
public int listACount;
public int listBCount;
protected override string GetChangedFromText(string parent)
{
return "count of keyless items " + (parent??"<root>") + "." + this.Prop + " has changed from " + this.listACount.ToString() + " to " + this.listBCount.ToString();
}
}
public class ValueRemovedVariance<T> : Variance
{
public T value;
protected override string GetChangedFromText(string parent)
{
return "value " + this.value.ToString() + " of " + (parent??"<root>") + "." + this.Prop + " was removed.";
}
}
public class ValueAddedVariance<T> : Variance
{
public T value;
protected override string GetChangedFromText(string parent)
{
return "value " + this.value.ToString() + " of " + (parent??"<root>") + "." + this.Prop + " was added.";
}
}
public class KeyedObjectRemovedVariance<T> : Variance
{
public T key;
protected override string GetChangedFromText(string parent)
{
return "key " + this.key.ToString() + " of " + (parent??"<root>") + "." + this.Prop + " was removed.";
}
}
public class KeyedObjectAddedVariance<T> : Variance
{
public T key;
protected override string GetChangedFromText(string parent)
{
return "key " + this.key.ToString() + " of " + (parent??"<root>") + "." + this.Prop + " was added.";
}
}
}
您的差异可能包括:
上面的代码应该有望解决这些差异。您可能会发现其他情况。希望这给你一个很好的起点。