简体   繁体   中英

Resolving parameter type in Roslyn

I'm using Roslyn to implement custom business rules. At the moment, I'm a bit stuck, I have to validate the type of a parameter when a method is being invoked. There are no issues validating that the method is being invoked or that is contains parameters. I have resolve an IdentifierNameSyntax by using GetSymbolInfo to get the symbol of my current syntax. It's not null and has the information I'm looking for such as the following :

CandidateReason: None
CandidateSymbols: Length = 0
Symbol: Local System.Threading.Tasks.TaskScheduler ui

When I get into the Symbol property, I want to validate that the ui variable is indeed a TaskScheduler, but it has been an unsuccessful operation for a few days.. Basically, here's the info I get when I'm in Symbol

Local System.Threading.Tasks.TaskScheduler ui
    CanBeReferencedByName: true
    ConstantValue: null
    ContainingAssembly: Assembly TestProject, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null
    ContainingNamespace: Namespace ConsoleApplication1
    ContainingSymbol: Method void ConsoleApplication1.TypeName.Test()
    ContainingType: NamedType ConsoleApplication1.TypeName
    DeclaredAccessibility: NotApplicable
    DeclaringSyntaxReferences: Length = 1
    HasConstantValue: false
    HasUnsupportedMetadata: false
    HighestPriorityUseSiteError: 2147483647
    IsAbstract: false
    IsCatch: false
    IsConst: false
    IsDefinition: true
    IsExtern: false
    IsFixed: false
    IsFor: false
    IsForEach: false
    IsImplicitlyDeclared: false
    IsOverride: false
    IsSealed: false
    IsStatic: false
    IsUsing: false
    IsVar: true
    IsVirtual: false
    Kind: Local
    Language: "C#"
    Locations: Length = 1
    MetadataName: "ui"
    Name: "ui"
    OriginalDefinition: Local System.Threading.Tasks.TaskScheduler ui
    OriginalSymbolDefinition: Local System.Threading.Tasks.TaskScheduler ui
    Type: NamedType System.Threading.Tasks.TaskScheduler
    binder: {Microsoft.CodeAnalysis.CSharp.BlockBinder}

Here's what I have so far to get the type of ui :

  • ToString() on OriginalDefinition
  • ToDisplayString() on OrigninalDefinition
  • ToString() on Symbol
  • Tried to reach type, but I can only see it in the runtime....

If needed, I can provided sample code but I cannot share the code I did. For those who might wonder, the console application is being simulated while under TDD, we have our own tool for creating unit test. But it has no impact whatsoever on this particular situation. Thanks for the help, I really appreciate it !

You need to cast the ISymbol to ILocalSymbol , which is public.
You can then use the Type property.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM