-
-
Notifications
You must be signed in to change notification settings - Fork 63
Add comprehensive CultureInsensitiveTypeAttribute documentation #922
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
Merged
Merged
Changes from 3 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
e00954d
Initial plan
Copilot 911bd92
Add CultureInsensitiveTypeAttribute to MA0011 documentation
Copilot d5f20ce
Add comprehensive CultureInsensitiveTypeAttribute documentation
Copilot 8c86412
Add NuGet link and mention attribute can be redefined locally
Copilot b471d5e
Add usage example for isDefaultFormatCultureInsensitive
Copilot f25dd7f
Replace attribute code with link to source definition
Copilot File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,95 @@ | ||
| # CultureInsensitiveTypeAttribute | ||
|
|
||
| The `CultureInsensitiveTypeAttribute` is used to mark types whose `ToString()` methods and format strings are culture-insensitive. This attribute can be used to suppress culture-related analyzer rules ([MA0011](Rules/MA0011.md), [MA0075](Rules/MA0075.md), [MA0076](Rules/MA0076.md)) for types that do not depend on the current culture. | ||
|
|
||
| ## Usage | ||
|
|
||
| The attribute is available through the `Meziantou.Analyzer.Annotations` NuGet package. | ||
|
|
||
| ### Marking a Type as Culture-Insensitive | ||
|
|
||
| Apply the attribute directly to a type to mark all its formats as culture-insensitive: | ||
|
|
||
| ```csharp | ||
| using Meziantou.Analyzer.Annotations; | ||
|
|
||
| [CultureInsensitiveType] | ||
| public struct Ulid | ||
| { | ||
| public override string ToString() => "..."; // Culture-insensitive implementation | ||
| } | ||
|
|
||
| // Usage - no warning | ||
| var id = new Ulid(); | ||
| id.ToString(); // OK - Type is marked as culture-insensitive | ||
| ``` | ||
|
|
||
| ### Marking Only the Default Format (ToString) | ||
|
|
||
| To mark only the parameterless `ToString()` method as culture-insensitive: | ||
|
|
||
| ```csharp | ||
| [CultureInsensitiveType(isDefaultFormatCultureInsensitive: true)] | ||
| public struct MyType | ||
| { | ||
| public override string ToString() => "..."; // Culture-insensitive | ||
| public string ToString(string format, IFormatProvider provider) => "..."; // Still culture-sensitive | ||
| } | ||
| ``` | ||
meziantou marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| ### Marking a Specific Format | ||
|
|
||
| To mark only a specific format string as culture-insensitive: | ||
|
|
||
| ```csharp | ||
| [CultureInsensitiveType("N")] // Only format "N" is culture-insensitive | ||
| public struct MyGuid | ||
| { | ||
| public string ToString(string format) => format == "N" ? "..." : "..."; | ||
| } | ||
|
|
||
| // Usage | ||
| var guid = new MyGuid(); | ||
| guid.ToString("N"); // OK - Format "N" is culture-insensitive | ||
| guid.ToString("D"); // Warning - Format "D" is not marked as culture-insensitive | ||
| ``` | ||
|
|
||
| ### Assembly-Level Annotation for External Types | ||
|
|
||
| When you cannot modify the source type (e.g., third-party libraries), use the assembly-level attribute: | ||
|
|
||
| ```csharp | ||
| using Meziantou.Analyzer.Annotations; | ||
|
|
||
| // Mark all formats of Cysharp.Ulid as culture-insensitive | ||
| [assembly: CultureInsensitiveType(typeof(Cysharp.Ulid))] | ||
|
|
||
| // Or mark only the default ToString() method | ||
| [assembly: CultureInsensitiveType(typeof(Cysharp.Ulid), isDefaultFormatCultureInsensitive: true)] | ||
|
|
||
| // Or mark only a specific format | ||
| [assembly: CultureInsensitiveType(typeof(SomeType), "N")] | ||
| ``` | ||
|
|
||
| ## Constructors | ||
|
|
||
| The attribute provides several constructors for different scenarios: | ||
|
|
||
| | Constructor | Description | | ||
| |-------------|-------------| | ||
| | `CultureInsensitiveType()` | Marks all formats of the type as culture-insensitive | | ||
| | `CultureInsensitiveType(string? format)` | Marks the specified format as culture-insensitive | | ||
| | `CultureInsensitiveType(bool isDefaultFormatCultureInsensitive)` | Marks only the default format (ToString()) as culture-insensitive when `true` | | ||
| | `CultureInsensitiveType(Type type)` | Assembly-level: marks all formats of the specified type as culture-insensitive | | ||
| | `CultureInsensitiveType(Type type, string? format)` | Assembly-level: marks the specified format of the type as culture-insensitive | | ||
| | `CultureInsensitiveType(Type type, bool isDefaultFormatCultureInsensitive)` | Assembly-level: marks only the default format of the type as culture-insensitive when `true` | | ||
|
|
||
| ## Related Rules | ||
|
|
||
| - [MA0011](Rules/MA0011.md) - IFormatProvider is missing | ||
| - [MA0075](Rules/MA0075.md) - Do not use implicit culture-sensitive ToString | ||
| - [MA0076](Rules/MA0076.md) - Do not use implicit culture-sensitive ToString in interpolated strings | ||
|
|
||
| ## Additional Information | ||
|
|
||
| The attribute is marked with `[Conditional("MEZIANTOU_ANALYZER_ANNOTATIONS")]`, which means it is only compiled into your assembly when the `MEZIANTOU_ANALYZER_ANNOTATIONS` compilation symbol is defined. This keeps the attribute metadata in your assembly for use by analyzers without affecting runtime behavior. | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.