APISampleUnitTestsCS.FAQ.ClassifyConversionFromAnExpressionToATypeSymbol C# (CSharp) Method

ClassifyConversionFromAnExpressionToATypeSymbol() private method

private ClassifyConversionFromAnExpressionToATypeSymbol ( ) : void
return void
        public void ClassifyConversionFromAnExpressionToATypeSymbol()
        {
            var source = @"
            using System;
            class Program
            {
            static void M()
            {
            }
            static void M(long l)
            {
            }
            static void M(short s)
            {
            }
            static void M(int i)
            {
            }
            static void Main()
            {
            int ii = 0;
            Console.WriteLine(ii);
            short jj = 1;
            Console.WriteLine(jj);
            string ss = string.Empty;
            Console.WriteLine(ss);

               // Perform conversion classification here.
            }
            }";
            var tree = SyntaxTree.ParseText(source);
            var compilation = Compilation.Create("MyCompilation")
                .AddReferences(MetadataReference.CreateAssemblyReference("mscorlib"))
                .AddSyntaxTrees(tree);
            var model = compilation.GetSemanticModel(tree);

            // Get VariableDeclaratorSyntax corresponding to variable 'ii' above.
            var variableDeclarator = (VariableDeclaratorSyntax)tree.GetRoot()
                .FindToken(source.IndexOf("ii")).Parent;

            // Get TypeSymbol corresponding to above VariableDeclaratorSyntax.
            TypeSymbol targetType = ((LocalSymbol)model.GetDeclaredSymbol(variableDeclarator)).Type;

            // Perform ClassifyConversion for expressions from within the above SyntaxTree.
            var sourceExpression1 = (ExpressionSyntax)tree.GetRoot()
                .FindToken(source.IndexOf("jj)")).Parent;
            Conversion conversion = model.ClassifyConversion(sourceExpression1, targetType);
            Assert.IsTrue(conversion.IsImplicit && conversion.IsNumeric);

            var sourceExpression2 = (ExpressionSyntax)tree.GetRoot()
                .FindToken(source.IndexOf("ss)")).Parent;
            conversion = model.ClassifyConversion(sourceExpression2, targetType);
            Assert.IsFalse(conversion.Exists);

            // Perform ClassifyConversion for constructed expressions
            // at the position identified by the comment '// Perform ...' above.
            ExpressionSyntax sourceExpression3 = Syntax.IdentifierName("jj");
            var position = source.IndexOf("//");
            conversion = model.ClassifyConversion(position, sourceExpression3, targetType);
            Assert.IsTrue(conversion.IsImplicit && conversion.IsNumeric);

            ExpressionSyntax sourceExpression4 = Syntax.IdentifierName("ss");
            conversion = model.ClassifyConversion(position, sourceExpression4, targetType);
            Assert.IsFalse(conversion.Exists);

            ExpressionSyntax sourceExpression5 = Syntax.ParseExpression("100L");
            conversion = model.ClassifyConversion(position, sourceExpression5, targetType);
            Assert.IsTrue(conversion.IsExplicit && conversion.IsNumeric);
        }