Skip to content

Commit 3ef8d3d

Browse files
authored
Merge pull request #12894 from dotnet/main
Merge main into live
2 parents 4385c84 + 959c0d3 commit 3ef8d3d

1,314 files changed

Lines changed: 159077 additions & 137877 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
using System;
5+
using System.Collections.Generic;
6+
using System.Linq;
7+
using System.Reflection;
8+
using System.Reflection.Context;
9+
using Xunit;
10+
11+
namespace System.Reflection.Context.Examples
12+
{
13+
#region Snippet1
14+
// A blank example attribute.
15+
class MyAttribute : Attribute
16+
{
17+
}
18+
19+
// Reflection context with custom rules.
20+
class MyCustomReflectionContext : CustomReflectionContext
21+
{
22+
// Called whenever the reflection context checks for custom attributes.
23+
protected override IEnumerable<object> GetCustomAttributes(MemberInfo member, IEnumerable<object> declaredAttributes)
24+
{
25+
// Add example attribute to "To*" members.
26+
if (member.Name.StartsWith("To", StringComparison.Ordinal))
27+
{
28+
yield return new MyAttribute();
29+
}
30+
// Keep existing attributes as well.
31+
foreach (object attr in declaredAttributes)
32+
{
33+
yield return attr;
34+
}
35+
}
36+
}
37+
#endregion Snippet1
38+
39+
public class CustomReflectionContextExamples
40+
{
41+
[Fact]
42+
public static void AddCustomAttribute()
43+
{
44+
#region Snippet2
45+
MyCustomReflectionContext mc = new();
46+
Type t = typeof(string);
47+
48+
// A representation of the type in the default reflection context.
49+
TypeInfo ti = t.GetTypeInfo();
50+
51+
// A representation of the type in the customized reflection context.
52+
TypeInfo myTI = mc.MapType(ti);
53+
54+
// Display affected members of the type ("To*") and their attributes.
55+
foreach (MemberInfo m in myTI.DeclaredMembers.Where(static m => m.Name.StartsWith("To", StringComparison.Ordinal)))
56+
{
57+
Console.WriteLine($"{m.Name}:");
58+
foreach (Attribute cd in m.GetCustomAttributes())
59+
{
60+
Console.WriteLine(cd.GetType());
61+
}
62+
}
63+
64+
Console.WriteLine();
65+
66+
// The "ToString" member as represented in the default reflection context.
67+
MemberInfo mi1 = ti.GetDeclaredMethods("ToString").FirstOrDefault();
68+
69+
Attribute[] defaultAttributes = mi1.GetCustomAttributes().ToArray();
70+
71+
// All the attributes of "ToString" in the default reflection context.
72+
Console.WriteLine("'ToString' Attributes in Default Reflection Context:");
73+
foreach (Attribute cd in defaultAttributes)
74+
{
75+
Console.WriteLine(cd.GetType());
76+
}
77+
78+
Console.WriteLine();
79+
80+
// The same member in the custom reflection context.
81+
mi1 = myTI.GetDeclaredMethods("ToString").FirstOrDefault();
82+
83+
Attribute[] customAttributes = mi1.GetCustomAttributes().ToArray();
84+
85+
// All its attributes, for comparison. MyAttribute is now included.
86+
Console.WriteLine("'ToString' Attributes in Custom Reflection Context:");
87+
foreach (Attribute cd in customAttributes)
88+
{
89+
Console.WriteLine(cd.GetType());
90+
}
91+
#endregion Snippet2
92+
93+
Assert.DoesNotContain(defaultAttributes, attribute => attribute is MyAttribute);
94+
Assert.Contains(customAttributes, attribute => attribute is MyAttribute);
95+
}
96+
}
97+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net10.0</TargetFramework>
6+
</PropertyGroup>
7+
8+
<ItemGroup>
9+
<PackageReference Include="System.Reflection.Context" Version="10.0.10" />
10+
<PackageReference Include="xunit.v3" Version="4.0.0-pre.154" />
11+
</ItemGroup>
12+
13+
</Project>
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net10.0</TargetFramework>
6+
</PropertyGroup>
7+
8+
<ItemGroup>
9+
<PackageReference Include="xunit.v3" Version="4.0.0-pre.154" />
10+
</ItemGroup>
11+
12+
</Project>
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
using System;
5+
using System.Text.RegularExpressions;
6+
using Xunit;
7+
8+
namespace System.Text.RegularExpressions.Examples
9+
{
10+
public class MatchExamples
11+
{
12+
[Fact]
13+
public static void MatchZipCode()
14+
{
15+
#region Match
16+
string input = "Zip code: 98052";
17+
var regex = new Regex(@"(?<=Zip code: )\d{5}");
18+
Match match = regex.Match(input, 5);
19+
if (match.Success)
20+
Console.WriteLine($"Match found: {match.Value}");
21+
22+
// This code prints the following output:
23+
//
24+
// Match found: 98052
25+
#endregion
26+
27+
Assert.True(match.Success);
28+
Assert.Equal("98052", match.Value);
29+
}
30+
}
31+
}

0 commit comments

Comments
 (0)