コード例 #1
0
        /// <summary>
        ///     Include specified field to resulting typing
        /// </summary>
        /// <param name="tc">Configuration builder</param>
        /// <param name="field">Field to include</param>
        /// <returns>Fluent</returns>
        public static PropertyExportConfigurationBuilder WithField <T, TData>(this TypeConfigurationBuilder <T> tc,
                                                                              Expression <Func <T, TData> > field)
        {
            var prop = LambdaHelpers.ParseFieldLambda(field);
            ITypeConfigurationBuilder tcb = tc;

            return(new PropertyExportConfigurationBuilder(prop, tc._blueprint));
        }
        /// <summary>
        ///     Include specified method to resulting typing.
        ///     User <see cref="Ts.Parameter{T}()" /> to mock up method parameters or specify configuration for perticular method
        ///     parameter
        /// </summary>
        /// <param name="tc">Configuration builder</param>
        /// <param name="method">Method to include</param>
        /// <param name="configuration">Configuration to be applied to method</param>
        /// <returns>Fluent</returns>
        public static ClassConfigurationBuilder <T> WithMethod <T>(this ClassConfigurationBuilder <T> tc,
                                                                   Expression <Action <T> > method, Action <MethodExportConfiguration> configuration)
        {
            tc.WithMethods(new[] { LambdaHelpers.ParseMethodLambda(method) }, configuration);
            ITypeConfigurationBuilder tcb = tc;

            ExtractParameters(tcb, method);
            return(tc);
        }
コード例 #3
0
        /// <summary>
        ///     Include specified method to resulting typing.
        ///     User <see cref="Ts.Parameter{T}()" /> to mock up method parameters or specify configuration for perticular method
        ///     parameter
        /// </summary>
        /// <param name="tc">Configuration builder</param>
        /// <param name="method">Method to include</param>
        /// <param name="configuration">configuration to be applied to method</param>
        /// <returns>Fluent</returns>
        public static InterfaceConfigurationBuilder <T> WithMethod <T, TData>(this InterfaceConfigurationBuilder <T> tc,
                                                                              Expression <Func <T, TData> > method, Action <MethodConfigurationBuilder> configuration)
        {
            tc.WithMethods(new[] { LambdaHelpers.ParseMethodLambda(method) }, configuration);
            ITypeConfigurationBuilder tcb = tc;

            ExtractParameters(tcb, method);
            return(tc);
        }
        /// <summary>
        ///     Include specified field to resulting typing
        /// </summary>
        /// <param name="tc">Configuration builder</param>
        /// <param name="field">Field to include</param>
        /// <returns>Fluent</returns>
        public static PropertyExportConfigurationBuilder WithField <T, TData>(this TypeConfigurationBuilder <T> tc,
                                                                              Expression <Func <T, TData> > field)
        {
            var prop = LambdaHelpers.ParseFieldLambda(field);
            ITypeConfigurationBuilder tcb = tc;

            return
                ((PropertyExportConfigurationBuilder)
                 tcb.MembersConfiguration.GetOrCreate(prop, () => new PropertyExportConfigurationBuilder(prop)));
        }
        /// <summary>
        ///     Include specified property to resulting typing
        /// </summary>
        /// <param name="tc">Configuration builder</param>
        /// <param name="property">Property to include</param>
        /// <returns>Fluent</returns>
        public static PropertyExportConfiguration WithProperty <T, TData>(this TypeConfigurationBuilder <T> tc,
                                                                          Expression <Func <T, TData> > property)
        {
            var prop = LambdaHelpers.ParsePropertyLambda(property);
            ITypeConfigurationBuilder tcb = tc;

            return
                ((PropertyExportConfiguration)
                 tcb.MembersConfiguration.GetOrCreate(prop, () => new PropertyExportConfiguration()));
        }
        /// <summary>
        ///     Include specified method to resulting typing.
        ///     User <see cref="Ts.Parameter{T}()" /> to mock up method parameters or specify configuration for perticular method
        ///     parameter
        /// </summary>
        /// <param name="tc">Configuration builder</param>
        /// <param name="method">Method to include</param>
        /// <returns>Fluent</returns>
        public static MethodConfigurationBuilder WithMethod <T>(this TypeConfigurationBuilder <T> tc,
                                                                Expression <Action <T> > method)
        {
            var prop = LambdaHelpers.ParseMethodLambda(method);
            ITypeConfigurationBuilder tcb = tc;
            var methodConf = new MethodConfigurationBuilder(prop, tc._blueprint);

            ExtractParameters(tcb, method);
            return(methodConf);
        }
