TypeIs() public static method

Creates a TypeBinaryExpression.
public static TypeIs ( Expression expression, Type type ) : TypeBinaryExpression
expression Expression An to set the property equal to.
type Type A to set the property equal to.
return TypeBinaryExpression
        public void TypeBinary_is()
        {
            var expression =
                LinqExpression.TypeIs(
                    LinqExpression.Empty(),
                    typeof(object));

            ShouldRoundrip(expression);
        }
 private TypeBinaryExpression TypeBinaryExpression(
     ExpressionType nodeType, System.Type type, JObject obj)
 {
     var expression = this.Prop(obj, "expression", this.Expression);
     var typeOperand = this.Prop(obj, "typeOperand", this.Type);
     
     switch (nodeType) {
         case ExpressionType.TypeIs:
             return Expr.TypeIs(expression, typeOperand);
         case ExpressionType.TypeEqual:
             return Expr.TypeEqual(expression, typeOperand);
         default:
             throw new NotSupportedException();
     }
 }
        private Expression <Func <TBaseInput, TBaseResult> > CreatePolymorphicExpression()
        {
            // Objective:
            // x is [Mapping.InputType] ?
            // ([TResult]) [Mapping.MapperExpression { ([Mapping1.InputType]) x }]
            // : [Next Mapping OR default]

            var inputParameter = LinqExpression.Parameter(typeof(TBaseInput), "x");

            var conditionalUnits = new List <(LinqExpression condition, LinqExpression expression)>();

            foreach (var entry in _mapping.Entries)
            {
                var mappingExpression = entry.ExpressionGetter();

                // (<Mapping.InputType>) x
                var castedParameter = LinqExpression.ConvertChecked(inputParameter, entry.InputType);
                // x is <Mapping.InputType>
                var condition = LinqExpression.TypeIs(inputParameter, entry.InputType);
                // Mapper((<Mapping.InputType>) x)
                var expression = MapperInliningOperations.InlineMapperExpression(mappingExpression, castedParameter,
                                                                                 false);

                // This cast is needed to make the ternary work.
                // (TResult) Mapper((<Mapping.InputType>) x)
                expression = LinqExpression.ConvertChecked(expression, typeof(TBaseResult));

                conditionalUnits.Add((condition, expression));
            }

            // Traverse the units in reverse, we stack up the expressions as we go.
            LinqExpression?finalExpression = null;

            for (var i = conditionalUnits.Count - 1; i >= 0; i--)
            {
                var(condition, expression) = conditionalUnits[i];

                finalExpression = LinqExpression.Condition(condition,
                                                           expression,
                                                           finalExpression ?? MakeDefaultExpression.For(typeof(TBaseResult)));
            }

            return(LinqExpression.Lambda <Func <TBaseInput, TBaseResult> >(finalExpression !, inputParameter));
        }
示例#4
0
        // TODO: do not test runtime for runtime bound sites
        // TODO: ResolveMethod invalidates modules that were not initialized yet -> snapshot version after method resolution
        // TODO: thread safety: synchronize version snapshot and method resolution
        public void AddTargetTypeTest(object target, Expression /*!*/ targetParameter, RubyContext /*!*/ context, Expression /*!*/ contextExpression)
        {
            // singleton nil:
            if (target == null)
            {
                AddRestriction(Ast.Equal(targetParameter, Ast.Constant(null)));
                context.NilClass.AddFullVersionTest(this, contextExpression);
                return;
            }

            // singletons true, false:
            if (target is bool)
            {
                AddRestriction(Ast.AndAlso(
                                   Ast.TypeIs(targetParameter, typeof(bool)),
                                   Ast.Equal(Ast.Convert(targetParameter, typeof(bool)), Ast.Constant(target))
                                   ));

                if ((bool)target)
                {
                    context.TrueClass.AddFullVersionTest(this, contextExpression);
                }
                else
                {
                    context.FalseClass.AddFullVersionTest(this, contextExpression);
                }
                return;
            }

            RubyClass immediateClass = context.GetImmediateClassOf(target);

            // user defined instance singletons, modules, classes:
            if (immediateClass.IsSingletonClass)
            {
                AddRestriction(
                    Ast.Equal(
                        Ast.Convert(targetParameter, typeof(object)),
                        Ast.Convert(Ast.Constant(target), typeof(object))
                        )
                    );

                // we need to check for a runtime (e.g. "foo" .NET string instance could be shared accross runtimes):
                immediateClass.AddFullVersionTest(this, contextExpression);
                return;
            }

            Type type = target.GetType();

            AddTypeRestriction(type, targetParameter);

            if (typeof(IRubyObject).IsAssignableFrom(type))
            {
                // Ruby objects (get the method directly to prevent interface dispatch):
                MethodInfo classGetter = type.GetMethod("get_" + RubyObject.ClassPropertyName, BindingFlags.Public | BindingFlags.Instance);
                if (classGetter != null && classGetter.ReturnType == typeof(RubyClass))
                {
                    AddCondition(
                        // (#{type})target.Class.Version == #{immediateClass.Version}
                        Ast.Equal(
                            Ast.Call(Ast.Call(Ast.Convert(targetParameter, type), classGetter), RubyModule.VersionProperty.GetGetMethod()),
                            Ast.Constant(immediateClass.Version)
                            )
                        );
                    return;
                }

                // TODO: explicit iface-implementation
                throw new NotSupportedException("Type implementing IRubyObject should have RubyClass getter");
            }
            else
            {
                // CLR objects:
                immediateClass.AddFullVersionTest(this, contextExpression);
            }
        }
