-
Notifications
You must be signed in to change notification settings - Fork 4.1k
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
Comments
Tagging @OmarTawfik @VSadov for this question |
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 This is not an ideal solution, but might be sufficient for your scenario. |
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. |
I've hit the lack of (In the meantime, if anyone else stumbles upon this issue, here's an implementation of the workaround.) |
@jaredpar doing some clean up, and apparently I cannot reassign my bugs if I'm no longer an owner here :( |
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. |
The API should ship in dev16.1 preview 2. |
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? I have the following NuGet packages in my Analyzer project.
Thanks, Fons |
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. |
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 |
FYI 3.1.0-beta2-final is now available. Thanks @JoeRobich. https://www.nuget.org/packages/Microsoft.CodeAnalysis.CSharp.Workspaces/3.1.0-beta2-final |
Thanks a lot. It just tested the 3.1.0-beta2-final. It works great. |
Excellent. I'll consider this issue resolved. Feel free to reopen if you have any issues with this API. |
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
The text was updated successfully, but these errors were encountered: