示例#1
0
        public static DynamicMetaObject Restrict(this DynamicMetaObject self, Type type)
        {
            ContractUtils.RequiresNotNull(self, nameof(self));
            ContractUtils.RequiresNotNull(type, nameof(type));

            if (self is IRestrictedMetaObject rmo)
            {
                return(rmo.Restrict(type));
            }

            if (type == self.Expression.Type)
            {
                if (type.IsSealed() ||
                    self.Expression.NodeType == ExpressionType.New ||
                    self.Expression.NodeType == ExpressionType.NewArrayBounds ||
                    self.Expression.NodeType == ExpressionType.NewArrayInit)
                {
                    return(self.Clone(self.Restrictions.Merge(BindingRestrictionsHelpers.GetRuntimeTypeRestriction(self.Expression, type))));
                }
            }

            if (type == typeof(DynamicNull))
            {
                return(self.Clone(
                           AstUtils.Constant(null),
                           self.Restrictions.Merge(BindingRestrictions.GetInstanceRestriction(self.Expression, null))
                           ));
            }

            Expression converted;

            // if we're converting to a value type just unbox to preserve
            // object identity.  If we're converting from Enum then we're
            // going to a specific enum value and an unbox is not allowed.
            if (type.IsValueType() && self.Expression.Type != typeof(Enum))
            {
                converted = Expression.Unbox(
                    self.Expression,
                    CompilerHelpers.GetVisibleType(type)
                    );
            }
            else
            {
                converted = AstUtils.Convert(
                    self.Expression,
                    CompilerHelpers.GetVisibleType(type)
                    );
            }

            return(self.Clone(converted, self.Restrictions.Merge(BindingRestrictionsHelpers.GetRuntimeTypeRestriction(self.Expression, type))));
        }
示例#2
0
        public DynamicMetaObject Restrict(Type type)
        {
            if (type == LimitType)
            {
                return(this);
            }

            if (HasValue)
            {
                return(new RestrictedMetaObject(
                           AstUtils.Convert(Expression, type),
                           BindingRestrictionsHelpers.GetRuntimeTypeRestriction(Expression, type),
                           Value
                           ));
            }

            return(new RestrictedMetaObject(
                       AstUtils.Convert(Expression, type),
                       BindingRestrictionsHelpers.GetRuntimeTypeRestriction(Expression, type)
                       ));
        }