3

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 !

Kevin Avignon
  • 2,853
  • 3
  • 19
  • 40

1 Answers1

4

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

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
  • Basically, the OP would end up with something like this ? ((ITypeSymbol) Symbol).Type.ToDisplayString() in order to find its value @SLaks – Kevin Avignon Mar 13 '15 at 15:47
  • @KevinAvignon: Yes, except that you're missing parentheses. Also, you may want to use other properties of the resulting `ITypeSymbol`. – SLaks Mar 13 '15 at 15:48
  • Also, oops; that should be a cast to `ILocalSymbol`. Fixed. – SLaks Mar 13 '15 at 15:49
  • I don't think the OP will be so mad for the little slip haha ! I'm curious, where did you find out about this or where could I get more info on the subject ? – Kevin Avignon Mar 13 '15 at 15:50
  • @KevinAvignon: About what in particular? I found this type by looking for types derived from `ISymbol` in http://source.roslyn.io/. See also http://blog.slaks.net/2014-04-07/exploring-roslyn-part-1-introduction/ for a high-level introduction. – SLaks Mar 13 '15 at 15:51
  • Thanks mate @SLaks, I truly appreciate it. Roslyn is kind of new to me and I'm doing my best to get my head around it – Kevin Avignon Mar 13 '15 at 15:52
  • what would you do when you would be in the situation of validating two types? As in you have an assignement expression and you have a symbol for Left and Right. I would like to see if Left.Type == Right.Type, how can I do this ? – Kevin Avignon Jun 19 '15 at 21:12
  • @KevinAvignon: Are you asking whether they're equal or whether they're assignment-compatible? – SLaks Jun 19 '15 at 21:26
  • If they're equal. I want to validate redundant calls and thats a part of my algorithm – Kevin Avignon Jun 19 '15 at 23:58