APISampleUnitTestsCS.FAQ.GetTypeForVariableDeclaration C# (CSharp) Метод

GetTypeForVariableDeclaration() приватный Метод

private GetTypeForVariableDeclaration ( ) : void
Результат void
        public void GetTypeForVariableDeclaration()
        {
            var tree = SyntaxTree.ParseText(@"
            class Program
            {
            public static void Main()
            {
            var i = 0; i += 1;
            }
            }");
            var compilation = Compilation.Create("MyCompilation")
                .AddReferences(MetadataReference.CreateAssemblyReference("mscorlib"))
                .AddSyntaxTrees(tree);
            var model = compilation.GetSemanticModel(tree);

            // Get VariableDeclaratorSyntax corresponding to the statement 'var i = ...' above.
            VariableDeclaratorSyntax variableDeclarator = tree.GetRoot()
                .DescendantNodes().OfType<VariableDeclaratorSyntax>()
                .Single();

            // Get TypeSymbol corresponding to 'var i' above.
            TypeSymbol type = ((LocalSymbol)model.GetDeclaredSymbol(variableDeclarator)).Type;
            Assert.AreEqual(SpecialType.System_Int32, type.SpecialType);
            Assert.AreEqual("int", type.ToDisplayString());
        }