-
Notifications
You must be signed in to change notification settings - Fork 4.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move WebcilReader reflection to a helper; add lazy initialization
- Loading branch information
1 parent
6eaee63
commit 45b7c47
Showing
2 changed files
with
63 additions
and
30 deletions.
There are no files selected for viewing
59 changes: 59 additions & 0 deletions
59
src/libraries/Microsoft.NET.WebAssembly.Webcil/src/Webcil/WebcilReader.Reflection.cs
This file contains 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,59 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using System; | ||
using System.Collections.Immutable; | ||
using System.IO; | ||
using System.Reflection; | ||
using System.Runtime.InteropServices; | ||
|
||
using System.Reflection.Metadata; | ||
using System.Reflection.PortableExecutable; | ||
|
||
namespace Microsoft.NET.WebAssembly.Webcil; | ||
|
||
|
||
public sealed partial class WebcilReader | ||
{ | ||
|
||
// Helpers to call into System.Reflection.Metadata internals | ||
internal static class Reflection | ||
{ | ||
private static Lazy<MethodInfo> _readUtf8NullTerminated = new Lazy<MethodInfo>(() => | ||
{ | ||
var mi = typeof(BlobReader).GetMethod("ReadUtf8NullTerminated", BindingFlags.NonPublic | BindingFlags.Instance); | ||
if (mi == null) | ||
{ | ||
throw new InvalidOperationException("Could not find BlobReader.ReadUtf8NullTerminated"); | ||
} | ||
return mi; | ||
}); | ||
|
||
internal static string? ReadUtf8NullTerminated(BlobReader reader) => (string?)_readUtf8NullTerminated.Value.Invoke(reader, null); | ||
|
||
private static Lazy<ConstructorInfo> _codeViewDebugDirectoryDataCtor = new Lazy<ConstructorInfo>(() => | ||
{ | ||
var types = new Type[] { typeof(Guid), typeof(int), typeof(string) }; | ||
var mi = typeof(CodeViewDebugDirectoryData).GetConstructor(BindingFlags.NonPublic | BindingFlags.Instance, null, types, null); | ||
if (mi == null) | ||
{ | ||
throw new InvalidOperationException("Could not find CodeViewDebugDirectoryData constructor"); | ||
} | ||
return mi; | ||
}); | ||
|
||
internal static CodeViewDebugDirectoryData MakeCodeViewDebugDirectoryData(Guid guid, int age, string path) => (CodeViewDebugDirectoryData)_codeViewDebugDirectoryDataCtor.Value.Invoke(new object[] { guid, age, path }); | ||
|
||
private static Lazy<ConstructorInfo> _pdbChecksumDebugDirectoryDataCtor = new Lazy<ConstructorInfo>(() => | ||
{ | ||
var types = new Type[] { typeof(string), typeof(ImmutableArray<byte>) }; | ||
var mi = typeof(PdbChecksumDebugDirectoryData).GetConstructor(BindingFlags.NonPublic | BindingFlags.Instance, null, types, null); | ||
if (mi == null) | ||
{ | ||
throw new InvalidOperationException("Could not find PdbChecksumDebugDirectoryData constructor"); | ||
} | ||
return mi; | ||
}); | ||
internal static PdbChecksumDebugDirectoryData MakePdbChecksumDebugDirectoryData(string algorithmName, ImmutableArray<byte> checksum) => (PdbChecksumDebugDirectoryData)_pdbChecksumDebugDirectoryDataCtor.Value.Invoke(new object[] { algorithmName, checksum }); | ||
} | ||
} |
This file contains 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