Exemplo n.º 1
0
        private ExpressionSyntax AddExplicitConversion(Microsoft.CodeAnalysis.VisualBasic.Syntax.ExpressionSyntax vbNode, ExpressionSyntax csNode,
                                                       ITypeSymbol vbConvertedType, TypeConversionKind conversionKind, bool addParenthesisIfNeeded)
        {
            switch (conversionKind)
            {
            case TypeConversionKind.Unknown:
            case TypeConversionKind.Identity:
                return(addParenthesisIfNeeded ? VbSyntaxNodeExtensions.ParenthesizeIfPrecedenceCouldChange(vbNode, csNode) : csNode);

            case TypeConversionKind.Implicit:
                return(ValidSyntaxFactory.CastExpression(_semanticModel.GetCsTypeSyntax(vbConvertedType, vbNode), csNode));

            case TypeConversionKind.Explicit:
                return(AddExplicitConvertTo(vbNode, csNode, vbConvertedType));

            default:
                throw new ArgumentOutOfRangeException();
            }
        }
        private TypeSyntax GetTypeSyntax(VariableDeclaratorSyntax declarator, bool preferExplicitType)
        {
            if (!preferExplicitType)
            {
                return(CreateVarTypeName());
            }

            var typeInf = _semanticModel.GetTypeInfo(declarator.Initializer.Value);

            if (typeInf.ConvertedType == null)
            {
                return(CreateVarTypeName());
            }

            return(_semanticModel.GetCsTypeSyntax(typeInf.ConvertedType, declarator));
        }