-
Notifications
You must be signed in to change notification settings - Fork 161
Create dynamic proxies in the same ALC as the interface they implement #1247
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
5461a74
Create dynamic proxies in fewer dynamic assemblies
AArnott 978f8ea
Be intentional about ALCs and dynamic proxies
AArnott 5a32ad3
Default to creating dynamic assemblies in the ALC that loaded the fir…
AArnott f834153
Improve code comments
AArnott c1ed6ae
Disable dotnet format check
AArnott File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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,36 @@ | ||
| // Copyright (c) Microsoft Corporation. All rights reserved. | ||
|
|
||
| using System.Reflection; | ||
|
|
||
| namespace StreamJsonRpc; | ||
|
|
||
| internal class AssemblyNameEqualityComparer : IEqualityComparer<AssemblyName> | ||
| { | ||
| internal static readonly IEqualityComparer<AssemblyName> Instance = new AssemblyNameEqualityComparer(); | ||
|
|
||
| private AssemblyNameEqualityComparer() | ||
| { | ||
| } | ||
|
|
||
| public bool Equals(AssemblyName? x, AssemblyName? y) | ||
| { | ||
| if (x is null && y is null) | ||
| { | ||
| return true; | ||
| } | ||
|
|
||
| if (x is null || y is null) | ||
| { | ||
| return false; | ||
| } | ||
|
|
||
| return string.Equals(x.FullName, y.FullName, StringComparison.OrdinalIgnoreCase); | ||
| } | ||
|
|
||
| public int GetHashCode(AssemblyName obj) | ||
| { | ||
| Requires.NotNull(obj, nameof(obj)); | ||
|
|
||
| return StringComparer.OrdinalIgnoreCase.GetHashCode(obj.FullName); | ||
| } | ||
| } |
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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,50 @@ | ||
| // Copyright (c) Microsoft Corporation. All rights reserved. | ||
| // Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
|
||
| #if NET | ||
|
|
||
| using System.Reflection; | ||
| using System.Runtime.CompilerServices; | ||
| using System.Runtime.Loader; | ||
|
|
||
| namespace StreamJsonRpc.Tests; | ||
|
|
||
| internal static class UnreachableAssemblyTools | ||
| { | ||
| /// <summary> | ||
| /// Useful for tests to call before asserting conditions that depend on the UnreachableAssembly.dll | ||
| /// actually being unreachable. | ||
| /// </summary> | ||
| internal static void VerifyUnreachableAssembly() | ||
| { | ||
| Assert.Throws<FileNotFoundException>(() => typeof(UnreachableAssembly.SomeUnreachableClass)); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Initializes an <see cref="AssemblyLoadContext"/> with UnreachableAssembly.dll loaded into it. | ||
| /// </summary> | ||
| /// <param name="testName">The name to give the <see cref="AssemblyLoadContext"/>.</param> | ||
| /// <returns>The new <see cref="AssemblyLoadContext"/>.</returns> | ||
| internal static AssemblyLoadContext CreateContextForReachingTheUnreachable([CallerMemberName] string? testName = null) | ||
| { | ||
| AssemblyLoadContext alc = new(testName); | ||
| alc.LoadFromAssemblyPath(Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)!, "hidden", "UnreachableAssembly.dll")); | ||
| return alc; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Translates a <see cref="MethodInfo"/> from one ALC into another ALC, so that it can be invoked | ||
| /// within the context of the new ALC. | ||
| /// </summary> | ||
| /// <param name="alc">The <see cref="AssemblyLoadContext"/> to load the method into.</param> | ||
| /// <param name="helperMethodInfo">The <see cref="MethodInfo"/> of the method in the caller's ALC to load into the given <paramref name="alc"/>.</param> | ||
| /// <returns>The translated <see cref="MethodInfo"/>.</returns> | ||
| internal static MethodInfo LoadHelperInAlc(AssemblyLoadContext alc, MethodInfo helperMethodInfo) | ||
| { | ||
| Assembly selfWithinAlc = alc.LoadFromAssemblyPath(helperMethodInfo.DeclaringType!.Assembly.Location); | ||
| MethodInfo helperWithinAlc = (MethodInfo)selfWithinAlc.ManifestModule.ResolveMethod(helperMethodInfo.MetadataToken)!; | ||
| return helperWithinAlc; | ||
| } | ||
| } | ||
|
|
||
| #endif |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.