Skip to content

Commit d5f20ce

Browse files
Copilotmeziantou
andcommitted
Add comprehensive CultureInsensitiveTypeAttribute documentation
Co-authored-by: meziantou <509220+meziantou@users.noreply.github.com>
1 parent 911bd92 commit d5f20ce

File tree

4 files changed

+98
-3
lines changed

4 files changed

+98
-3
lines changed
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
# CultureInsensitiveTypeAttribute
2+
3+
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.
4+
5+
## Usage
6+
7+
The attribute is available through the `Meziantou.Analyzer.Annotations` NuGet package.
8+
9+
### Marking a Type as Culture-Insensitive
10+
11+
Apply the attribute directly to a type to mark all its formats as culture-insensitive:
12+
13+
```csharp
14+
using Meziantou.Analyzer.Annotations;
15+
16+
[CultureInsensitiveType]
17+
public struct Ulid
18+
{
19+
public override string ToString() => "..."; // Culture-insensitive implementation
20+
}
21+
22+
// Usage - no warning
23+
var id = new Ulid();
24+
id.ToString(); // OK - Type is marked as culture-insensitive
25+
```
26+
27+
### Marking Only the Default Format (ToString)
28+
29+
To mark only the parameterless `ToString()` method as culture-insensitive:
30+
31+
```csharp
32+
[CultureInsensitiveType(isDefaultFormatCultureInsensitive: true)]
33+
public struct MyType
34+
{
35+
public override string ToString() => "..."; // Culture-insensitive
36+
public string ToString(string format, IFormatProvider provider) => "..."; // Still culture-sensitive
37+
}
38+
```
39+
40+
### Marking a Specific Format
41+
42+
To mark only a specific format string as culture-insensitive:
43+
44+
```csharp
45+
[CultureInsensitiveType("N")] // Only format "N" is culture-insensitive
46+
public struct MyGuid
47+
{
48+
public string ToString(string format) => format == "N" ? "..." : "...";
49+
}
50+
51+
// Usage
52+
var guid = new MyGuid();
53+
guid.ToString("N"); // OK - Format "N" is culture-insensitive
54+
guid.ToString("D"); // Warning - Format "D" is not marked as culture-insensitive
55+
```
56+
57+
### Assembly-Level Annotation for External Types
58+
59+
When you cannot modify the source type (e.g., third-party libraries), use the assembly-level attribute:
60+
61+
```csharp
62+
using Meziantou.Analyzer.Annotations;
63+
64+
// Mark all formats of Cysharp.Ulid as culture-insensitive
65+
[assembly: CultureInsensitiveType(typeof(Cysharp.Ulid))]
66+
67+
// Or mark only the default ToString() method
68+
[assembly: CultureInsensitiveType(typeof(Cysharp.Ulid), isDefaultFormatCultureInsensitive: true)]
69+
70+
// Or mark only a specific format
71+
[assembly: CultureInsensitiveType(typeof(SomeType), "N")]
72+
```
73+
74+
## Constructors
75+
76+
The attribute provides several constructors for different scenarios:
77+
78+
| Constructor | Description |
79+
|-------------|-------------|
80+
| `CultureInsensitiveType()` | Marks all formats of the type as culture-insensitive |
81+
| `CultureInsensitiveType(string? format)` | Marks the specified format as culture-insensitive |
82+
| `CultureInsensitiveType(bool isDefaultFormatCultureInsensitive)` | Marks only the default format (ToString()) as culture-insensitive when `true` |
83+
| `CultureInsensitiveType(Type type)` | Assembly-level: marks all formats of the specified type as culture-insensitive |
84+
| `CultureInsensitiveType(Type type, string? format)` | Assembly-level: marks the specified format of the type as culture-insensitive |
85+
| `CultureInsensitiveType(Type type, bool isDefaultFormatCultureInsensitive)` | Assembly-level: marks only the default format of the type as culture-insensitive when `true` |
86+
87+
## Related Rules
88+
89+
- [MA0011](Rules/MA0011.md) - IFormatProvider is missing
90+
- [MA0075](Rules/MA0075.md) - Do not use implicit culture-sensitive ToString
91+
- [MA0076](Rules/MA0076.md) - Do not use implicit culture-sensitive ToString in interpolated strings
92+
93+
## Additional Information
94+
95+
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.

docs/Rules/MA0011.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,4 +39,4 @@ MA0011.exclude_tostring_methods=true
3939
MA0011.consider_nullable_types=true
4040
````
4141

42-
You can also annotate a type with `[Meziantou.Analyzer.Annotations.CultureInsensitiveTypeAttribute]` to disable the rule for this type.
42+
You can also annotate a type with `[Meziantou.Analyzer.Annotations.CultureInsensitiveTypeAttribute]` to disable the rule for this type. See [CultureInsensitiveTypeAttribute](../CultureInsensitiveTypeAttribute.md) for more details.

docs/Rules/MA0075.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,4 +45,4 @@ MA0075.exclude_tostring_methods=true
4545
MA0075.consider_nullable_types=true
4646
````
4747

48-
You can also annotate a type with `[Meziantou.Analyzer.Annotations.CultureInsensitiveTypeAttribute]` to disable the rule for this type.
48+
You can also annotate a type with `[Meziantou.Analyzer.Annotations.CultureInsensitiveTypeAttribute]` to disable the rule for this type. See [CultureInsensitiveTypeAttribute](../CultureInsensitiveTypeAttribute.md) for more details.

docs/Rules/MA0076.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,4 @@ MA0076.exclude_tostring_methods=true
2121
MA0076.consider_nullable_types=true
2222
````
2323

24-
You can also annotate a type with `[Meziantou.Analyzer.Annotations.CultureInsensitiveTypeAttribute]` to disable the rule for this type.
24+
You can also annotate a type with `[Meziantou.Analyzer.Annotations.CultureInsensitiveTypeAttribute]` to disable the rule for this type. See [CultureInsensitiveTypeAttribute](../CultureInsensitiveTypeAttribute.md) for more details.

0 commit comments

Comments
 (0)