Collectives™ on Stack Overflow
Find centralized, trusted content and collaborate around the technologies you use most.
Learn more about Collectives
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
Learn more about Teams
I want to get the type represented by
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;
I tried GetSymbolInfo()
and GetTypeInfo()
, the first one returns null
, the second one returns {?}
.
Get Type of 'var' with Roslyn
The method it provides does not work either. The debugging information is attached in the figure below. You can see that these values are all null or {?}.
My code looks like this
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);
I tried the Syntax Visualizer tool in visual studio 2019 and it showed the correct type.
The result of GetDiagnostic() is as follows:
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
–
–
I seem to have found a solution. I put it here for people with the same confusion. If you have a better way, you are welcome to write it down below. thanks.
private static void InitEnvironment()
GlobalVariable.MySolution = MSBuildWorkspace.Create().OpenSolutionAsync(ConstVariable.SolutionPath).Result;
var metadataReferenceCustomDlls = Directory.GetFiles(@"I:\gameplay\Library\", "*.dll");
var metadataReferences = metadataReferenceCustomDlls.Select(f => MetadataReference.CreateFromFile(f)).ToList();
var metadataReferenceNetStandardDlls = Directory.GetFiles(@"C:\Users\user\.nuget\packages\netstandard.library\2.0.3\build\netstandard2.0\ref\", "*.dll");
var metadataReferencesB = metadataReferenceNetStandardDlls.Select(f => MetadataReference.CreateFromFile(f)).ToList();
metadataReferences = metadataReferences.Concat(metadataReferencesB).ToList();
foreach (var projectId in GlobalVariable.MySolution.ProjectIds.ToArray())
GlobalVariable.MySolution = GlobalVariable.MySolution.AddMetadataReferences(projectId, metadataReferences);
GlobalVariable.MyProject = GlobalVariable.MySolution.Projects.First(x => x.Name == "GamePlay");
GlobalVariable.CompilationByAll = GlobalVariable.MyProject.GetCompilationAsync().Result;
I added some code in InitEnvironment().In general, the corresponding reference is added.
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.