public static IAction ExtendedConvertToAction(this ActionModel model, IEntityMetadata metaData, IEntityRegistry registry, IServiceProvider services) { if (((IEnumerable <object>)metaData.Type.GetCustomAttributes(typeof(ObsoleteAttribute), false)).Any <object>()) { return((IAction)null); } IAction instance1 = ActivatorUtilities.CreateInstance(services, metaData.Type, Array.Empty <object>()) as IAction; if (instance1 == null) { return((IAction)null); } PropertyInfo[] properties = instance1.GetType().GetProperties(BindingFlags.Instance | BindingFlags.Public); if (((IEnumerable <PropertyInfo>)properties).Any <PropertyInfo>((Func <PropertyInfo, bool>)(p => ModelExtensions.IsBinaryOperator(p.PropertyType)))) { PropertyInfo propertyInfo = ((IEnumerable <PropertyInfo>)properties).FirstOrDefault <PropertyInfo>((Func <PropertyInfo, bool>)(p => ModelExtensions.IsBinaryOperator(p.PropertyType))); PropertyModel operatorModelProperty = model.Properties.FirstOrDefault <PropertyModel>((Func <PropertyModel, bool>)(x => x.IsOperator)); if (operatorModelProperty != null) { IEntityMetadata entityMetadata = registry.GetOperators().FirstOrDefault <IEntityMetadata>((Func <IEntityMetadata, bool>)(m => m.Type.FullName.Equals(operatorModelProperty.Value, StringComparison.OrdinalIgnoreCase))); object instance2 = ActivatorUtilities.CreateInstance(services, entityMetadata?.Type, Array.Empty <object>()); if ((object)propertyInfo != null) { propertyInfo.SetValue((object)instance1, instance2); } } } foreach (PropertyModel property1 in (IEnumerable <PropertyModel>)model.Properties) { if (!property1.IsOperator) { PropertyInfo property2 = instance1.GetType().GetProperty(property1.Name, BindingFlags.Instance | BindingFlags.Public); Type type = (!property2.PropertyType.IsGenericType ? 0 : (typeof(IRuleValue <>).IsAssignableFrom(property2.PropertyType.GetGenericTypeDefinition()) ? 1 : 0)) != 0 ? ((IEnumerable <Type>)property2.PropertyType.GetGenericArguments()).FirstOrDefault <Type>() : property2.PropertyType; if (!(type == (Type)null)) { string fullName = type.FullName; if (!(fullName == "System.DateTime")) { if (!(fullName == "System.DateTimeOffset")) { if (!(fullName == "System.Int32")) { if (!(fullName == "System.Boolean")) //our extended boolean condition { if (fullName == "System.Decimal") { Decimal result; Decimal.TryParse(property1.Value, out result); LiteralRuleValue <Decimal> literalRuleValue = new LiteralRuleValue <Decimal>() { Value = result }; property2.SetValue((object)instance1, (object)literalRuleValue, (object[])null); } else { LiteralRuleValue <string> literalRuleValue = new LiteralRuleValue <string>() { Value = property1.Value }; property2.SetValue((object)instance1, (object)literalRuleValue, (object[])null); } } else { bool result; bool.TryParse(property1.Value, out result); LiteralRuleValue <bool> literalRuleValue = new LiteralRuleValue <bool>() { Value = result }; property2.SetValue((object)instance1, (object)literalRuleValue, (object[])null); } } else { int result; int.TryParse(property1.Value, out result); LiteralRuleValue <int> literalRuleValue = new LiteralRuleValue <int>() { Value = result }; property2.SetValue((object)instance1, (object)literalRuleValue, (object[])null); } } else { DateTimeOffset result; DateTimeOffset.TryParse(property1.Value, out result); LiteralRuleValue <DateTimeOffset> literalRuleValue = new LiteralRuleValue <DateTimeOffset>() { Value = result }; property2.SetValue((object)instance1, (object)literalRuleValue, (object[])null); } } else { DateTime result; DateTime.TryParse(property1.Value, out result); LiteralRuleValue <DateTime> literalRuleValue = new LiteralRuleValue <DateTime>() { Value = result }; property2.SetValue((object)instance1, (object)literalRuleValue, (object[])null); } } } } return(instance1); }
public static IAction ConvertToAction(this ActionModel model, IEntityMetadata metaData, IEntityRegistry registry, IServiceProvider services) { if (((IEnumerable <object>)metaData.Type.GetCustomAttributes(typeof(ObsoleteAttribute), false)).Any()) { return(null); } var instance = ActivatorUtilities.CreateInstance(services, metaData.Type) as IAction; if (instance == null) { return(null); } var properties = instance.GetType().GetProperties(BindingFlags.Instance | BindingFlags.Public); if (((IEnumerable <PropertyInfo>)properties).Any(p => ModelExtensions.IsBinaryOperator(p.PropertyType))) { var propertyInfo = ((IEnumerable <PropertyInfo>)properties).FirstOrDefault(p => ModelExtensions.IsBinaryOperator(p.PropertyType)); var operatorModelProperty = model.Properties.FirstOrDefault(x => x.IsOperator); if (operatorModelProperty != null) { var entityMetadata = registry.GetOperators().FirstOrDefault(m => m.Type.FullName.Equals(operatorModelProperty.Value, StringComparison.OrdinalIgnoreCase)); var instance2 = ActivatorUtilities.CreateInstance(services, entityMetadata?.Type); if ((object)propertyInfo != null) { propertyInfo.SetValue(instance, instance2); } } } foreach (var property in model.Properties) { if (!property.IsOperator) { var instanceProperty = instance.GetType().GetProperty(property.Name, BindingFlags.Instance | BindingFlags.Public); var type = (instanceProperty.PropertyType.IsGenericType && (typeof(IRuleValue <>).IsAssignableFrom(instanceProperty.PropertyType.GetGenericTypeDefinition()))) ? ((IEnumerable <Type>)instanceProperty.PropertyType.GetGenericArguments()).FirstOrDefault() : instanceProperty.PropertyType; if (type != null) { switch (type.FullName) { case "System.Decimal": Decimal.TryParse(property.Value, out Decimal result1); var literalRuleValue1 = new LiteralRuleValue <Decimal>() { Value = result1 }; instanceProperty.SetValue(instance, literalRuleValue1, null); break; case "System.Int32": int.TryParse(property.Value, out int result2); var literalRuleValue2 = new LiteralRuleValue <int>() { Value = result2 }; instanceProperty.SetValue(instance, literalRuleValue2, null); break; case "System.DateTimeOffset": DateTimeOffset.TryParse(property.Value, out DateTimeOffset result3); var literalRuleValue3 = new LiteralRuleValue <DateTimeOffset>() { Value = result3 }; instanceProperty.SetValue(instance, literalRuleValue3, null); break; case "System.DateTime": DateTime.TryParse(property.Value, out DateTime result4); var literalRuleValue4 = new LiteralRuleValue <DateTime>() { Value = result4 }; instanceProperty.SetValue(instance, literalRuleValue4, null); break; case "System.Boolean": bool.TryParse(property.Value, out bool result5); var literalRuleValue5 = new LiteralRuleValue <bool>() { Value = result5 }; instanceProperty.SetValue(instance, literalRuleValue5, null); break; default: var literalRuleValue6 = new LiteralRuleValue <string>() { Value = property.Value }; instanceProperty.SetValue(instance, literalRuleValue6, null); break; } } } } return(instance); }