Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Question] How can I check if a ITypeSymbol is a readonly struct? #23792

Closed
sonnemaf opened this issue Dec 14, 2017 · 13 comments
Closed

[Question] How can I check if a ITypeSymbol is a readonly struct? #23792

sonnemaf opened this issue Dec 14, 2017 · 13 comments
Assignees
Labels
Area-Compilers Concept-API This issue involves adding, removing, clarification, or modification of an API. Question
Milestone

Comments

@sonnemaf
Copy link

I'm trying to create a Roslyn Analyzer which checks whether an InParameter is defined on a non-readonly valuetype (from C# 7.2). I can't find a way to check if an ITypeSymbol is readonly.

I tried the GetAttributes() method. I expected to have the IsReadOnlyAttribute but it doesn't. It is empty.

private static bool IsReadonly(ITypeSymbol type) { return type.GetAttributes().Any(a => a.AttributeClass.GetType().FullName == "System.Runtime.CompilerServices.IsReadOnlyAttribute"); }

Any suggestions?

I published my code on: Gist

@jcouv
Copy link
Member

jcouv commented Dec 14, 2017

Tagging @OmarTawfik @VSadov for this question

@jcouv jcouv added this to the 15.7 milestone Dec 14, 2017
@VSadov
Copy link
Member

VSadov commented Jan 8, 2018

I do not think there is a public API to retrieve that. (We should add that).

If this is a blocking issue, you may try using SymbolDisplay.ToDisplayParts. I think it will put a readonly display part for readonly structs.

This is not an ideal solution, but might be sufficient for your scenario.

@sonnemaf
Copy link
Author

sonnemaf commented Jan 9, 2018

I have tried the SymbolDisplay.ToDisplayParts() solution but it doesn't return any readonly display part. I hope you will add a public 'IsReadonly' API soon. For now I'm stuck.

@jcouv jcouv added the Concept-API This issue involves adding, removing, clarification, or modification of an API. label Feb 25, 2018
@jaredpar jaredpar modified the milestones: 15.7, 15.8 Mar 23, 2018
@HellBrick
Copy link

I've hit the lack of ITypeSymbol.IsReadOnly too. As a workaround, it's possible to use reflection / code generation to get the value of internal TypeSymbol.IsReadOnly property, but it would definitely be nice to have this exposed publicly and avoid relying on Roslyn internals.

(In the meantime, if anyone else stumbles upon this issue, here's an implementation of the workaround.)

@OmarTawfik
Copy link
Contributor

@jaredpar doing some clean up, and apparently I cannot reassign my bugs if I'm no longer an owner here :(
Want to make sure this is not forgotten. can you please triage accordingly?

@sonnemaf
Copy link
Author

sonnemaf commented Apr 2, 2019

Does anybody know when I can use the ITypeSymbol.IsReadOnly property in my Analyzer. Will it be shipped in the Microsoft.CodeAnalysis NuGet package soon? I can't wait to have it.

@RikkiGibson
Copy link
Contributor

The API should ship in dev16.1 preview 2.

@sonnemaf
Copy link
Author

@RikkiGibson

I have just installed VS2019 Preview 2 and update the NuGet packages to the latest (preview) versions. I can still not find the IsReadOnly property on ITypeSymbol. Can you tell me what I'm doing wrong?

image

I have the following NuGet packages in my Analyzer project.

  <ItemGroup>
    <PackageReference Include="Microsoft.CodeAnalysis.Analyzers" Version="2.9.2" PrivateAssets="all" />
    <PackageReference Include="Microsoft.CodeAnalysis.CSharp.Workspaces" Version="3.1.0-beta1-final" PrivateAssets="all" />
    <PackageReference Update="NETStandard.Library" PrivateAssets="all" />
  </ItemGroup>

Thanks,

Fons

@RikkiGibson
Copy link
Contributor

RikkiGibson commented Apr 24, 2019

Roslyn version 3.1.0 beta 2 corresponds to VS 16.1 preview2. So if you update your CSharp.CodeAnalysis packages to that you should start seeing it.

@RikkiGibson
Copy link
Contributor

Looks like this version isn't on NuGet yet. I would expect 3.1.0-beta2-final to get added there very soon.

For the immediate term you can try including this MyGet version in your project: https://dotnet.myget.org/feed/roslyn/package/nuget/Microsoft.CodeAnalysis.CSharp.Workspaces/3.1.0-beta2-19223-12

@RikkiGibson
Copy link
Contributor

@sonnemaf
Copy link
Author

Thanks a lot. It just tested the 3.1.0-beta2-final. It works great.

@RikkiGibson
Copy link
Contributor

Excellent. I'll consider this issue resolved. Feel free to reopen if you have any issues with this API.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area-Compilers Concept-API This issue involves adding, removing, clarification, or modification of an API. Question
Projects
None yet
Development

No branches or pull requests

7 participants