public override bool Equals(Object obj) { PaoObject personObj = obj as PaoObject; if (personObj == null) { return(false); } else if (Key != this) { return(Key.Equals(personObj.Key)); } else { return(base.Equals(obj)); } }
/// <summary> /// 用键值对的方式来设置属性 /// </summary> /// <param name="originalObject">对象</param> /// <param name="path">属性名称</param> /// <param name="value">值</param> public static void SetPropertyValueByPath(this object originalObject, string path, object value) { originalObject.CheckNotNull("原始对象不能为空"); Debug.Assert(!path.IsNullOrEmpty()); // 通过"."来分割属性 string[] properties = path.Split(new char[] { '.' }, 2); Type type = originalObject.GetType(); string propertyName = properties[0]; object childObject = null; if (type.IsDerivedFrom(typeof(IDictionary))) { if (properties.Length == 1) { // 设置字典值 ((IDictionary)originalObject)[propertyName] = value; } else { childObject = ((IDictionary)originalObject)[propertyName]; } } else if (originalObject is IList && type.IsGenericType && type.GetGenericArguments()[0].IsDerivedFrom(typeof(PaoObject))) { // 资产列表 IList list = originalObject as IList; for (int i = 0; i < list.Count; i++) { PaoObject asset = list[i] as PaoObject; if (asset != null && asset.ID == propertyName) { if (properties.Length == 1) { // 设置值 list[i] = value; } else { childObject = list[i]; } } } } else if (type.IsDerivedFrom(typeof(IList))) { // 设置列表元素值 int index = Convert.ToInt32(propertyName); if (properties.Length == 1) { // 设置值 ((IList)originalObject)[index] = value; } else { childObject = ((IList)originalObject)[index]; } } else { // 设置属性值 PropertyInfo propertyInfo = type.GetProperty(propertyName, BindingFlags.Public | BindingFlags.GetProperty | BindingFlags.SetProperty | BindingFlags.Instance); if (propertyInfo != null) { if (properties.Length == 1) { // 如果是最后一个".",则设置属性 propertyInfo.SetValue(originalObject, value, null); } else { childObject = propertyInfo.GetValue(originalObject, null); } } else { FieldInfo fieldInfo = type.GetField(propertyName, BindingFlags.Public | BindingFlags.GetField | BindingFlags.SetField); if (fieldInfo == null) { return; } if (properties.Length == 1) { // 如果是最后一个".",则设置属性 fieldInfo.SetValue(originalObject, value); } else { childObject = fieldInfo.GetValue(originalObject); } } } if (childObject != null) { SetPropertyValueByPath(childObject, properties[1], value); } }