public bool HandlersReqResult(ProductSettings.ProductRequirement requirement) { bool resB = false; try { string[] KeysArr = RequirementToArray(requirement); LogicalOperatorType logicaloperatorType = (LogicalOperatorType)Enum.Parse(typeof(LogicalOperatorType), requirement.LogicalOperator); CompareOperationType operatorType = (CompareOperationType)Enum.Parse(typeof(CompareOperationType), requirement.ValueOperator); string requirementType = requirement.Type.ToLower(); string methodResult = EvalMethod(requirementType, KeysArr, logicaloperatorType); resB = EvalOperator(methodResult, requirementType, requirement.Value, operatorType, logicaloperatorType); #if DEBUG Logger.GetLogger().Info(requirement.Type + ((KeysArr.Count() > 1) ? " {" + logicaloperatorType + "}" : "") + " (" + string.Join(", ", KeysArr) + ") <" + operatorType + "> [" + requirement.Value + "] => " + resB); #endif } #if DEBUG catch (Exception e) #else catch (Exception) #endif { #if DEBUG Logger.GetLogger().Error("Product Requirement " + requirement.Type + "failed with error message: " + e.Message); #endif } return(resB); }
public static bool CompareOperation(string value1, string value2, CompareOperationType operation, LogicalOperatorType logicalOperatorType = LogicalOperatorType.OR) { bool val1Parsed = double.TryParse(value1, out double val1); bool val2Parsed = double.TryParse(value2, out double val2); bool match = operation switch { CompareOperationType.Contains => value1.Contains(value2), CompareOperationType.StartsWith => value1.StartsWith(value2), CompareOperationType.EndsWith => value1.EndsWith(value2), CompareOperationType.Equal => (val1Parsed && val2Parsed && Equals(val1, val2)) || value1.Equals(value2), CompareOperationType.Greater => val1Parsed && val2Parsed && val1 > val2, CompareOperationType.GreaterEqual => val1Parsed && val2Parsed && val1 >= val2, CompareOperationType.Less => val1Parsed && val2Parsed && val1 < val2, CompareOperationType.LessEqual => val1Parsed && val2Parsed && val1 <= val2, _ => throw new ArgumentOutOfRangeException(), }; if (logicalOperatorType == LogicalOperatorType.NOT) { match = !match; } return(match); }
public static bool CompareOperation(string value1, string value2, CompareOperationType operation, LogicalOperatorType logicalOperatorType = LogicalOperatorType.OR) { bool match; if (operation == CompareOperationType.Contains) { match = value1.Contains(value2); } else if (operation == CompareOperationType.StartsWith) { match = value1.StartsWith(value2); } else if (operation == CompareOperationType.EndsWith) { match = value1.EndsWith(value2); } else { var val1 = Convert.ToDouble(value1); var val2 = Convert.ToDouble(value2); switch (operation) { case CompareOperationType.Equal: match = Equals(val1, val2); break; case CompareOperationType.Greater: match = val1 > val2; break; case CompareOperationType.GreaterEqual: match = val1 >= val2; break; case CompareOperationType.Less: match = val1 < val2; break; case CompareOperationType.LessEqual: match = val1 <= val2; break; default: throw new ArgumentOutOfRangeException(); } } if (logicalOperatorType == LogicalOperatorType.NOT) { match = !match; } return(match); }
public static bool CompareTwoVersions(string value1, string value2, CompareOperationType operation) { bool match = true; string[] val1 = value1.Split('.'); string[] val2 = value2.Split('.'); int min = Math.Min(val1.Length, val2.Length); for (int i = 0; i < min; i++) { match = CompareOperation(val1[i], val2[i], operation); if (!match) { break; } } return(match); }
private bool EvalOperator(string methodResult, string requirementType, string requirementValue, CompareOperationType operatorType, LogicalOperatorType logicalOperatorType) { bool resB = true; switch (requirementType) { case RequirementType.Disk: case RequirementType.Ram: case RequirementType.BrowserDefault: case RequirementType.Processor: case RequirementType.ConfigValue: resB = CompareOperation(methodResult, requirementValue, operatorType, logicalOperatorType); break; case RequirementType.OSVersion: int index = methodResult.IndexOf('.'); resB = CompareOperation(methodResult.Substring(0, index + 2), requirementValue, operatorType, logicalOperatorType); break; case RequirementType.BrowserVersion: case RequirementType.RegistryKeyValue: if (string.IsNullOrEmpty(methodResult)) { resB = (LogicalOperatorType.NOT == logicalOperatorType); } else if (operatorType >= CompareOperationType.Contains || double.TryParse(methodResult, out _)) //True if it succeeds in parsing, false if it fails { resB = CompareOperation(methodResult, requirementValue, operatorType, logicalOperatorType); } else if (IsVersionOnly(methodResult) && IsVersionOnly(requirementValue)) { resB = CompareTwoVersions(methodResult, requirementValue, operatorType); } else { resB = methodResult.Equals(requirementValue); } break; case RequirementType.Process: case RequirementType.BrowserInstalled: case RequirementType.RegistryKeyExists: case RequirementType.HasAdminPrivileges: case RequirementType.UserAdmin: case RequirementType.FileExists: resB = ToBoolean(methodResult) == ToBoolean(requirementValue); break; default: break; } return(resB); }
/// <summary> /// /// </summary> /// <param name="propertyToCompare">Название поля модели, с которым сравниваем</param> public CompareWithPropertyAttribute(string propertyToCompare, CompareOperationType compareType) : base("") { this._propertyToCompare = propertyToCompare; this._compareType = compareType; }