public OperateTypeAttribute(ESingleOperateType type1, params ESingleOperateType[] restType) { OperateType = typeof(ESingleOperateType); _OperateTypeList.Add(type1); if (restType != null) { foreach (var item in restType) { if (!_OperateTypeList.Contains(item)) { _OperateTypeList.Add(item); } } } }
public bool CanExecute(object param, ESingleOperateType operType) { if (!(param is EquipmentBase)) { return(false); } EquipmentBase equip = param as EquipmentBase; SingleOperate so = new SingleOperate() { EquipmentType = equip.GetType(), OperateType = operType }; ObservableCollection <OperateCondition> operateConditionList = null; SingleOperConditionDic.TryGetValue(so, out operateConditionList); if (operateConditionList == null) { return(false); //(重要)没有操作条件的记录,不是没有限制随便操作,而是不能操作。没有限制的操作ConditionList为空集合而非没有 } foreach (var operCondition in operateConditionList) { //找需要操作的设备中关联的设备 Type deviceType = equip.GetType(); PropertyInfo[] propertyInfos = deviceType.GetProperties(); foreach (var propertyInfo in propertyInfos) { if (propertyInfo.Name == operCondition.PropertyName) { if (propertyInfo.PropertyType.IsValueType) //也可用 if (operCondition.PropertyValue != null) { if (propertyInfo.GetValue(equip).Equals(operCondition.PropertyValue) == false) //这里!=有问题,为什么?两个false不相等 { return(false); } } else if (propertyInfo.PropertyType.IsSubclassOf(typeof(EquipmentBase))) //如果是单设备 { var relatedEquip = propertyInfo.GetValue(equip); //实际上必须是EquipmentBase子类 foreach (var relatedProp in propertyInfo.PropertyType.GetProperties()) { foreach (var pair in operCondition.RelatedEquipProList) { if (pair.PropertyName == relatedProp.Name) { if (pair.PropertyValue.Equals(relatedProp.GetValue(relatedEquip)) == false) { return(false); } } } } } //else if (propertyInfo.PropertyType.IsAssignableFrom(typeof(ICollection)))//为什么不行 else if (propertyInfo.GetValue(equip) is ICollection) //如果是集合类 { ICollection relatedEquips = (ICollection)propertyInfo.GetValue(equip); //关联的设备 foreach (var relatedEquip in relatedEquips) { foreach (var relatedProp in relatedEquip.GetType().GetProperties()) { foreach (var pair in operCondition.RelatedEquipProList) { if (pair.PropertyName == relatedProp.Name) { if (pair.PropertyValue.Equals(relatedProp.GetValue(relatedEquip)) == false) { return(false); } } } } } } } } } return(true); }