问题描述
我在.NET Core 3.1项目中使用roslyn,它正在解析ASP.NET解决方案。 ASP.NET解决方案正在调用函数MyTestMethod,该函数位于.Net FW 4.6.2引用的项目中。当为该函数运行以下代码时,它不会在ASP.NET解决方案中返回引用的位置。但是如果我调用相同的功能例如在测试项目中,位置已成功检测到。
var symbol = semanticModel.GetDeclaredSymbol(node);
var references = SymbolFinder.FindReferencesAsync(symbol,solution).Result;
此问题可能与Roslyn使用的.net核心有关吗? 有什么建议可以进一步测试以缩小问题根源吗?
如果有问题,我还将在Roslyn项目中使用Buildalyzer.Workspaces。
[更新] 根据诊断,我收到以下错误,但是我不确定它是否影响引用的解析,因为在测试项目中,我也收到此错误,以供参考。
C:\root\MySolution\MyBllRefProject\Bll.cs(7,14): error CS0234: The type or namespace name 'MyNamespace' does not exist in the namespace 'MyAssembly' (are you missing an assembly reference?)
C:\root\MySolution\MyBllRefProject\OtherLogic.cs(629,32): error CS0012: The type 'Object' is defined in an assembly that is not referenced. You must add a reference to assembly 'mscorlib,Version=2.0.5.0,Culture=neutral,PublicKeyToken=7cec85d7bea7798e,Retargetable=Yes'.
C:\root\MySolution\MyBllRefProject\OtherLogic.cs(629,25): error CS0012: The type 'Action<>' is defined in an assembly that is not referenced. You must add a reference to assembly 'mscorlib,Retargetable=Yes'.
C:\root\MySolution\MyBllRefProject\Config\AutoMapperBootstrap.cs(17,20): error CS0012: The type 'Object' is defined in an assembly that is not referenced. You must add a reference to assembly 'mscorlib,Retargetable=Yes'.
C:\root\MySolution\MyBllRefProject\DAL.cs(265,29): error CS0103: The name 'sqlHelper' does not exist in the current context
C:\root\MySolution\MyBllRefProject\DAL.cs(296,29): error CS0103: The name 'sqlHelper' does not exist in the current context
这是创建(简化)编译的方式
var workspace = new AnalyzerManager(Solution).GetWorkspace();
var solution = workspace.CurrentSolution;
foreach (var file in p.Documents)
{
// Get Syntax tree for file
var tree = file.GetSyntaxTreeAsync().Result;
// Get root of Syntax tree
var root = (compilationunitSyntax)tree.GetRoot();
// Get project compilation for symbolic relationship in assembly
var compilation = p.GetCompilationAsync().Result;
// Get semantic model based on project compilation and the current document tree
var semanticModel = compilation.GetSemanticModel(tree);
// Get all methods from the file
var methodReferences = getmethodReferences(solution,semanticModel,file,tree);
// do some more stuff
}
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)