我想得到由var
.
var localDeclaration = methodDeclaration.DescendantNodes().OfType<LocalDeclarationStatementSyntax>().First();
var identifierName = localDeclaration.DescendantNodes().OfType<IdentifierNameSyntax>().First();
var symbolInfo = semanticModel.GetSymbolInfo(identifierName);
var typeSymbol = semanticModel.GetTypeInfo(identifierName).Type;
我试过了GetSymbolInfo()
,GetTypeInfo()
第一个返回null
,第二个返回{?}
。
使用 Roslyn 获取“var”类型 它提供的方法也不起作用。下图附上调试信息。可以看到这些值都是 null 或者 {?}。
我的代码看起来像这样
public class Start
{
static void Main(string[] args)
{
InitEnvironment();
Run();
Console.ReadLine();
}
private static void InitEnvironment()
{
GlobalVariable.MySolution = MSBuildWorkspace.Create().OpenSolutionAsync(ConstVariable.SolutionPath).Result;
GlobalVariable.MyProject = GlobalVariable.MySolution.Projects.First(x => x.Name == "GamePlay");
GlobalVariable.CompilationByAll = GlobalVariable.MyProject.GetCompilationAsync().Result;
}
private static void Run()
{
var tree = SyntaxTreeProcessor.GetSyntaxTreeByFileName(ConstVariable.SystemPath + @"\GameModeSystems\JokerMode\JokerProcessPlayerStatisSystems.cs");
var semanticModel = GlobalVariable.CompilationByAll.GetSemanticModel(tree);
var methodDeclarations = tree.GetRoot().DescendantNodes().OfType<MethodDeclarationSyntax>().ToList();
foreach(var methodDeclaration in methodDeclarations)
{
Console.WriteLine($"methodDeclaration:{methodDeclaration.Identifier.ValueText}");
var localDeclaration = methodDeclaration.DescendantNodes().OfType<LocalDeclarationStatementSyntax>().First();
var identifierName = localDeclaration.DescendantNodes().OfType<IdentifierNameSyntax>().First();
foreach (var varibale in localDeclaration.Declaration.Variables)
{
Console.WriteLine($"varibale.Identifier:{varibale.Identifier}");
Console.WriteLine($"varibale.Initializer:{varibale.Initializer}");
}
var variableType = semanticModel.GetSymbolInfo(localDeclaration.Declaration.Type).Symbol;
var typeSymbol = semanticModel.GetTypeInfo(identifierName).Type;
var symbolInfo = semanticModel.GetSymbolInfo(identifierName);
}
Console.WriteLine(tree.FilePath);
}
}
我在 Visual Studio 2019 中尝试了 Syntax Visualizer 工具,它显示了正确的类型。
GetDiagnostic() 的结果如下:
COUNT: 32
I:\gameplay\Test\GamePlay\System\GameModeSystems\JokerMode\JokerProcessPlayerStatisSystems.cs: (0,6)-(0,12)未能找到类型或命名空间名“System”(是否缺少 using 指令或程序集引用?)
I:\gameplay\Test\GamePlay\System\GameModeSystems\JokerMode\JokerProcessPlayerStatisSystems.cs: (1,6)-(1,12)未能找到类型或命名空间名“System”(是否缺少 using 指令或程序集引用?)
I:\gameplay\Test\GamePlay\System\GameModeSystems\JokerMode\JokerProcessPlayerStatisSystems.cs: (2,6)-(2,12)未能找到类型或命名空间名“System”(是否缺少 using 指令或程序集引用?)
I:\gameplay\Test\GamePlay\System\GameModeSystems\JokerMode\JokerProcessPlayerStatisSystems.cs: (3,6)-(3,12)未能找到类型或命名空间名“System”(是否缺少 using 指令或程序集引用?)
I:\gameplay\Test\GamePlay\System\GameModeSystems\JokerMode\JokerProcessPlayerStatisSystems.cs: (6,6)-(6,13)未能找到类型或命名空间名“Entitas”(是否缺少 using 指令或程序集引用?)
I:\gameplay\Test\GamePlay\System\GameModeSystems\JokerMode\JokerProcessPlayerStatisSystems.cs: (10,111)-(10,128)未能找到类型或命名空间名“IInitializeSystem”(是否缺少 using 指令或程序集引用?)
I:\gameplay\Test\GamePlay\System\GameModeSystems\JokerMode\JokerProcessPlayerStatisSystems.cs: (14,15)-(14,19)预定义类型“System.Void”未定义或导入
I:\gameplay\Test\GamePlay\System\GameModeSystems\JokerMode\JokerProcessPlayerStatisSystems.cs: (20,54)-(20,60)预定义类型“System.Object”未定义或导入
I:\gameplay\Test\GamePlay\System\GameModeSystems\JokerMode\JokerProcessPlayerStatisSystems.cs: (20,54)-(20,60)未能找到类型或命名空间名“Entity”(是否缺少 using 指令或程序集引用?)
I:\gameplay\Test\GamePlay\System\GameModeSystems\JokerMode\JokerProcessPlayerStatisSystems.cs: (20,79)-(20,97)预定义类型“System.Object”未定义或导入
I:\gameplay\Test\GamePlay\System\GameModeSystems\JokerMode\JokerProcessPlayerStatisSystems.cs: (20,27)-(20,31)预定义类型“System.Void”未定义或导入
I:\gameplay\Test\GamePlay\System\GameModeSystems\JokerMode\JokerProcessPlayerStatisSystems.cs: (12,16)-(12,41)预定义类型“System.Object”未定义或导入
I:\gameplay\Test\GamePlay\System\GameModeSystems\JokerMode\JokerProcessPlayerStatisSystems.cs: (16,24)-(16,32)预定义类型“System.Object”未定义或导入
I:\gameplay\Test\GamePlay\System\GameModeSystems\JokerMode\JokerProcessPlayerStatisSystems.cs: (16,24)-(16,32)当前上下文中不存在名称“GetWorld”
I:\gameplay\Test\GamePlay\System\GameModeSystems\JokerMode\JokerProcessPlayerStatisSystems.cs: (17,60)-(17,85)预定义类型“System.Object”未定义或导入
I:\gameplay\Test\GamePlay\System\GameModeSystems\JokerMode\JokerProcessPlayerStatisSystems.cs: (22,24)-(22,32)预定义类型“System.Object”未定义或导入
I:\gameplay\Test\GamePlay\System\GameModeSystems\JokerMode\JokerProcessPlayerStatisSystems.cs: (22,24)-(22,32)当前上下文中不存在名称“GetWorld”
I:\gameplay\Test\GamePlay\System\GameModeSystems\JokerMode\JokerProcessPlayerStatisSystems.cs: (23,12)-(23,42)预定义类型“System.Object”未定义或导入
I:\gameplay\Test\GamePlay\System\GameModeSystems\JokerMode\JokerProcessPlayerStatisSystems.cs: (23,53)-(23,83)预定义类型“System.Object”未定义或导入
I:\gameplay\Test\GamePlay\System\GameModeSystems\JokerMode\JokerProcessPlayerStatisSystems.cs: (23,53)-(23,83)预定义类型“System.Void”未定义或导入
I:\gameplay\Test\GamePlay\System\GameModeSystems\JokerMode\JokerProcessPlayerStatisSystems.cs: (24,65)-(24,95)预定义类型“System.Object”未定义或导入
I:\gameplay\Test\GamePlay\System\GameModeSystems\JokerMode\JokerProcessPlayerStatisSystems.cs: (24,97)-(24,101)预定义类型“System.Object”未定义或导入
I:\gameplay\Test\GamePlay\System\GameModeSystems\JokerMode\JokerProcessPlayerStatisSystems.cs: (24,97)-(24,101)未能找到类型或命名空间名“Game”(是否缺少 using 指令或程序集引用?)
I:\gameplay\Test\GamePlay\System\GameModeSystems\JokerMode\JokerProcessPlayerStatisSystems.cs: (26,45)-(26,75)预定义类型“System.Object”未定义或导入
I:\gameplay\Test\GamePlay\System\GameModeSystems\JokerMode\JokerProcessPlayerStatisSystems.cs: (26,77)-(26,81)预定义类型“System.Object”未定义或导入
I:\gameplay\Test\GamePlay\System\GameModeSystems\JokerMode\JokerProcessPlayerStatisSystems.cs: (26,77)-(26,81)未能找到类型或命名空间名“Game”(是否缺少 using 指令或程序集引用?)
I:\gameplay\Test\GamePlay\System\GameModeSystems\JokerMode\JokerProcessPlayerStatisSystems.cs: (32,12)-(32,30)预定义类型“System.Object”未定义或导入
I:\gameplay\Test\GamePlay\System\GameModeSystems\JokerMode\JokerProcessPlayerStatisSystems.cs: (34,16)-(34,107)预定义类型“System.Boolean”未定义或导入
I:\gameplay\Test\GamePlay\System\GameModeSystems\JokerMode\JokerProcessPlayerStatisSystems.cs: (38,16)-(38,30)预定义类型“System.Object”未定义或导入
I:\gameplay\Test\GamePlay\System\GameModeSystems\JokerMode\JokerProcessPlayerStatisSystems.cs: (38,31)-(38,40)预定义类型“System.Object”未定义或导入
I:\gameplay\Test\GamePlay\System\GameModeSystems\JokerMode\JokerProcessPlayerStatisSystems.cs: (38,16)-(38,47)预定义类型“System.Boolean”未定义或导入
I:\gameplay\Test\GamePlay\System\GameModeSystems\JokerMode\JokerProcessPlayerStatisSystems.cs: (40,20)-(40,111)预定义类型“System.Boolean”未定义或导入
methodDeclaration:Initialize
varibale.Identifier:world
varibale.Initializer:= GetWorld()
methodDeclaration:HandleReliableRequest
varibale.Identifier:world
varibale.Initializer:= GetWorld()
I:\gameplay\Test\GamePlay\System\GameModeSystems\JokerMode\JokerProcessPlayerStatisSystems.cs