/// <summary> /// Filters the source collection using the passed query parameters. /// </summary> /// <param name="source">The source items to filter.</param> /// <param name="query">The query parameters defining the filter.</param> /// <typeparam name="T">The <see cref="Type"/> of items in the source collection.</typeparam> /// <returns>A filtered and projected enumeration of the source collection.</returns> public static IQueryable <object> Filter <T>(this IEnumerable <T> source, NameValueCollection query) { CustomContract.Requires <ArgumentNullException>(source != null); CustomContract.Requires <ArgumentNullException>(query != null); var parser = new ParameterParser <T>(); return(Filter(source, parser.Parse(query))); }
/// <summary> /// Adds the defined mapping to the mapper. /// </summary> /// <typeparam name="TSource">The source parameter type.</typeparam> /// <typeparam name="TTarget">The target parameter type.</typeparam> /// <returns>An instance of a <see cref="PredicateMapper"/>.</returns> public PredicateMapper And <TSource, TTarget>() { CustomContract.Ensures(CustomContract.Result <PredicateMapper>() != null); if (_converters.All(x => x.SourceType != typeof(TSource))) { _converters.Add(new PredicateConverter <TSource, TTarget>()); } return(this); }
public static bool IsAnonymousType(this Type type) { CustomContract.Requires <ArgumentNullException>(type != null); return(KnownAnonymousTypes.GetOrAdd( type, t => t.GetCustomAttribute <CompilerGeneratedAttribute>() != null && t.IsGenericType() && t.Name.Contains("AnonymousType") && (t.Name.StartsWith("<>") || t.Name.StartsWith("VB$")) && (t.Attributes() & TypeAttributes.NotPublic) == TypeAttributes.NotPublic)); }
private static MemberInfo CheckFrontingProperty(MemberInfo field) { CustomContract.Requires(field != null); var declaringType = field.DeclaringType; var correspondingProperty = declaringType.GetProperties() .FirstOrDefault(x => string.Equals(x.Name, field.Name.Replace("_", string.Empty), StringComparison.OrdinalIgnoreCase)); return(correspondingProperty ?? field); }
public IQueryable <object> Filter(IEnumerable <T> model) { var result = _filterExpression != null ? model.AsQueryable().Where(_filterExpression) : model.AsQueryable(); CustomContract.Assert(result != null); if (_sortDescriptions.Any()) { var isFirst = true; foreach (var sortDescription in _sortDescriptions.Where(x => x != null)) { if (isFirst) { isFirst = false; result = sortDescription.Direction == SortDirection.Ascending ? result.OrderBy(sortDescription.KeySelector) : result.OrderByDescending(sortDescription.KeySelector); } else { var orderedEnumerable = result as IOrderedQueryable <T>; CustomContract.Assume(orderedEnumerable != null); result = sortDescription.Direction == SortDirection.Ascending ? orderedEnumerable.ThenBy(sortDescription.KeySelector) : orderedEnumerable.ThenByDescending(sortDescription.KeySelector); } } } if (_skip > 0) { CustomContract.Assume(result != null); result = result.Skip(_skip); } if (_top > -1) { CustomContract.Assume(result != null); result = result.Take(_top); } CustomContract.Assume(_selectExpression == null || result != null); return(new UntypedQueryable <T>(result, _selectExpression)); }
public static Tuple <Type, string> GetNameFromAlias(this IMemberNameResolver memberNameResolver, MemberInfo alias, Type sourceType) { CustomContract.Requires(sourceType != null); CustomContract.Requires(alias != null); CustomContract.Ensures(CustomContract.Result <Tuple <Type, string> >() != null); var source = sourceType.GetMembers() .Select(x => new { Original = x, Name = memberNameResolver.ResolveName(x) }) .FirstOrDefault(x => x.Original.Name == alias.Name); return(source != null ? new Tuple <Type, string>(GetMemberType(source.Original), source.Name) : new Tuple <Type, string>(GetMemberType(alias), alias.Name)); }
public static IOrderedQueryable <T> ThenByDescending <T>(this IOrderedQueryable <T> source, Expression keySelector) { CustomContract.Requires(source != null); CustomContract.Requires(keySelector != null); var propertyType = keySelector.GetType().GetGenericArguments()[0].GetGenericArguments()[1]; var orderbyMethod = typeof(Queryable).GetMethods(BindingFlags.Public | BindingFlags.Static).FirstOrDefault(x => x.Name == "ThenByDescending" && x.GetParameters().Length == 2); CustomContract.Assume(orderbyMethod != null); orderbyMethod = orderbyMethod.MakeGenericMethod(typeof(T), propertyType); return((IOrderedQueryable <T>)orderbyMethod.Invoke(null, new object[] { source, keySelector })); }
private static MethodBase GetGenericMethod(Type type, string name, Type[] typeArgs, Type[] argTypes, BindingFlags flags) { CustomContract.Requires(typeArgs != null); CustomContract.Requires(type != null); //CustomContract.Assume(Type.DefaultBinder != null); var typeArity = typeArgs.Length; var methods = type.GetMethods() .Where(m => m.Name == name) .Where(m => m.GetGenericArguments().Length == typeArity) .Where(m => m.GetParameters().Length == argTypes.Length && m.GetParameters().All(p => p.GetCustomAttribute <ParamArrayAttribute>() == null && !(p.IsIn || p.IsOut || p.IsOptional))) .Select(m => m.MakeGenericMethod(typeArgs)); //return Type.DefaultBinder.SelectMethod(flags, methods.ToArray(), argTypes, null); return(methods.First()); }
private static IEnumerable <CustomAttributeBuilder> CreateCustomAttributeBuilders(IEnumerable <CustomAttributeData> customAttributes) { CustomContract.Requires(customAttributes != null); var attributeBuilders = customAttributes .Select( x => { var namedArguments = x.NamedArguments; var properties = namedArguments.Select(a => x.AttributeType.GetProperty(a.MemberName)).OfType <PropertyInfo>().ToArray(); var values = namedArguments.Select(a => a.TypedValue.Value).ToArray(); var constructorArgs = x.ConstructorArguments.Select(a => a.Value).ToArray(); var constructor = x.Constructor; return(new CustomAttributeBuilder(constructor, constructorArgs, properties, values)); }); return(attributeBuilders); }
public static MethodInfo GetAnyAllMethod(string name, Type collectionType) { CustomContract.Requires(collectionType != null); var implementationType = GetIEnumerableImpl(collectionType); var elemType = implementationType.GetGenericArguments()[0]; var predType = typeof(Func <,>).MakeGenericType(elemType, typeof(bool)); var allMethod = (MethodInfo)GetGenericMethod( typeof(Enumerable), name, new[] { elemType }, new[] { implementationType, predType }, BindingFlags.Static); return(allMethod); }
public static Type GetIEnumerableImpl(Type type) { CustomContract.Requires(type != null); // Get IEnumerable implementation. Either type is IEnumerable<T> for some T, // or it implements IEnumerable<T> for some T. We need to find the interface. if (IsIEnumerable(type)) { return(type); } var interfaces = type.FindInterfaces((m, o) => IsIEnumerable(m), null); CustomContract.Assume(interfaces.Count() > 0); var t = interfaces.First(); return(t); }
private static Type GetMemberType(MemberInfo member) { CustomContract.Requires(member != null); switch (member.MemberType) { case MemberTypes.Field: return(((FieldInfo)member).FieldType); case MemberTypes.Property: return(((PropertyInfo)member).PropertyType); case MemberTypes.Method: return(((MethodInfo)member).ReturnType); default: throw new InvalidOperationException(member.MemberType + " is not resolvable"); } }
private static void CreateProperty(TypeBuilder typeBuilder, KeyValuePair <string, MemberInfo> field) { CustomContract.Requires(typeBuilder != null); var propertyType = field.Value.MemberType == MemberTypes.Property ? ((PropertyInfo)field.Value).PropertyType : ((FieldInfo)field.Value).FieldType; var fieldBuilder = typeBuilder.DefineField("_" + field.Key, propertyType, FieldAttributes.Private); var propertyBuilder = typeBuilder.DefineProperty(field.Key, PropertyAttributes.None, propertyType, null); CustomContract.Assume(propertyBuilder != null, "Created above."); SetAttributes(propertyBuilder, field.Value); var getAccessor = typeBuilder.DefineMethod( "get_" + field.Key, GetSetAttr, propertyType, Type.EmptyTypes); var getIl = getAccessor.GetILGenerator(); getIl.Emit(OpCodes.Ldarg_0); getIl.Emit(OpCodes.Ldfld, fieldBuilder); getIl.Emit(OpCodes.Ret); var setAccessor = typeBuilder.DefineMethod( "set_" + field.Key, GetSetAttr, null, new[] { propertyType }); var setIl = setAccessor.GetILGenerator(); setIl.Emit(OpCodes.Ldarg_0); setIl.Emit(OpCodes.Ldarg_1); setIl.Emit(OpCodes.Stfld, fieldBuilder); setIl.Emit(OpCodes.Ret); propertyBuilder.SetGetMethod(getAccessor); propertyBuilder.SetSetMethod(setAccessor); }
public static Tuple <Type, Expression> CreateMemberExpression(this IMemberNameResolver memberNameResolver, ParameterExpression parameter, IEnumerable <string> propertyChain, Type parentType, Expression propertyExpression) { CustomContract.Requires(parentType != null); CustomContract.Requires(propertyChain != null); foreach (var propertyName in propertyChain) { var name = propertyName; var member = memberNameResolver.ResolveAlias(parentType, name); if (member != null) { parentType = GetMemberType(member); propertyExpression = propertyExpression == null ? Expression.MakeMemberAccess(parameter, member) : Expression.MakeMemberAccess(propertyExpression, member); } } return(new Tuple <Type, Expression>(parentType, propertyExpression)); }
private static void SetAttributes(PropertyBuilder propertyBuilder, MemberInfo memberInfo) { CustomContract.Requires(propertyBuilder != null); CustomContract.Requires(memberInfo != null); var customAttributeBuilders = PropertyAttributeBuilders .GetOrAdd( memberInfo, p => { var customAttributes = p.GetCustomAttributesData(); return(CreateCustomAttributeBuilders(customAttributes).ToArray()); }); CustomContract.Assume(customAttributeBuilders != null); foreach (var attribute in customAttributeBuilders) { propertyBuilder.SetCustomAttribute(attribute); } }
private static void SetAttributes(TypeBuilder typeBuilder, Type type) { CustomContract.Requires(typeBuilder != null); CustomContract.Requires(type != null); var attributeBuilders = TypeAttributeBuilders .GetOrAdd( type, t => { var customAttributes = t.GetCustomAttributesData(); return(CreateCustomAttributeBuilders(customAttributes).ToArray()); }); CustomContract.Assume(attributeBuilders != null); foreach (var attributeBuilder in attributeBuilders) { typeBuilder.SetCustomAttribute(attributeBuilder); } }
private static IEnumerable <MemberInfo> GetMembers(Type type) { CustomContract.Requires(type != null); if (type.IsInterface()) { var propertyInfos = new List <MemberInfo>(); var considered = new List <Type>(); var queue = new Queue <Type>(); considered.Add(type); queue.Enqueue(type); while (queue.Count > 0) { var subType = queue.Dequeue(); foreach (var subInterface in subType.GetInterfaces() .Where(x => !considered.Contains(x))) { considered.Add(subInterface); queue.Enqueue(subInterface); } var typeProperties = subType.GetMembers( BindingFlags.FlattenHierarchy | BindingFlags.Public | BindingFlags.Instance); var newPropertyInfos = typeProperties .Where(x => !propertyInfos.Contains(x)); propertyInfos.InsertRange(0, newPropertyInfos); } return(propertyInfos.ToArray()); } var members = type.GetMembers(BindingFlags.FlattenHierarchy | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance); return(members); }
public PredicateMapper MapMember <TSource, TTarget, TValue>( Expression <Func <TSource, TValue> > source, Expression <Func <TTarget, TValue> > result) { CustomContract.Requires <ArgumentNullException>(result != null); CustomContract.Requires <ArgumentNullException>(source != null); CustomContract.Ensures(CustomContract.Result <PredicateMapper>() != null); var memberBody = result.Body as MemberExpression; if (memberBody == null) { throw new ArgumentException("Expression should access member.", "result"); } var sourceBody = source.Body as MemberExpression; if (sourceBody == null) { throw new ArgumentException("Expression should access member.", "source"); } var resultMember = memberBody.Member; var sourceMember = sourceBody.Member; var converter = _converters.FirstOrDefault(x => x.SourceType == typeof(TSource) && x.TargetType == typeof(TTarget)); if (converter == null) { converter = new PredicateConverter <TSource, TTarget>(); _converters.Add(converter); } converter.Substitutions[sourceMember] = resultMember; return(this); }
public static Stream ToStream(this string input) { CustomContract.Requires(input != null); return(new MemoryStream(Encoding.UTF8.GetBytes(input ?? string.Empty))); }
public static string Capitalize(this string input) { CustomContract.Requires(!string.IsNullOrEmpty(input)); return(char.ToUpperInvariant(input[0]) + input.Substring(1)); }
private void Invariants() { CustomContract.Invariant(_source != null); }
public MemberInfo ResolveAlias(Type type, string alias) { CustomContract.Requires <ArgumentNullException>(type != null); CustomContract.Requires <ArgumentException>(!string.IsNullOrWhiteSpace(alias)); throw new NotImplementedException(); }
/// <summary> /// Create a map based on the passed type arguments. /// </summary> /// <typeparam name="TSource">The source parameter type.</typeparam> /// <typeparam name="TTarget">The target parameter type.</typeparam> /// <returns>An instance of a <see cref="PredicateMapper"/></returns> public static PredicateMapper Map <TSource, TTarget>() { CustomContract.Ensures(CustomContract.Result <PredicateMapper>() != null); return(new PredicateMapper(new PredicateConverter <TSource, TTarget>())); }
private void Invariants() { CustomContract.Invariant(_writer != null); }
/// <summary> /// Filters the source collection using the passed query parameters. /// </summary> /// <param name="source">The source items to filter.</param> /// <param name="filter">The filter to apply.</param> /// <typeparam name="T">The <see cref="Type"/> of items in the source collection.</typeparam> /// <returns>A filtered and projected enumeration of the source collection.</returns> public static IQueryable <object> Filter <T>(this IEnumerable <T> source, IModelFilter <T> filter) { CustomContract.Requires <ArgumentNullException>(source != null); return(filter == null?source.OfType <object>().AsQueryable() : filter.Filter(source)); }
public IQueryable <object> Filter(IEnumerable <T> source) { CustomContract.Requires <ArgumentNullException>(source != null); throw new NotImplementedException(); }
/// <summary> /// Initializes a new instance of the <see cref="RuntimeTypeProvider"/> class. /// </summary> /// <param name="nameResolver"></param> public RuntimeTypeProvider(IMemberNameResolver nameResolver) { CustomContract.Requires(nameResolver != null); _nameResolver = nameResolver; }
private void Invariants() { CustomContract.Invariant(_converters != null); }