コード例 #7
0
        /// <summary>
        ///     Include specified field to resulting typing
        /// </summary>
        /// <param name="tc">Configuration builder</param>
        /// <param name="fieldName">Name of field to include</param>
        /// <param name="configuration">Configuration to be applied to selected field</param>
        /// <returns>Fluent</returns>
        public static ITypeConfigurationBuilder WithField(this ITypeConfigurationBuilder tc, string fieldName,
                                                          Action <PropertyExportConfigurationBuilder> configuration)
        {
            var field = tc.Type._GetField(fieldName);

            if (field == null)
            {
                ErrorMessages.RTE0013_InvalidField.Throw(fieldName, tc.Type.FullName);
            }
            return(tc.WithFields(new[] { field }, configuration));
        }
        /// <summary>
        ///     Include specified property to resulting typing
        /// </summary>
        /// <param name="tc">Configuration builder</param>
        /// <param name="propertyName">Name of property to include</param>
        /// <param name="configuration">Configuration to be applied to selected property</param>
        /// <returns>Fluent</returns>
        public static ITypeConfigurationBuilder WithProperty(this ITypeConfigurationBuilder tc, string propertyName,
                                                             Action <PropertyExportConfigurationBuilder> configuration)
        {
            var prop = tc.Type._GetProperty(propertyName);

            if (prop == null)
            {
                ErrorMessages.RTE0014_InvalidProperty.Throw(propertyName, tc.Type.FullName);
            }
            return(tc.WithProperties(new[] { prop }, configuration));
        }
        /// <summary>
        ///     Include specified method to resulting typing.
        ///     User <see cref="Ts.Parameter{T}()" /> to mock up method parameters or specify configuration for perticular method
        ///     parameter
        /// </summary>
        /// <param name="tc">Configuration builder</param>
        /// <param name="method">Method to include</param>
        /// <returns>Fluent</returns>
        public static MethodExportConfiguration WithMethod <T>(this TypeConfigurationBuilder <T> tc,
                                                               Expression <Action <T> > method)
        {
            var prop = LambdaHelpers.ParseMethodLambda(method);
            ITypeConfigurationBuilder tcb = tc;
            var methodConf =
                (MethodExportConfiguration)
                tcb.MembersConfiguration.GetOrCreate(prop, () => new MethodExportConfiguration());

            ExtractParameters(tcb, method);
            return(methodConf);
        }
        private static void ExtractParameters(ITypeConfigurationBuilder conf, LambdaExpression methodLambda)
        {
            var mex = methodLambda.Body as MethodCallExpression;

            if (mex == null)
            {
                throw new Exception(
                          "MethodCallExpression should be provided for .WithMethod call. Please use only lamba expressions in this place.");
            }
            var mi = mex.Method;

            var methodParameters = mi.GetParameters();

            if (methodParameters.Length == 0)
            {
                return;
            }


            var i = 0;

            foreach (var expression in mex.Arguments)
            {
                var pi = methodParameters[i];
                i++;

                var call = expression as MethodCallExpression;
                if (call != null)
                {
                    if (call.Method.IsGenericMethod &&
                        call.Method.GetGenericMethodDefinition() == Ts.ParametrizedParameterMethod)
                    {
                        var pcb =
                            (ParameterConfigurationBuilder)
                            conf.ParametersConfiguration.GetOrCreate(pi, () => new ParameterConfigurationBuilder());

                        var parsed = false;
                        var arg    = call.Arguments[0] as LambdaExpression;
                        if (arg != null)
                        {
                            var delg = arg.Compile();
                            delg.DynamicInvoke(pcb);
                            parsed = true;
                        }
                        var uarg = call.Arguments[0] as UnaryExpression; // convert expression
                        if (uarg != null)
                        {
                            var operand = uarg.Operand as MethodCallExpression;
                            if (operand != null)
                            {
                                var actionArg = operand.Object as ConstantExpression;
                                if (actionArg != null)
                                {
                                    var value = actionArg.Value as MethodInfo;
                                    if (value != null)
                                    {
                                        var param     = Expression.Parameter(typeof(ParameterConfigurationBuilder));
                                        var newCall   = Expression.Call(value, param);
                                        var newLambda = Expression.Lambda(newCall, param);
                                        var delg      = newLambda.Compile();
                                        delg.DynamicInvoke(pcb);
                                        parsed = true;
                                    }
                                }
                            }
                            if (!parsed)
                            {
                                throw new Exception(
                                          string.Format(
                                              "Sorry, but {0} is not very good idea for parameter configuration. Try using simple lambda expression.",
                                              call.Arguments[0]));
                            }
                        }
                    }
                }
            }
        }
コード例 #11
0
        private static void ExtractParameters(ITypeConfigurationBuilder conf, LambdaExpression methodLambda)
        {
            var mex = methodLambda.Body as MethodCallExpression;

            if (mex == null)
            {
                ErrorMessages.RTE0008_FluentWithMethodError.Throw();
            }
            var mi = mex.Method;

            var methodParameters = mi.GetParameters();

            if (methodParameters.Length == 0)
            {
                return;
            }


            var i = 0;

            foreach (var expression in mex.Arguments)
            {
                var pi = methodParameters[i];
                i++;

                var call = expression as MethodCallExpression;
                if (call != null)
                {
                    if (call.Method.IsGenericMethod &&
                        call.Method.GetGenericMethodDefinition() == Ts.ParametrizedParameterMethod)
                    {
                        var pcb =
                            (ParameterConfigurationBuilder)
                            conf.ParametersConfiguration.GetOrCreate(pi, () => new ParameterConfigurationBuilder(pi));

                        var parsed = false;
                        var arg    = call.Arguments[0] as LambdaExpression;
                        if (arg != null)
                        {
                            var delg = arg.Compile();
                            delg.DynamicInvoke(pcb);
                            parsed = true;
                        }
                        var uarg = call.Arguments[0] as UnaryExpression; // convert expression
                        if (uarg != null)
                        {
                            var operand = uarg.Operand as MethodCallExpression;
                            if (operand != null)
                            {
                                var actionArg = operand.Object as ConstantExpression;
                                if (actionArg != null)
                                {
                                    var value = actionArg.Value as MethodInfo;
                                    if (value != null)
                                    {
                                        var param     = Expression.Parameter(typeof(ParameterConfigurationBuilder));
                                        var newCall   = Expression.Call(value, param);
                                        var newLambda = Expression.Lambda(newCall, param);
                                        var delg      = newLambda.Compile();
                                        delg.DynamicInvoke(pcb);
                                        parsed = true;
                                    }
                                }
                            }
                            if (!parsed)
                            {
                                ErrorMessages.RTE0009_FluentWithMethodCouldNotParse.Throw(call.Arguments[0]);
                            }
                        }
                    }
                }
            }
        }