Ejemplo n.º 1
0
        public static IEnumerable <T> SelectIncluding <T>(
            this IQueryable <T> source,
            IEnumerable <Expression <Func <T, object> > > includeExpessions)
        {
            if (source == null)
            {
                throw new ArgumentNullException("Source Object is NULL");
            }
            ParameterExpression parameterExpression = Expression.Parameter(source.ElementType, "t");

            IQueryableExtensions.PredicateRewriterVisitor  predicateRewriterVisitor = new IQueryableExtensions.PredicateRewriterVisitor(parameterExpression);
            Dictionary <string, Tuple <Expression, Type> > dynamicFields            = new Dictionary <string, Tuple <Expression, Type> >();
            int num = 0;

            foreach (Expression <Func <T, object> > includeExpession in includeExpessions)
            {
                if (includeExpession.Body.NodeType == ExpressionType.Convert || includeExpession.Body.NodeType == ExpressionType.ConvertChecked)
                {
                    UnaryExpression body = includeExpession.Body as UnaryExpression;
                    if (body != null)
                    {
                        Type type = body.Operand.Type;
                    }
                }
                Type type1 = includeExpession.Body.Type;
                dynamicFields.Add("f" + (object)num, new Tuple <Expression, Type>(predicateRewriterVisitor.ReplaceParameter(includeExpession.Body, includeExpession.Parameters[0]), type1));
                ++num;
            }
            dynamicFields.Add("sourceObject", new Tuple <Expression, Type>((Expression)parameterExpression, source.ElementType));
            Type dynamicType = DynamicTypeBuilder.GetDynamicType(dynamicFields.ToDictionary <KeyValuePair <string, Tuple <Expression, Type> >, string, Type>((Func <KeyValuePair <string, Tuple <Expression, Type> >, string>)(x => x.Key), (Func <KeyValuePair <string, Tuple <Expression, Type> >, Type>)(x => x.Value.Item2)), typeof(object), Type.EmptyTypes);
            List <MemberBinding>           list     = ((IEnumerable <FieldInfo>)dynamicType.GetFields()).Select <FieldInfo, MemberAssignment>((Func <FieldInfo, MemberAssignment>)(p => Expression.Bind((MemberInfo)p, dynamicFields[p.Name].Item1))).OfType <MemberBinding>().ToList <MemberBinding>();
            Expression <Func <T, object> > selector = Expression.Lambda <Func <T, object> >((Expression)Expression.MemberInit(Expression.New(dynamicType.GetConstructor(Type.EmptyTypes)), (IEnumerable <MemberBinding>)list), parameterExpression);

            return(source.Select <T, object>(selector).AsEnumerable <object>().Select <object, T>((Func <object, T>)(x =>
            {
                // ISSUE: reference to a compiler-generated field
                if (IQueryableExtensions.o__13 <T> .p__1 == null)
                {
                    // ISSUE: reference to a compiler-generated field
                    IQueryableExtensions.o__13 <T> .p__1 = CallSite <Func <CallSite, object, T> > .Create(Microsoft.CSharp.RuntimeBinder.Binder.Convert(CSharpBinderFlags.ConvertExplicit, typeof(T), typeof(IQueryableExtensions)));
                }
                // ISSUE: reference to a compiler-generated field
                Func <CallSite, object, T> target = IQueryableExtensions.o__13 <T> .p__1.Target;
                // ISSUE: reference to a compiler-generated field
                CallSite <Func <CallSite, object, T> > p1 = IQueryableExtensions.o__13 <T> .p__1;
                // ISSUE: reference to a compiler-generated field
                if (IQueryableExtensions.o__13 <T> .p__0 == null)
                {
                    // ISSUE: reference to a compiler-generated field
                    IQueryableExtensions.o__13 <T> .p__0 = CallSite <Func <CallSite, object, object> > .Create(Microsoft.CSharp.RuntimeBinder.Binder.GetMember(CSharpBinderFlags.None, "sourceObject", typeof(IQueryableExtensions), (IEnumerable <CSharpArgumentInfo>) new CSharpArgumentInfo[1]
                    {
                        CSharpArgumentInfo.Create(CSharpArgumentInfoFlags.None, (string)null)
                    }));
                }
                // ISSUE: reference to a compiler-generated field
                // ISSUE: reference to a compiler-generated field
                object obj = IQueryableExtensions.o__13 <T> .p__0.Target((CallSite)IQueryableExtensions.o__13 <T> .p__0, x);
                return target((CallSite)p1, obj);
            })));
        }
