Skip to content

Commit b2a9be0

Browse files
committed
Added StringSyntaxAttribute example
1 parent 1edd8b0 commit b2a9be0

File tree

5 files changed

+78
-0
lines changed

5 files changed

+78
-0
lines changed

StringHighlighting/Program.cs

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
using System.Diagnostics.CodeAnalysis;
2+
3+
void SomeRegex([StringSyntax(StringSyntaxAttribute.Regex)] string regex) { }
4+
5+
void SomeDate([StringSyntax(StringSyntaxAttribute.DateTimeFormat)] string dateTime) { }
6+
7+
void SomeFormat(
8+
[StringSyntax(StringSyntaxAttribute.CompositeFormat)] string format,
9+
params object[] args) { }
10+
11+
SomeFormat("Hello {0} {1}", "World", "Test");
12+
SomeDate("h:mm:ss tt zz");
13+
SomeRegex("");

StringHighlighting/README.md

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# Give your strings context with StringSyntaxAttribute
2+
3+
Strings are one of the most universal data types. We use them for URLs or regular expressions or even to define some date. With .NET 7 we have a new way of giving those strings a bit of meaning. Meet `StringSyntaxAttribute`.
4+
5+
I also show you a way how to use them in .NET 6 and earlier.
6+
7+
Found [here](https://steven-giesel.com/blogPost/d2b2fc18-ca4c-4879-b87f-a1d36f435805)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<ImplicitUsings>enable</ImplicitUsings>
6+
<Nullable>enable</Nullable>
7+
<TargetFrameworks>net6.0;net7.0</TargetFrameworks>
8+
</PropertyGroup>
9+
10+
</Project>
+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "StringHighlighting", "StringHighlighting.csproj", "{DD0FC98A-EE3F-478B-AD28-75F5000F56EB}"
4+
EndProject
5+
Global
6+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
7+
Debug|Any CPU = Debug|Any CPU
8+
Release|Any CPU = Release|Any CPU
9+
EndGlobalSection
10+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
11+
{DD0FC98A-EE3F-478B-AD28-75F5000F56EB}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
12+
{DD0FC98A-EE3F-478B-AD28-75F5000F56EB}.Debug|Any CPU.Build.0 = Debug|Any CPU
13+
{DD0FC98A-EE3F-478B-AD28-75F5000F56EB}.Release|Any CPU.ActiveCfg = Release|Any CPU
14+
{DD0FC98A-EE3F-478B-AD28-75F5000F56EB}.Release|Any CPU.Build.0 = Release|Any CPU
15+
EndGlobalSection
16+
EndGlobal
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#if !NET7_0_OR_GREATER
2+
3+
// The namespace is important
4+
namespace System.Diagnostics.CodeAnalysis;
5+
6+
/// <summary>Fake version of the StringSyntaxAttribute, which was introduced in .NET 7</summary>
7+
public sealed class StringSyntaxAttribute : Attribute
8+
{
9+
/// <summary>The syntax identifier for strings containing composite formats.</summary>
10+
public const string CompositeFormat = nameof(CompositeFormat);
11+
12+
/// <summary>The syntax identifier for strings containing regular expressions.</summary>
13+
public const string Regex = nameof(Regex);
14+
15+
/// <summary>The syntax identifier for strings containing date information.</summary>
16+
public const string DateTimeFormat = nameof(DateTimeFormat);
17+
18+
/// <summary>
19+
/// Initializes a new instance of the <see cref="StringSyntaxAttribute"/> class.
20+
/// </summary>
21+
public StringSyntaxAttribute(string syntax)
22+
{
23+
}
24+
25+
/// <summary>
26+
/// Initializes a new instance of the <see cref="StringSyntaxAttribute"/> class.
27+
/// </summary>
28+
public StringSyntaxAttribute(string syntax, params object?[] arguments)
29+
{
30+
}
31+
}
32+
#endif

0 commit comments

Comments
 (0)