API proposal: Introduce a strongly-typed diagnostic properties
See original GitHub issueBackground and Motivation
Communication between analyzers and their corresponding codefixes is a very common scenario. Currently there is Diagnostic.Properties
property that we can set via the properties
parameter on Diagnostic.Create
method. However, the existing Properties
is a dictionary of string, string. This is often not sufficient.
Proposed API
Option 1
namespace Microsoft.CodeAnalysis
{
+ public abstract class Diagnostic<TKey, TValue> : Diagnostic, IEquatable<Diagnostic<TKey, TValue>>, IFormattable
+ {
+ public ImmutableDictionary<TKey, TValue> GenericProperties { get; } // Or, TypedProperties, or some other suitable name
+ }
}
Option 2
namespace Microsoft.CodeAnalysis
{
public abstract class Diagnostic : IEquatable<Diagnostic>, IFormattable
{
+ public ImmutableDictionary<object, object> ObjectProperties { get; } // Possibly need to find a better name
}
}
Usage Examples
// Analyzer code
context.ReportDiagnostic(Diagnostic<string, ISymbol>.Create(...));
// Codefix
if (context.Diagnostics[0] is Diagnostic<string, ISymbol> diagnostic)
{
// Do work using diagnostic.GenericProperties
}
Alternative Designs
Risks
Issue Analytics
- State:
- Created 2 years ago
- Comments:7 (7 by maintainers)
Top Results From Across the Web
System.Diagnostics.Activity: Enumeration API [Iteration 2] · ...
First proposal: #67072 Background and motivation One of the main things ... the contract strongly typed by introducing some new properties.
Read more >ECMAScript proposal: Type Annotations
This proposal aims to enable developers to add type annotations to their JavaScript code, allowing those annotations to be checked by a type...
Read more >A Proposal For Type Syntax in JavaScript - TypeScript
Today we're excited to announce our support and collaboration on a new Stage 0 proposal to bring optional and erasable type syntax to ......
Read more >Announcing 'Machine Learning .NET' 0.5
Specifically, the new ML.NET API offers attractive features which aren't possible with the current LearningPipeline API: Strongly-typed API: ...
Read more >Zalando RESTful API and Event Guidelines
The purpose of our "RESTful API guidelines" is to define standards to successfully establish "consistent API look and feel" quality.
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
It itself is not serialized (and being an internal member and not part of the public api), cannot be observed.
I’m def not sure. The benefit of string/string is that it forces the type (and the users) to be able to put in data that is sure to serialize and sure to act as a pure value. It would need a very strong API and design to no longer have that. 😃
It’s not exposed publicly, but I thought serialization would includes private members as well?
Anyway, is there some way to overcome those concerns (even by a different proposal)? or it’s simply “impossible”?