【发布时间】:2023-03-05 06:56:02
【问题描述】:
我有一段这样的代码:
using System;
using System.Linq;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
namespace TestProject
{
class Program
{
private const string text = @"
using System;
class Program
{
static void Main(string[] args)
{
if (args == null) { }
if (args != null) { }
}
}";
static void Main(string[] args)
{
var tree = CSharpSyntaxTree.ParseText(text);
var compilation = CSharpCompilation.Create(null).AddSyntaxTrees(tree);
var semanticModel = compilation.GetSemanticModel(tree);
var root = tree.GetRoot();
foreach (var param in root.DescendantNodes().OfType<ParameterSyntax>().ToList())
{
var type = param.Type;
var name = param.Identifier;
var typeSymbol = semanticModel.GetTypeInfo(param).Type;
}
}
}
}
我的目标是能够检查一个类型是否是引用类型。我想这样做
typeSymbol.IsReferenceType
但问题是semanticModel.GetTypeInfo(param).Type 返回null。在同一个param.Type 返回正确的值(string[])。
任何想法为什么它会以这种方式工作以及如何修复它?
【问题讨论】:
标签: c# .net roslyn roslyn-code-analysis