public static bool Get <T>(this WrapperContext context, out Type mapped)
 {
     if (context == null)
     {
         throw new ArgumentNullException(nameof(context));
     }
     return(context.Get(typeof(T), out mapped));
 }
 public static void Set <T, M>(this WrapperContext context)
 {
     if (context == null)
     {
         throw new ArgumentNullException(nameof(context));
     }
     context.Set(typeof(T), typeof(M));
 }
Пример #3
0
        public static IQueryable <T> Wrap <T>(this IQueryable <T> queryable, WrapperContext context)
        {
            if (queryable == null)
            {
                throw new ArgumentNullException(nameof(queryable));
            }
            var visitor = new WrapperVisitor(context);

            return(queryable.Provider.CreateQuery <T>(visitor.Visit(queryable.Expression)));
        }
Пример #4
0
        public static IQueryable <T> Wrap <T, M>(this IQueryable <M> queryable, WrapperContext context = null)
            where M : T
        {
            if (queryable == null)
            {
                throw new ArgumentNullException(nameof(queryable));
            }
            if (context == null)
            {
                context = new WrapperContext();
            }
            WrappedQueryableProvider <T, M> provider = new WrappedQueryableProvider <T, M>(queryable.Provider, context);

            return(provider.CreateQuery <T>(queryable.Expression));
        }
Пример #5
0
        public static IQueryable <M> Unwrap <T, M>(this IQueryable <T> queryable, WrapperContext context = null)
            where M : T
        {
            if (queryable == null)
            {
                throw new ArgumentNullException(nameof(queryable));
            }
            WrappedQueryable <T, M> wrapped = queryable as WrappedQueryable <T, M>;

            if (wrapped == null)
            {
                throw new NotSupportedException("不支持的类型。");
            }
            WrappedQueryableProvider <T, M> provider = wrapped.Provider;
            var visitor    = new WrapperVisitor(context ?? provider.Context);
            var expression = visitor.Visit(wrapped.Expression);

            return(provider.InnerQueryProvider.CreateQuery <M>(expression));
        }
        public static void Set <T, M>(this WrapperContext context, string targetMethod, string mappedMethod)
        {
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }
            var target = typeof(T).GetMethod(targetMethod);

            if (target == null)
            {
                throw new ArgumentException(nameof(targetMethod), "方法不存在。");
            }
            var mapped = typeof(M).GetMethod(mappedMethod);

            if (mapped == null)
            {
                throw new ArgumentException(nameof(mappedMethod), "方法不存在。");
            }
            context.Set(target, mapped);
        }
 public static void Set <T, M, P>(this WrapperContext context, Expression <Func <T, P> > target, Expression <Func <M, P> > mapped)
 {
     if (context == null)
     {
         throw new ArgumentNullException(nameof(context));
     }
     if (target == null)
     {
         throw new ArgumentNullException(nameof(target));
     }
     if (mapped == null)
     {
         throw new ArgumentNullException(nameof(mapped));
     }
     if (target.Body.NodeType != ExpressionType.MemberAccess || ((MemberExpression)target.Body).Expression.NodeType != ExpressionType.Parameter)
     {
         throw new ArgumentException("目标只能为类型成员。");
     }
     if (mapped.Body.NodeType != ExpressionType.MemberAccess || ((MemberExpression)mapped.Body).Expression.NodeType != ExpressionType.Parameter)
     {
         throw new ArgumentException("映射只能为类型成员。");
     }
     context.Set(((MemberExpression)target.Body).Member, ((MemberExpression)mapped.Body).Member);
 }
 public WrapperVisitor(WrapperContext context)
 {
     Context     = context ?? throw new ArgumentNullException(nameof(context));
     _Parameters = new Dictionary <ParameterExpression, ParameterExpression>();
 }
Пример #9
0
 public WrappedQueryableProvider(IQueryProvider queryProvider, WrapperContext context)
 {
     Context            = context ?? throw new ArgumentNullException(nameof(context));
     InnerQueryProvider = queryProvider ?? throw new ArgumentNullException(nameof(queryProvider));
 }