private void ParseParameter(KeyValuePair <string, StringValues> parameter, Type type) { string parameterName = parameter.Key.Trim(); if (_options.KnownQueryStringParameters.Contains(parameterName, StringComparer.OrdinalIgnoreCase)) { return; } PropertyInfo property = null; if (ParameterIsQueryStringParameter(parameterName)) { property = _propertyProvider.GetProperties(typeof(QueryString)).FirstOrDefault(p => p.Name.Equals(parameterName, StringComparison.OrdinalIgnoreCase)); ParseValue(type, property, parameterName, parameter.Value, _queryString); return; } else if (ParameterIsQueryParamsParameter(parameterName)) { property = _propertyProvider.GetProperties(typeof(QueryParams)).FirstOrDefault(p => p.Name.Equals(parameterName, StringComparison.OrdinalIgnoreCase)); ParseValue(type, property, parameterName, parameter.Value, _queryString.QueryParams); return; } AddInvalidParameterError(parameterName); }
public void Must_return_a_collection_of_class_property_accessors(Type classType) { // Arrange var source = classType.Instantiate(); // Act var accessors = sut.GetProperties(source); // Assert accessors.Should().BeEquivalentByName(classType.GetProperties()); }
private Expression BuildMasterDetailManyToManyRelationExpression(Expression memberChain, Type masterType, Type detailType, IEnumerable <Type> routeEntityTypes, int currentRouteEntityTypeIndex) { var listProperties = _propertyProvider.GetProperties(detailType).Where(p => p.PropertyType.IsListOfRawGeneric(typeof(Entity <>))); foreach (var detailProperty in listProperties) { var manyToManyType = detailProperty.PropertyType.BaseGenericType().GenericTypeArguments[0]; var masterIdProperty = manyToManyType.GetPropertyIgnoreCase(masterType.Name + "id"); if (masterIdProperty != null) { var unprocessedRouteEntityTypes = GetUnprocessedRouteEntityTypes(routeEntityTypes, currentRouteEntityTypeIndex); unprocessedRouteEntityTypes.Add(manyToManyType); return(Expression.Call( _methodProvider.MakeGenericMethod(typeof(Enumerable), "Any", new Type[] { typeof(IEnumerable <>), typeof(Func <,>) }, new Type[] { manyToManyType }), Expression.Property(memberChain, detailProperty), BuildFilter(unprocessedRouteEntityTypes))); } } return(null); }
protected virtual bool ValidateFieldPart(ref Type type, string fieldPart, bool isLast) { var property = _propertyProvider.GetProperties(type).FirstOrDefault(p => p.Name.Equals(fieldPart, StringComparison.OrdinalIgnoreCase)); if (property == null) { return(false); } var genericTypeDefinition = type.BaseGenericType().GetGenericTypeDefinition(); bool isDetail = property.PropertyType.IsDetailOfRawGeneric(genericTypeDefinition); if (isDetail && !isLast) { type = property.PropertyType.IsListOfRawGeneric(genericTypeDefinition) ? property.PropertyType.BaseGenericType().GenericTypeArguments[0] : property.PropertyType; return(true); } else if (!isDetail && isLast) { return(true); } return(false); }
private bool TryGetSkillExperience(string skill, Skills skills, out decimal exp, out int level) { var props = propertyProvider.GetProperties <Skills, decimal>(); exp = 0; level = 0; if (string.IsNullOrEmpty(skill)) { foreach (var property in props) { var experience = (decimal)property.GetValue(skills); level += GameMath.ExperienceToLevel(experience); exp += experience; } exp = Math.Floor(exp); return(true); } var targetProperty = props.FirstOrDefault(x => x.PropertyType == typeof(decimal) && x.Name.Equals(skill, StringComparison.OrdinalIgnoreCase)); if (targetProperty == null) { return(false); } exp = Math.Floor((decimal)targetProperty.GetValue(skills)); level = GameMath.ExperienceToLevel(exp); return(true); }
public override void Enumerate(IMemberCompletionAcceptor acceptor) { if (acceptor == null) { throw ExceptionBuilder.ArgumentNull("acceptor"); } NamedConstantExpression namedConstantExpression = _expressionBeforeDot as NamedConstantExpression; ParameterExpression parameterExpression = _expressionBeforeDot as ParameterExpression; IList <PropertyBinding> properties = null; if (namedConstantExpression == null && parameterExpression == null) { // The properties must be provided by a regular property provider. IPropertyProvider propertyProvider = _scope.DataContext.MetadataContext.PropertyProviders[_expressionBeforeDot.ExpressionType]; if (propertyProvider != null) { properties = propertyProvider.GetProperties(_expressionBeforeDot.ExpressionType); } } else { // The expression before the dot is named constant or a parameter. In both cases, the properties // could be contributed as custom properties. if (namedConstantExpression != null) { properties = namedConstantExpression.Constant.CustomProperties; } else { properties = parameterExpression.Parameter.CustomProperties; } } if (properties != null) { foreach (PropertyBinding propertyBinding in properties) { acceptor.AcceptProperty(propertyBinding); } } // Now contribute any methods IMethodProvider methodProvider = _scope.DataContext.MetadataContext.MethodProviders[_expressionBeforeDot.ExpressionType]; if (methodProvider != null) { MethodBinding[] methods = methodProvider.GetMethods(_expressionBeforeDot.ExpressionType); foreach (MethodBinding methodBinding in methods) { acceptor.AcceptMethod(methodBinding); } } }
public void FillForeignKeysFromRoute <TEntity>(TEntity entity) { foreach (var property in _propertyProvider.GetProperties(entity.GetType()) .Where(p => _actionContextAccessor.HasRouteParamIgnoreCase(p.Name))) { entity.SetPropertyValue(property.Name, Convert.ChangeType(_actionContextAccessor.GetRouteParamIgnoreCase(property.Name), property.PropertyType)); } }
private bool TryGetSkillExperience(string skill, Skills skills, out double exp, out int level) { var expProps = propertyProvider.GetProperties <Skills, double>(); var lvlProps = propertyProvider.GetProperties <Skills, int>(); exp = 0; level = 0; if (string.IsNullOrEmpty(skill)) { foreach (var prop in expProps) { exp += (double)prop.GetValue(skills); } foreach (var prop in lvlProps) { level += (int)prop.GetValue(skills); } return(true); } var expProp = expProps.FirstOrDefault(x => x.Name.Equals(skill, StringComparison.OrdinalIgnoreCase)); if (expProp == null) { return(false); } var lvlProp = lvlProps.FirstOrDefault(x => x.Name.IndexOf(skill, StringComparison.OrdinalIgnoreCase) >= 0); if (lvlProp == null) { return(false); } exp = Math.Floor((double)expProp.GetValue(skills)); level = (int)lvlProp.GetValue(skills); return(true); }
private IEnumerable <ValidationResult> ValidateProperties(object source, IPropertyAccessor parentProperty) { foreach (var property in propertyProvider.GetProperties(source)) { if (property.IsReference) { // Property is a pointer to another class, // so you need to recursively validate this object. var referenceEnd = property.GetValue(source); if (property.IsCollection) { foreach (var child in (IEnumerable)referenceEnd) { foreach (var result in Validate(child, property)) { yield return(result); } } } else { foreach (var result in Validate(referenceEnd, property)) { yield return(result); } } } else { // Simple (build-in) type property. foreach (var result in propertyValidator.Validate(source, property, parentProperty)) { if (result != ValidationResult.Success) { yield return(result); } } } } }
private IEnumerable <PropertyInfo> GetProperties(TWalkInfo walkInfo, IEnumerable <string> fields) { var properties = _propertyProvider.GetProperties(walkInfo.Type); if ((!fields.Any() && walkInfo.RelatedDataLevel > 0) || fields.Contains("*")) { return(properties.Where(p => !p.PropertyType.IsDetailOfRawGeneric(walkInfo.GenericDefinition) || IsDetailAndNotExistInWalkedTypes(p.PropertyType, walkInfo))); } else if (!fields.Any() && walkInfo.RelatedDataLevel <= 0) { return(properties.Where(p => !p.PropertyType.IsDetailOfRawGeneric(walkInfo.GenericDefinition))); } else { return(properties.Where(p => fields.Contains(p.Name, StringComparer.OrdinalIgnoreCase) && (!p.PropertyType.IsDetailOfRawGeneric(walkInfo.GenericDefinition) || IsDetailAndNotExistInWalkedTypes(p.PropertyType, walkInfo)))); } }
/// <summary> /// Returns all properties matching the identifier. /// </summary> /// <param name="type">The type to search the properties in.</param> /// <param name="identifier">The identifier to match the properties.</param> public PropertyBinding[] FindProperty(Type type, Identifier identifier) { if (type == null) { throw ExceptionBuilder.ArgumentNull("type"); } if (identifier == null) { throw ExceptionBuilder.ArgumentNull("identifier"); } // Get property provider responsible for the given type. IPropertyProvider propertyProvider = _propertyProviders[type]; if (propertyProvider == null) { return(new PropertyBinding[0]); } // Get properties from the provider. PropertyBinding[] properties; try { properties = propertyProvider.GetProperties(type); } catch (NQueryException) { throw; } catch (Exception ex) { throw ExceptionBuilder.IPropertyProviderGetPropertiesFailed(ex); } return(FindProperty(properties, identifier)); }
private IQueryable <TEntity> ApplySearch(QueryWalkInfo <TEntity> walkInfo) { if (string.IsNullOrWhiteSpace(walkInfo.QueryParams.Search)) { return(walkInfo.Query); } var searchKeyProperties = _propertyProvider.GetProperties(typeof(TEntity)).Where(p => p.GetCustomAttribute <SearchKeyAttribute>() != null); if (searchKeyProperties.Where(p => p.PropertyType != typeof(string)).Any()) { throw new InvalidOperationException("CoreApiDirect.Entities.SearchKeyAttribute is added to a non string property."); } if (!searchKeyProperties.Any()) { return(walkInfo.Query); } var type = typeof(TEntity); var parameter = Expression.Parameter(type, type.Name.Camelize()); Expression expressionOr = null; foreach (var searchKeyProperty in searchKeyProperties) { Expression property = Expression.Property(parameter, searchKeyProperty.Name); var value = Expression.Constant(walkInfo.CaseSensitiveSearch ? walkInfo.QueryParams.Search : walkInfo.QueryParams.Search.ToLower()); if (!walkInfo.CaseSensitiveSearch) { property = Expression.Call(property, typeof(string).GetMethod("ToLower", new Type[] { })); } var containsMethod = typeof(string).GetMethod("Contains", new Type[] { typeof(string) }); var expression = Expression.Equal(Expression.Call(property, containsMethod, value), Expression.Constant(true)); expressionOr = expressionOr == null ? expression : Expression.OrElse(expressionOr, expression); } return(walkInfo.Query.Where(Expression.Lambda <Func <TEntity, bool> >(expressionOr, parameter))); }
private Expression BuildMasterDetailManyToManyRelationExpression(ParameterExpression parameter, Type masterType, Type detailType, object masterId) { var properties = _propertyProvider.GetProperties(detailType).Where(p => p.PropertyType.IsListOfRawGeneric(typeof(Entity <>))); foreach (var property in properties) { var propertyGenericArgument = property.PropertyType.BaseGenericType().GenericTypeArguments[0]; var masterIdProperty = propertyGenericArgument.GetPropertyIgnoreCase(masterType.Name + "id"); if (masterIdProperty != null) { return(Expression.Call( _methodProvider.MakeGenericMethod(typeof(Enumerable), "Any", new Type[] { typeof(IEnumerable <>), typeof(Func <,>) }, new Type[] { propertyGenericArgument }), Expression.Property(parameter, property), BuildAnyExpresion(propertyGenericArgument, masterIdProperty, masterId))); } } return(null); }
public TableBinding Add(IEnumerable enumerable, Type elementType, string tableName) { if (enumerable == null) { throw ExceptionBuilder.ArgumentNull("enumerable"); } if (elementType == null) { throw ExceptionBuilder.ArgumentNull("elementType"); } if (tableName == null) { throw ExceptionBuilder.ArgumentNull("tableName"); } IPropertyProvider elementPropertyProvider = GetRequiredPropertyProvider(elementType); PropertyBinding[] properties; try { properties = elementPropertyProvider.GetProperties(elementType); } catch (NQueryException) { throw; } catch (Exception ex) { throw ExceptionBuilder.IPropertyProviderGetPropertiesFailed(ex); } EnumerableTableBinding tableBinding = new EnumerableTableBinding(enumerable, elementType, properties, tableName); Add(tableBinding); return(tableBinding); }
protected override IEnumerable <ColumnDefinition> GetColumns() { return(_propertyProvider.GetProperties(_rowType) .Select(p => new PropertyColumnDefinition(_rowType, p))); }