private static void CreateExpressions(ExpressionSyntax ifExpression, ExpressionSyntax elseExpression, ITypeSymbol ifType, ITypeSymbol elseType, ITypeSymbol ifConvertedType, ITypeSymbol elseConvertedType, TypeSyntax typeSyntax, SemanticModel semanticModel, out ExpressionSyntax trueExpression, out ExpressionSyntax falseExpression) { var isNullable = false; if (ifConvertedType?.OriginalDefinition.SpecialType == SpecialType.System_Nullable_T) { var constValue = semanticModel.GetConstantValue(ifExpression); if (constValue.HasValue && constValue.Value == null) { trueExpression = SyntaxFactory.CastExpression(typeSyntax, ifExpression); } else { trueExpression = ifExpression; } isNullable = true; } else { trueExpression = ifExpression; } if (elseConvertedType?.OriginalDefinition.SpecialType == SpecialType.System_Nullable_T) { var constValue = semanticModel.GetConstantValue(elseExpression); if (constValue.HasValue && constValue.Value == null) { falseExpression = SyntaxFactory.CastExpression(typeSyntax, elseExpression); } else { falseExpression = elseExpression; } isNullable = true; } else { falseExpression = elseExpression; } if (!isNullable && !ifType.CanBeAssignedTo(elseType) || !elseType.CanBeAssignedTo(ifType)) { trueExpression = CastToBaseType(ifExpression, ifType, elseType, trueExpression); } }
private static void CreateExpressions(ExpressionSyntax ifExpression, ExpressionSyntax elseExpression, ITypeSymbol ifType, ITypeSymbol elseType, ITypeSymbol ifConvertedType, ITypeSymbol elseConvertedType, TypeSyntax typeSyntax, SemanticModel semanticModel, out ExpressionSyntax trueExpression, out ExpressionSyntax falseExpression) { var isNullable = false; if (ifConvertedType?.OriginalDefinition.SpecialType == SpecialType.System_Nullable_T) { var constValue = semanticModel.GetConstantValue(ifExpression); trueExpression = constValue.HasValue && constValue.Value == null ? SyntaxFactory.CastExpression(typeSyntax, ifExpression) : ifExpression; isNullable = true; } else { trueExpression = ifExpression; } if (elseConvertedType?.OriginalDefinition.SpecialType == SpecialType.System_Nullable_T) { var constValue = semanticModel.GetConstantValue(elseExpression); falseExpression = constValue.HasValue && constValue.Value == null ? SyntaxFactory.CastExpression(typeSyntax, elseExpression) : elseExpression; isNullable = true; } else { falseExpression = elseExpression; } if (!elseType.HasImplicitNumericConversion(ifType) && !IsEnumAndZero(ifType, elseExpression) && !IsEnumAndZero(elseType, ifExpression) && (!isNullable && !ifType.CanBeAssignedTo(elseType) || !elseType.CanBeAssignedTo(ifType)) && ifType != ifConvertedType) { trueExpression = CastToConvertedType(ifExpression, ifConvertedType); } }
protected virtual bool ShouldCreateConversionBetweenTypes(ITypeSymbol targetType, ITypeSymbol sourceType) { return(sourceType.CanBeAssignedTo(targetType) == false && ObjectHelper.IsSimpleType(targetType) == false && ObjectHelper.IsSimpleType(sourceType) == false); }