/// <summary> /// Creates a <see cref="SwitchExpression"/>. /// </summary> /// <param name="type">The result type of the switch.</param> /// <param name="switchValue">The value to be tested against each case.</param> /// <param name="defaultBody">The result of the switch if no cases are matched.</param> /// <param name="comparison">The equality comparison method to use.</param> /// <param name="cases">The valid cases for this switch.</param> /// <returns>The created <see cref="SwitchExpression"/>.</returns> public static SwitchExpression Switch(Type type, Expression switchValue, Expression defaultBody, MethodInfo comparison, IEnumerable <SwitchCase> cases) { RequiresCanRead(switchValue, nameof(switchValue)); if (switchValue.Type == typeof(void)) { throw Error.ArgumentCannotBeOfTypeVoid(); } var caseList = cases.ToReadOnly(); ContractUtils.RequiresNotNullItems(caseList, nameof(cases)); // Type of the result. Either provided, or it is type of the branches. Type resultType; if (type != null) { resultType = type; } else if (caseList.Count != 0) { resultType = caseList[0].Body.Type; } else if (defaultBody != null) { resultType = defaultBody.Type; } else { resultType = typeof(void); } bool customType = type != null; if (comparison != null) { var pms = comparison.GetParametersCached(); if (pms.Length != 2) { throw Error.IncorrectNumberOfMethodCallArguments(comparison); } // Validate that the switch value's type matches the comparison method's // left hand side parameter type. var leftParam = pms[0]; bool liftedCall = false; if (!ParameterIsAssignable(leftParam, switchValue.Type)) { liftedCall = ParameterIsAssignable(leftParam, switchValue.Type.GetNonNullableType()); if (!liftedCall) { throw Error.SwitchValueTypeDoesNotMatchComparisonMethodParameter(switchValue.Type, leftParam.ParameterType); } } var rightParam = pms[1]; foreach (var c in caseList) { ContractUtils.RequiresNotNull(c, nameof(cases)); ValidateSwitchCaseType(c.Body, customType, resultType, nameof(cases)); for (int i = 0; i < c.TestValues.Count; i++) { // When a comparison method is provided, test values can have different type but have to // be reference assignable to the right hand side parameter of the method. Type rightOperandType = c.TestValues[i].Type; if (liftedCall) { if (!rightOperandType.IsNullableType()) { throw Error.TestValueTypeDoesNotMatchComparisonMethodParameter(rightOperandType, rightParam.ParameterType); } rightOperandType = rightOperandType.GetNonNullableType(); } if (!ParameterIsAssignable(rightParam, rightOperandType)) { throw Error.TestValueTypeDoesNotMatchComparisonMethodParameter(rightOperandType, rightParam.ParameterType); } } } } else if (caseList.Count != 0) { // When comparison method is not present, all the test values must have // the same type. Use the first test value's type as the baseline. var firstTestValue = caseList[0].TestValues[0]; foreach (var c in caseList) { ContractUtils.RequiresNotNull(c, nameof(cases)); ValidateSwitchCaseType(c.Body, customType, resultType, nameof(cases)); // When no comparison method is provided, require all test values to have the same type. for (int i = 0; i < c.TestValues.Count; i++) { if (!TypeUtils.AreEquivalent(firstTestValue.Type, c.TestValues[i].Type)) { throw new ArgumentException(Strings.AllTestValuesMustHaveSameType, nameof(cases)); } } } // Now we need to validate that switchValue.Type and testValueType // make sense in an Equal node. Fortunately, Equal throws a // reasonable error, so just call it. var equal = Equal(switchValue, firstTestValue, false, comparison); // Get the comparison function from equals node. comparison = equal.Method; } if (defaultBody == null) { if (resultType != typeof(void)) { throw Error.DefaultBodyMustBeSupplied(); } } else { ValidateSwitchCaseType(defaultBody, customType, resultType, nameof(defaultBody)); } // if we have a non-boolean user-defined equals, we don't want it. if (comparison != null && comparison.ReturnType != typeof(bool)) { throw Error.EqualityMustReturnBoolean(comparison); } return(new SwitchExpression(resultType, switchValue, defaultBody, comparison, caseList)); }