示例#5
0
        public override Expression ConvertExpression(Expression /*!*/ expr, Type /*!*/ toType, ConversionResultKind kind, Expression context)
        {
            Type fromType = expr.Type;

            if (toType == typeof(object))
            {
                if (fromType.IsValueType)
                {
                    return(Ast.Convert(expr, toType));
                }
                else
                {
                    return(expr);
                }
            }

            if (toType.IsAssignableFrom(fromType))
            {
                return(expr);
            }

            // We used to have a special case for int -> double...
            if (fromType != typeof(object) && fromType.IsValueType)
            {
                expr = Ast.Convert(expr, typeof(object));
            }

            MethodInfo fastConvertMethod = GetFastConvertMethod(toType);

            if (fastConvertMethod != null)
            {
                return(Ast.Call(fastConvertMethod, AstUtils.Convert(expr, typeof(object))));
            }

            if (typeof(Delegate).IsAssignableFrom(toType))
            {
                return(Ast.Convert(
                           Ast.Call(
                               typeof(Converter).GetMethod("ConvertToDelegate"),
                               AstUtils.Convert(expr, typeof(object)),
                               Ast.Constant(toType)
                               ),
                           toType
                           ));
            }

            Expression typeIs;
            Type       visType = CompilerHelpers.GetVisibleType(toType);

            if (toType.IsVisible)
            {
                typeIs = Ast.TypeIs(expr, toType);
            }
            else
            {
                typeIs = Ast.Call(
                    AstUtils.Convert(Ast.Constant(toType), typeof(Type)),
                    typeof(Type).GetMethod("IsInstanceOfType"),
                    AstUtils.Convert(expr, typeof(object))
                    );
            }

            return(Ast.Condition(
                       typeIs,
                       Ast.Convert(
                           expr,
                           visType),
                       Ast.Convert(
                           Ast.Call(
                               GetGenericConvertMethod(visType),
                               AstUtils.Convert(expr, typeof(object)),
                               Ast.Constant(visType.TypeHandle)
                               ),
                           visType
                           )
                       ));
        }
 public override SysExpr ToExpression() => SysExpr.TypeIs(Expression.ToExpression(), TypeOperand);
示例#7
0
        protected virtual Expression VisitTypeIs(TypeBinaryExpression b)
        {
            var expr = Visit(b.Expression);

            return(expr != b.Expression ? Expression.TypeIs(expr, b.TypeOperand) : b);
        }
示例#8
0
 /// <summary>
 /// Creates an expression to adjudicate whether a value if of a specified type
 /// </summary>
 /// <param name="value">The value to test</param>
 /// <typeparam name="T">The type to test against</typeparam>
 /// <returns></returns>
 public static TypeBinaryExpression typeIs <T>(object value)
 => XPR.TypeIs(constant(value), typeof(T));
示例#9
0
 /// <summary>
 /// Creates an expression to adjudicate whether a value if of a specified type
 /// </summary>
 /// <param name="value">The value to test</param>
 /// <param name="t">The type to test against</param>
 /// <returns></returns>
 public static TypeBinaryExpression typeIs(object value, Type t)
 => XPR.TypeIs(constant(value), t);
示例#10
0
 public static TypeBinaryExpression TypeIs(Expression expression, Type type) => Expression.TypeIs(expression, type);
示例#11
0
        private Expression VisitTypeIs(TypeBinaryExpression b)
        {
            var expr = Visit(b.Expression);

            return(expr != b.Expression ? Expression.TypeIs(expr, b.TypeOperand) : b);
        }