VariableMustNotBeByRef() static private method

ArgumentException with message like "Variable '{0}' uses unsupported type '{1}'. Reference types are not supported for variables."
static private VariableMustNotBeByRef ( object p0, object p1, string paramName ) : Exception
p0 object
p1 object
paramName string
return Exception
Esempio n. 1
0
        // Checks that all variables are non-null, not byref, and unique.
        internal static void ValidateVariables(ReadOnlyCollection <ParameterExpression> varList, string collectionName)
        {
            if (varList.Count == 0)
            {
                return;
            }

            int count = varList.Count;
            var set   = new Set <ParameterExpression>(count);

            for (int i = 0; i < count; i++)
            {
                ParameterExpression v = varList[i];
                if (v == null)
                {
                    throw new ArgumentNullException(string.Format(System.Globalization.CultureInfo.CurrentCulture, "{0}[{1}]", collectionName, set.Count));
                }
                if (v.IsByRef)
                {
                    throw Error.VariableMustNotBeByRef(v, v.Type);
                }
                if (set.Contains(v))
                {
                    throw Error.DuplicateVariable(v);
                }
                set.Add(v);
            }
        }
Esempio n. 2
0
        /// <summary>
        /// Creates a <see cref="CatchBlock"/> representing a catch statement with the specified elements.
        /// </summary>
        /// <param name="type">The <see cref="Type"/> of <see cref="Exception"/> this <see cref="CatchBlock"/> will handle.</param>
        /// <param name="variable">A <see cref="ParameterExpression"/> representing a reference to the <see cref="Exception"/> object caught by this handler.</param>
        /// <param name="body">The body of the catch statement.</param>
        /// <param name="filter">The body of the <see cref="Exception"/> filter.</param>
        /// <returns>The created <see cref="CatchBlock"/>.</returns>
        /// <remarks><paramref name="type"/> must be non-null and match the type of <paramref name="variable"/> (if it is supplied).</remarks>
        public static CatchBlock MakeCatchBlock(Type type, ParameterExpression variable, Expression body, Expression filter)
        {
            ContractUtils.RequiresNotNull(type, nameof(type));
            ContractUtils.Requires(variable == null || TypeUtils.AreEquivalent(variable.Type, type), nameof(variable));
            if (variable == null)
            {
                TypeUtils.ValidateType(type, nameof(type));
                if (type.IsByRef)
                {
                    throw Error.TypeMustNotBeByRef(nameof(type));
                }

                if (type.IsPointer)
                {
                    throw Error.TypeMustNotBePointer(nameof(type));
                }
            }
            else if (variable.IsByRef)
            {
                throw Error.VariableMustNotBeByRef(variable, variable.Type, nameof(variable));
            }
            ExpressionUtils.RequiresCanRead(body, nameof(body));
            if (filter != null)
            {
                ExpressionUtils.RequiresCanRead(filter, nameof(filter));
                if (filter.Type != typeof(bool))
                {
                    throw Error.ArgumentMustBeBoolean(nameof(filter));
                }
            }

            return(new CatchBlock(type, variable, body, filter));
        }
        /// <summary>
        /// Creates a catch block.
        /// </summary>
        /// <param name="variables">The variables introduced by the catch block.</param>
        /// <param name="type">The type of the exceptions to handle.</param>
        /// <param name="variable">The variable to assign a caught exception to.</param>
        /// <param name="body">The body of the catch block.</param>
        /// <param name="filter">The filter to apply.</param>
        /// <returns>The created <see cref="CSharpCatchBlock"/>.</returns>
        public static CSharpCatchBlock MakeCatchBlock(IEnumerable <ParameterExpression> variables, Type type, ParameterExpression variable, Expression body, Expression filter)
        {
            var variablesList = CheckUniqueVariables(variables, nameof(variables));

            Requires(variable == null || AreEquivalent(variable.Type, type), nameof(variable));

            if (variable != null)
            {
                if (variable.IsByRef)
                {
                    throw LinqError.VariableMustNotBeByRef(variable, variable.Type);
                }

                // REVIEW: See UsingCSharpStatement for a similar situation.

                if (!variablesList.Contains(variable))
                {
                    throw Error.CatchVariableNotInScope(variable);
                }

                if (type == null)
                {
                    type = variable.Type;
                }
                else if (!AreEquivalent(type, variable.Type))
                {
                    throw Error.CatchTypeNotEquivalentWithVariableType(type, variable.Type);
                }
            }
            else if (type != null)
            {
                ValidateType(type);
            }
            else
            {
                type = typeof(object); // NB: Used to catch all.
            }

            RequiresCanRead(body, nameof(body));

            if (filter != null)
            {
                RequiresCanRead(filter, nameof(filter));

                if (filter.Type != typeof(bool))
                {
                    throw LinqError.ArgumentMustBeBoolean();
                }
            }

            return(new CSharpCatchBlock(variablesList, type, variable, body, filter));
        }
