栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

在C#中比较对象属性

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

在C#中比较对象属性

我一直在寻找一段代码,这些代码可以做类似的事情来帮助编写单元测试。这是我最终使用的内容。

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);        foreach (System.Reflection.PropertyInfo pi in type.GetProperties(System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance))        {if (!ignoreList.Contains(pi.Name)){   object selfValue = type.GetProperty(pi.Name).GetValue(self, null);   object toValue = type.GetProperty(pi.Name).GetValue(to, null);   if (selfValue != toValue && (selfValue == null || !selfValue.Equals(toValue)))   {      return false;   }}        }        return true;     }     return self == to;  }

编辑:

与上面相同的代码,但使用LINQ和Extension方法:

public static bool PublicInstancePropertiesEqual<T>(this T self, T to, params string[] ignore) where T : class{    if (self != null && to != null)    {        var type = typeof(T);        var 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   {      /// <summary>      /// Determine whether a type is simple (String, Decimal, DateTime, etc)       /// or complex (i.e. custom class with public properties and methods).      /// </summary>      /// <see cref="http://stackoverflow.com/questions/2442534/how-to-test-if-type-is-primitive"/>      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    (       "Input MemberInfo must be if type EventInfo, FieldInfo, MethodInfo, or PropertyInfo"    );         }      }   }


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/465643.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号