コード例 #1
0
        public void GenericClassNameWithoutDirectGenericNameSyntaxInLine()
        {
            var        code        = @"using System;
using System.Collections.Generic;
class C 
{
  void M()
  {
    var test = new List<int>();
    test.Add(0);
  }
}
";
            SyntaxTree tree        = CSharpSyntaxTree.ParseText(code);
            var        compilation = CSharpCompilation.Create("HelloWorld")
                                     .AddReferences(
                MetadataReference.CreateFromFile(
                    typeof(object).Assembly.Location))
                                     .AddSyntaxTrees(tree);

            var model = compilation.GetSemanticModel(tree);

            var span = tree.GetText().Lines[7].Span;
            var root = (CompilationUnitSyntax)tree.GetRoot();

            var allNodes  = root.DescendantNodesAndSelf();
            var lineNodes = root.DescendantNodesAndSelf(span);

            var nodeHelperInstance = new NodeHelpers(model);
            var genericName        = nodeHelperInstance.GetClassNameUsedAsGenericParameter(lineNodes, allNodes);

            Assert.Equal("Int32", genericName);
        }
コード例 #2
0
        private bool TryResolveExpression(ExpressionResult expressionResult, out string result)
        {
            result = null;
            switch (expressionResult.UnresolvedPart)
            {
            case UnresolvedPartType.None:
                return(false);

            case UnresolvedPartType.GenericClassName:
                result = expressionResult.Resolve(_helperInstance.GetClassNameUsedAsGenericParameter(_lineNodes, _documentNodes.Value));
                return(!string.IsNullOrEmpty(result));

            default:
                throw new InvalidOperationException("Unexpected Case");
            }
        }