Esempio n. 4
0
        /// <summary>
        /// Creates an object holding type information about a pattern.
        /// </summary>
        /// <param name="info">The underlying type information.</param>
        /// <param name="variable">The CSharpObjectPatternInfo to assign to upon successfully matching the associated pattern.</param>
        /// <returns>A <see cref="CSharpPatternInfo" /> object holding type information about a pattern.</returns>
        public static CSharpObjectPatternInfo ObjectPatternInfo(CSharpPatternInfo info, ParameterExpression variable)
        {
            info ??= PatternInfo(typeof(object), variable?.Type ?? typeof(object));

            if (variable != null)
            {
                if (variable.Type.IsByRef)
                {
                    throw LinqError.VariableMustNotBeByRef(variable, variable.Type);
                }

                RequiresCompatiblePatternTypes(info.NarrowedType, variable.Type);
            }

            return(new CSharpObjectPatternInfo(info, variable));
        }
Esempio n. 5
0
        /// <summary>
        /// Creates a <see cref="CatchBlock"/> representing a catch statement with the specified elements.
        /// </summary>
        /// <param name="type">The <see cref="Type"/> of <see cref="Exception"/> this <see cref="CatchBlock"/> will handle.</param>
        /// <param name="variable">A <see cref="ParameterExpression"/> representing a reference to the <see cref="Exception"/> object caught by this handler.</param>
        /// <param name="body">The body of the catch statement.</param>
        /// <param name="filter">The body of the <see cref="Exception"/> filter.</param>
        /// <returns>The created <see cref="CatchBlock"/>.</returns>
        /// <remarks><paramref name="type"/> must be non-null and match the type of <paramref name="variable"/> (if it is supplied).</remarks>
        public static CatchBlock MakeCatchBlock(Type type, ParameterExpression variable, Expression body, Expression filter)
        {
            ContractUtils.RequiresNotNull(type, "type");
            ContractUtils.Requires(variable == null || TypeUtils.AreEquivalent(variable.Type, type), "variable");
            if (variable != null && variable.IsByRef)
            {
                throw Error.VariableMustNotBeByRef(variable, variable.Type);
            }
            RequiresCanRead(body, "body");
            if (filter != null)
            {
                RequiresCanRead(filter, "filter");
                if (filter.Type != typeof(bool))
                {
                    throw Error.ArgumentMustBeBoolean();
                }
            }

            return(new CatchBlock(type, variable, body, filter));
        }
Esempio n. 6
0
        // Checks that all variables are non-null, not byref, and unique.
        internal static void ValidateVariables(ReadOnlyCollection <ParameterExpression> varList, string collectionName)
        {
            int count = varList.Count;

            if (count != 0)
            {
                var set = new HashSet <ParameterExpression>();
                for (int i = 0; i < count; i++)
                {
                    ParameterExpression v = varList[i];
                    ContractUtils.RequiresNotNull(v, collectionName, i);
                    if (v.IsByRef)
                    {
                        throw Error.VariableMustNotBeByRef(v, v.Type, collectionName, i);
                    }
                    if (!set.Add(v))
                    {
                        throw Error.DuplicateVariable(v, collectionName, i);
                    }
                }
            }
        }