/// <summary> /// Creates a <see cref="ConditionalExpression"/>. /// </summary> /// <param name="test">An <see cref="Expression"/> to set the <see cref="P:ConditionalExpression.Test"/> property equal to.</param> /// <param name="ifTrue">An <see cref="Expression"/> to set the <see cref="P:ConditionalExpression.IfTrue"/> property equal to.</param> /// <param name="ifFalse">An <see cref="Expression"/> to set the <see cref="P:ConditionalExpression.IfFalse"/> property equal to.</param> /// <returns>A <see cref="ConditionalExpression"/> that has the <see cref="P:Expression.NodeType"/> property equal to /// <see cref="F:ExpressionType.Conditional"/> and the <see cref="P:ConditionalExpression.Test"/>, <see cref="P:ConditionalExpression.IfTrue"/>, /// and <see cref="P:ConditionalExpression.IfFalse"/> properties set to the specified values.</returns> public static ConditionalExpression Condition(Expression test, Expression ifTrue, Expression ifFalse) { RequiresCanRead(test, "test"); RequiresCanRead(ifTrue, "ifTrue"); RequiresCanRead(ifFalse, "ifFalse"); if (test.Type != typeof(bool)) { throw Error.ArgumentMustBeBoolean(); } if (!TypeUtils.AreEquivalent(ifTrue.Type, ifFalse.Type)) { throw Error.ArgumentTypesMustMatch(); } return(ConditionalExpression.Make(test, ifTrue, ifFalse, ifTrue.Type)); }
/// <summary> /// Creates a <see cref="ConditionalExpression" />. /// </summary> /// <param name="test">An <see cref="Expression" /> to set the <see cref="ConditionalExpression.Test" /> property equal to.</param> /// <param name="ifTrue"> /// An <see cref="Expression" /> to set the <see cref="ConditionalExpression.IfTrue" /> property equal /// to. /// </param> /// <param name="ifFalse"> /// An <see cref="Expression" /> to set the <see cref="ConditionalExpression.IfFalse" /> property /// equal to. /// </param> /// <returns> /// A <see cref="ConditionalExpression" /> that has the <see cref="NodeType" /> property equal to /// <see cref="ExpressionType.Conditional" /> and the <see cref="ConditionalExpression.Test" />, /// <see cref="ConditionalExpression.IfTrue" />, /// and <see cref="ConditionalExpression.IfFalse" /> properties set to the specified values. /// </returns> public static ConditionalExpression Condition(Expression test, Expression ifTrue, Expression ifFalse) { ExpressionUtils.RequiresCanRead(test, nameof(test)); ExpressionUtils.RequiresCanRead(ifTrue, nameof(ifTrue)); ExpressionUtils.RequiresCanRead(ifFalse, nameof(ifFalse)); if (test.Type != typeof(bool)) { throw new ArgumentException("Argument must be boolean", nameof(test)); } if (!TypeUtils.AreEquivalent(ifTrue.Type, ifFalse.Type)) { throw new ArgumentException("Argument types do not match"); } return(ConditionalExpression.Make(test, ifTrue, ifFalse, ifTrue.Type)); }
/// <summary> /// Creates a <see cref="ConditionalExpression" />. /// </summary> /// <param name="test">An <see cref="Expression" /> to set the <see cref="ConditionalExpression.Test" /> property equal to.</param> /// <param name="ifTrue"> /// An <see cref="Expression" /> to set the <see cref="ConditionalExpression.IfTrue" /> property equal /// to. /// </param> /// <param name="ifFalse"> /// An <see cref="Expression" /> to set the <see cref="ConditionalExpression.IfFalse" /> property /// equal to. /// </param> /// <param name="type">A <see cref="Type" /> to set the <see cref="Type" /> property equal to.</param> /// <returns> /// A <see cref="ConditionalExpression" /> that has the <see cref="NodeType" /> property equal to /// <see cref="ExpressionType.Conditional" /> and the <see cref="ConditionalExpression.Test" />, /// <see cref="ConditionalExpression.IfTrue" />, /// and <see cref="ConditionalExpression.IfFalse" /> properties set to the specified values. /// </returns> /// <remarks> /// This method allows explicitly unifying the result type of the conditional expression in cases where the types of /// <paramref name="ifTrue" /> /// and <paramref name="ifFalse" /> expressions are not equal. Types of both <paramref name="ifTrue" /> and /// <paramref name="ifFalse" /> must be implicitly /// reference assignable to the result type. The <paramref name="type" /> is allowed to be <see cref="System.Void" />. /// </remarks> public static ConditionalExpression Condition(Expression test, Expression ifTrue, Expression ifFalse, Type type) { ExpressionUtils.RequiresCanRead(test, nameof(test)); ExpressionUtils.RequiresCanRead(ifTrue, nameof(ifTrue)); ExpressionUtils.RequiresCanRead(ifFalse, nameof(ifFalse)); ContractUtils.RequiresNotNull(type, nameof(type)); if (test.Type != typeof(bool)) { throw new ArgumentException("Argument must be boolean", nameof(test)); } if (type != typeof(void) && (!type.IsReferenceAssignableFromInternal(ifTrue.Type) || !type.IsReferenceAssignableFromInternal(ifFalse.Type))) { throw new ArgumentException("Argument types do not match"); } return(ConditionalExpression.Make(test, ifTrue, ifFalse, type)); }
/// <summary> /// Creates a <see cref="ConditionalExpression"/>. /// </summary> /// <param name="test">An <see cref="Expression"/> to set the <see cref="P:ConditionalExpression.Test"/> property equal to.</param> /// <param name="ifTrue">An <see cref="Expression"/> to set the <see cref="P:ConditionalExpression.IfTrue"/> property equal to.</param> /// <param name="ifFalse">An <see cref="Expression"/> to set the <see cref="P:ConditionalExpression.IfFalse"/> property equal to.</param> /// <param name="type">A <see cref="Type"/> to set the <see cref="P:Expression.Type"/> property equal to.</param> /// <returns>A <see cref="ConditionalExpression"/> that has the <see cref="P:Expression.NodeType"/> property equal to /// <see cref="F:ExpressionType.Conditional"/> and the <see cref="P:ConditionalExpression.Test"/>, <see cref="P:ConditionalExpression.IfTrue"/>, /// and <see cref="P:ConditionalExpression.IfFalse"/> properties set to the specified values.</returns> /// <remarks>This method allows explicitly unifying the result type of the conditional expression in cases where the types of <paramref name="ifTrue"/> /// and <paramref name="ifFalse"/> expressions are not equal. Types of both <paramref name="ifTrue"/> and <paramref name="ifFalse"/> must be implicitly /// reference assignable to the result type. The <paramref name="type"/> is allowed to be <see cref="System.Void"/>.</remarks> public static ConditionalExpression Condition(Expression test, Expression ifTrue, Expression ifFalse, Type type) { RequiresCanRead(test, "test"); RequiresCanRead(ifTrue, "ifTrue"); RequiresCanRead(ifFalse, "ifFalse"); ContractUtils.RequiresNotNull(type, "type"); if (test.Type != typeof(bool)) { throw Error.ArgumentMustBeBoolean(); } if (type != typeof(void)) { if (!TypeUtils.AreReferenceAssignable(type, ifTrue.Type) || !TypeUtils.AreReferenceAssignable(type, ifFalse.Type)) { throw Error.ArgumentTypesMustMatch(); } } return(ConditionalExpression.Make(test, ifTrue, ifFalse, type)); }
/// <summary> /// Creates a <see cref="ConditionalExpression"/>. /// </summary> /// <param name="test">An <see cref="Expression"/> to set the <see cref="ConditionalExpression.Test"/> property equal to.</param> /// <param name="ifTrue">An <see cref="Expression"/> to set the <see cref="ConditionalExpression.IfTrue"/> property equal to.</param> /// <param name="ifFalse">An <see cref="Expression"/> to set the <see cref="ConditionalExpression.IfFalse"/> property equal to.</param> /// <param name="type">A <see cref="Type"/> to set the <see cref="Type"/> property equal to.</param> /// <returns>A <see cref="ConditionalExpression"/> that has the <see cref="NodeType"/> property equal to /// <see cref="ExpressionType.Conditional"/> and the <see cref="ConditionalExpression.Test"/>, <see cref="ConditionalExpression.IfTrue"/>, /// and <see cref="ConditionalExpression.IfFalse"/> properties set to the specified values.</returns> /// <remarks>This method allows explicitly unifying the result type of the conditional expression in cases where the types of <paramref name="ifTrue"/> /// and <paramref name="ifFalse"/> expressions are not equal. Types of both <paramref name="ifTrue"/> and <paramref name="ifFalse"/> must be implicitly /// reference assignable to the result type. The <paramref name="type"/> is allowed to be <see cref="Void"/>.</remarks> public static ConditionalExpression Condition(Expression test, Expression ifTrue, Expression ifFalse, Type type) { ExpressionUtils.RequiresCanRead(test, nameof(test)); ExpressionUtils.RequiresCanRead(ifTrue, nameof(ifTrue)); ExpressionUtils.RequiresCanRead(ifFalse, nameof(ifFalse)); ArgumentNullException.ThrowIfNull(type); if (test.Type != typeof(bool)) { throw Error.ArgumentMustBeBoolean(nameof(test)); } if (type != typeof(void)) { if (!TypeUtils.AreReferenceAssignable(type, ifTrue.Type) || !TypeUtils.AreReferenceAssignable(type, ifFalse.Type)) { throw Error.ArgumentTypesMustMatch(); } } return(ConditionalExpression.Make(test, ifTrue, ifFalse, type)); }