internal void Compile(MethodInvoke method)
        {
            // init
            method.AddElement(this);
            method.AddCode(method.ParameterName);
            method.AddLiteral(this.Prefix);
            method.AddLiteral(this.Suffix);

            // fix
            var family = (this.NameParts == null || !this.NameParts.Any(x => x.Name == NamePartName.Family) ? new NamePartElement() : this.NameParts.Single(x => x.Name == NamePartName.Family));
            var given  = (this.NameParts == null || !this.NameParts.Any(x => x.Name == NamePartName.Given) ? new NamePartElement() : this.NameParts.Single(x => x.Name == NamePartName.Given));

            // name part parameters
            using (var lambda = method.AddLambdaExpression(false))
            {
                // array
                lambda.AppendArray("NamePartParameters", new NamePartElement[] { family, given }, (part, scope) =>
                {
                    // init
                    using (var m = scope.AppendMethodInvoke("new NamePartParameters", part))
                    {
                        part.Compile(m);
                    }
                });
            }

            // default parameters
            method.AddDefaultParameters();
        }
Exemple #2
0
 private static void AddFromContext <T>(this MethodInvoke owner, object context, string parameterName, Predicate <T> specified, Func <T, object> value)
 {
     if (context is T && specified((T)context))
     {
         owner.AddCode("{0}: {1}", parameterName, Compiler.GetLiteral(value((T)context)));
     }
 }
 internal void Compile(MethodInvoke method)
 {
     // name
     method.AddElement(this);
     method.AddCode(method.ParameterName);
     method.AddLiteral(this.TextCaseSpecified ? (object)this.TextCase : null);
     method.AddLiteral(this.Prefix);
     method.AddLiteral(this.Suffix);
     method.AddDefaultParameters();
 }
Exemple #4
0
        public static void AddContextAndParameters(this MethodInvoke owner)
        {
            // context
            owner.AddCode(Compiler.CONTEXT_NAME);

            // parameters
            using (var method = owner.AddMethodInvoke("new Parameters", null))
            {
                method.AddCode(method.ParameterName);
                method.AddDefaultParameters();
            }
        }
Exemple #5
0
        public static void AddSortComparer(this MethodInvoke method, EntryElement element)
        {
            // find orders
            var orders = (element == null || element.Sort == null || element.Sort.Keys == null ? new KeyElement[] { } : element.Sort.Keys)
                         .Select(key =>
            {
                // init
                var order = (key.SortOrderSpecified ? key.SortOrder : SortOrder.Ascending);

                // done
                return(Compiler.GetLiteral(order));
            })
                         .ToArray();

            // done
            method.AddCode("new SortComparer({0})", string.Join(", ", orders));
        }
        /// <summary>
        /// Compiles this DatePart.
        /// </summary>
        /// <param name="method"></param>
        internal void Compile(MethodInvoke method)
        {
            // allowed formats?
            if (this.FormatSpecified)
            {
                // init
                var allowed = true;

                // allowed?
                switch (this.Name)
                {
                case DatePartName.Day:
                    allowed = (this.Format == DatePartFormat.Numeric || this.Format == DatePartFormat.NumericLeadingZeros || this.Format == DatePartFormat.Ordinal);
                    break;

                case DatePartName.Month:
                    allowed = (this.Format == DatePartFormat.Long || this.Format == DatePartFormat.Short || this.Format == DatePartFormat.Numeric || this.Format == DatePartFormat.NumericLeadingZeros);
                    break;

                case DatePartName.Year:
                    allowed = (this.Format == DatePartFormat.Long || this.Format == DatePartFormat.Short);
                    break;
                }

                // done
                if (!allowed)
                {
                    throw new CompilerException(this, "Format '{0}' is not allowed for '{1}' date-parts.", this.Format, this.Name);
                }
            }

            // strip periods allowed?
            if (this.StripPeriodsSpecified && this.Name != DatePartName.Month)
            {
                throw new CompilerException(this, "StripPeriods is only allowed for 'Month' date-parts.");
            }

            // name
            method.AddElement(this);
            method.AddCode(method.ParameterName);
            method.AddLiteral(this.Name);

            // format
            if (this.FormatSpecified)
            {
                // specified format
                method.AddLiteral(this.Format);
            }
            else
            {
                // use default
                switch (this.Name)
                {
                case DatePartName.Day:
                    method.AddLiteral(DatePartFormat.Numeric);
                    break;

                case DatePartName.Month:
                    method.AddLiteral(DatePartFormat.Long);
                    break;

                case DatePartName.Year:
                    method.AddLiteral(DatePartFormat.Long);
                    break;

                default:
                    throw new CompilerException("Invalid date-part name.");
                }
            }

            // affixes
            method.AddLiteral(this.Prefix);
            method.AddLiteral(this.Suffix);
            method.AddLiteral(this.TextCaseSpecified ? (object)this.TextCase : null);
            method.AddDefaultParameters();
        }