Skip to content

Commit 75b72af

Browse files
authored
Merge pull request #459 from microsoft/refixCG
Re-apply System.Text.Encodings.Web update
2 parents 4296338 + 5876040 commit 75b72af

File tree

3 files changed

+64
-0
lines changed

3 files changed

+64
-0
lines changed
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
3+
4+
namespace Microsoft.Windows.CsWin32
5+
{
6+
using System;
7+
using System.Collections.Generic;
8+
using System.IO;
9+
using System.Linq;
10+
using System.Reflection;
11+
using System.Runtime.CompilerServices;
12+
using System.Runtime.InteropServices;
13+
14+
internal static class BindingRedirects
15+
{
16+
private static readonly string SourceGeneratorAssemblyDirectory = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
17+
private static readonly Lazy<Dictionary<string, string>> LocalAssemblies;
18+
19+
static BindingRedirects()
20+
{
21+
LocalAssemblies = new Lazy<Dictionary<string, string>>(
22+
() => Directory.GetFiles(SourceGeneratorAssemblyDirectory, "*.dll").ToDictionary(Path.GetFileNameWithoutExtension, StringComparer.OrdinalIgnoreCase));
23+
}
24+
25+
private static bool IsNetFramework => RuntimeInformation.FrameworkDescription.StartsWith(".NET Framework", StringComparison.OrdinalIgnoreCase);
26+
27+
#pragma warning disable CA2255 // The 'ModuleInitializer' attribute should not be used in libraries
28+
[ModuleInitializer]
29+
#pragma warning restore CA2255 // The 'ModuleInitializer' attribute should not be used in libraries
30+
internal static void ApplyBindingRedirects()
31+
{
32+
if (IsNetFramework)
33+
{
34+
AppDomain.CurrentDomain.AssemblyResolve += CurrentDomain_AssemblyResolve;
35+
}
36+
}
37+
38+
private static Assembly? CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs args)
39+
{
40+
AssemblyName expected = new(args.Name);
41+
if (LocalAssemblies.Value.TryGetValue(expected.Name, out string? path))
42+
{
43+
AssemblyName actual = AssemblyName.GetAssemblyName(path);
44+
if (actual.Version >= expected.Version)
45+
{
46+
return Assembly.LoadFile(path);
47+
}
48+
}
49+
50+
return null;
51+
}
52+
}
53+
}

src/Microsoft.Windows.CsWin32/Microsoft.Windows.CsWin32.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
<PackageReference Include="Nullable" Version="1.3.0" />
5151
<PackageReference Include="System.Memory" Version="4.5.4" PrivateAssets="none" />
5252
<PackageReference Include="System.Reflection.Metadata" Version="5.0.0" />
53+
<PackageReference Include="System.Text.Encodings.Web" Version="4.7.2" />
5354
<PackageReference Include="System.Text.Json" Version="4.7.2" />
5455
</ItemGroup>
5556

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
3+
4+
namespace System.Runtime.CompilerServices
5+
{
6+
[AttributeUsage(AttributeTargets.Method, AllowMultiple = false)]
7+
internal sealed class ModuleInitializerAttribute : Attribute
8+
{
9+
}
10+
}

0 commit comments

Comments
 (0)