Пример #1
0
        private void InferNew(NewExpression ne, TypeInferenceCache cache)
        {
            ne.Props.Values.ForEach(e => InferTypes(e, cache));

            var propTypes = ne.Props.Values.Select(e => cache[e]);
            if (propTypes.All(t => t is ClrType))
            {
                var clrPropTypes = propTypes.Cast<ClrType>().Select(pt => pt.Type);
                cache.Add(ne, AnonymousTypesHelper.ForgeTupleType(ne.Props.Keys, clrPropTypes));
            }
            else
            {
                var fail = Array.FindIndex(propTypes.ToArray(), 
                    t => !(t is ClrType || t is Variant));

                if (fail == -1)
                {
                    cache.Add(ne, new Variant());
                }
                else
                {
                    throw new CannotForgeAnonymousTypeException(
                        Root, ne, ne.Props.Keys.ElementAt(fail), propTypes.ElementAt(fail));
                }
            }
        }
 public CannotForgeAnonymousTypeException(RelinqScriptExpression root, NewExpression ne, string offendingProperty, RelinqScriptType offendingType)
     : base(JSToCSharpExceptionType.CannotForgeAnonymousType, root, ne)
 {
     OffendingProperty = offendingProperty;
     OffendingType = offendingType;
 }
Пример #3
0
        private LinqExpression CompileNew(NewExpression ne, CompilationContext ctx)
        {
            var clrType = ctx.Types[ne] is ClrType ? ((ClrType)ctx.Types[ne]).Type : null;
            if (clrType == null || !clrType.IsAnonymous())
            {
                throw new CSharpBuilderException(
                    JSToCSharpExceptionType.UnexpectedInferredAst, Ast, ne, ctx);
            }

            return LinqExpression.New(
                clrType.GetConstructors()[0],
                ne.Props.Values.Select(propExpr => Compile(propExpr, ctx)),
                ne.Props.Keys.Select(prop => clrType.GetProperty(prop)).ToArray());
        }