Ejemplo n.º 2
0
        public static IQueryable <object> SelectBy <T>(
            this IQueryable <T> source,
            IEnumerable <string> propertyNames)
        {
            if (source == null)
            {
                throw new ArgumentNullException("Source Object is NULL");
            }
            ParameterExpression sourceItem = Expression.Parameter(source.ElementType, "t");
            Dictionary <string, PropertyInfo> sourceProperties = propertyNames.Where <string>((Func <string, bool>)(name => source.ElementType.GetProperty(name) != (PropertyInfo)null)).ToDictionary <string, string, PropertyInfo>((Func <string, string>)(name => name), (Func <string, PropertyInfo>)(name => source.ElementType.GetProperty(name)));
            Type dynamicType = DynamicTypeBuilder.GetDynamicType(sourceProperties.Values.ToDictionary <PropertyInfo, string, Type>((Func <PropertyInfo, string>)(f => f.Name), (Func <PropertyInfo, Type>)(f => f.PropertyType)), typeof(object), Type.EmptyTypes);
            List <MemberBinding>           list     = ((IEnumerable <FieldInfo>)dynamicType.GetFields()).Select <FieldInfo, MemberAssignment>((Func <FieldInfo, MemberAssignment>)(p => Expression.Bind((MemberInfo)p, (Expression)Expression.Property((Expression)sourceItem, sourceProperties[p.Name])))).OfType <MemberBinding>().ToList <MemberBinding>();
            Expression <Func <T, object> > selector = Expression.Lambda <Func <T, object> >((Expression)Expression.MemberInit(Expression.New(dynamicType.GetConstructor(Type.EmptyTypes)), (IEnumerable <MemberBinding>)list), sourceItem);

            return(source.Select <T, object>(selector));
        }
Ejemplo n.º 3
0
 public static Type GetDynamicType(
     Dictionary <string, Type> fields,
     Type basetype,
     Type[] interfaces)
 {
     if (fields == null)
     {
         throw new ArgumentNullException(nameof(fields));
     }
     if (fields.Count == 0)
     {
         throw new ArgumentOutOfRangeException(nameof(fields), "fields must have at least 1 field definition");
     }
     try
     {
         Monitor.Enter((object)DynamicTypeBuilder.builtTypes);
         string typeKey = DynamicTypeBuilder.GetTypeKey(fields);
         if (DynamicTypeBuilder.builtTypes.ContainsKey(typeKey))
         {
             return(DynamicTypeBuilder.builtTypes[typeKey].Item2);
         }
         string      name        = "DynamicLinqType" + DynamicTypeBuilder.builtTypes.Count.ToString();
         TypeBuilder typeBuilder = DynamicTypeBuilder.moduleBuilder.DefineType(name, TypeAttributes.Public | TypeAttributes.Serializable, (Type)null, Type.EmptyTypes);
         foreach (KeyValuePair <string, Type> field in fields)
         {
             typeBuilder.DefineField(field.Key, field.Value, FieldAttributes.Public);
         }
         DynamicTypeBuilder.builtTypes[typeKey] = new Tuple <string, Type>(name, typeBuilder.CreateType());
         return(DynamicTypeBuilder.builtTypes[typeKey].Item2);
     }
     catch
     {
         throw;
     }
     finally
     {
         Monitor.Exit((object)DynamicTypeBuilder.builtTypes);
     }
 }