-
Notifications
You must be signed in to change notification settings - Fork 5.3k
Enable setting the TypeMap entrypoint assembly via a RuntimeHostConfigurationOption #121513
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
Changes from all commits
0edbd53
cd198c0
fe74d8a
bc4753d
8a2335c
0d42667
90e9f40
b6d3631
845ad5b
5156a44
06f4102
0761be6
cfef57a
bd6d21f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -189,7 +189,16 @@ private static unsafe CallbackContext CreateMaps( | |
| delegate* unmanaged<CallbackContext*, ProcessAttributesCallbackArg*, Interop.BOOL> newExternalTypeEntry, | ||
| delegate* unmanaged<CallbackContext*, ProcessAttributesCallbackArg*, Interop.BOOL> newProxyTypeEntry) | ||
| { | ||
| RuntimeAssembly? startingAssembly = (RuntimeAssembly?)Assembly.GetEntryAssembly(); | ||
| RuntimeAssembly? startingAssembly; | ||
| if (AppContext.GetData("System.Runtime.InteropServices.TypeMappingEntryAssembly") is string entryAssemblyName) | ||
| { | ||
| startingAssembly = Assembly.Load(entryAssemblyName) as RuntimeAssembly; | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is odd. The
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah, I forgot |
||
| } | ||
| else | ||
| { | ||
| startingAssembly = Assembly.GetEntryAssembly() as RuntimeAssembly; | ||
| } | ||
|
|
||
| if (startingAssembly is null) | ||
| { | ||
| throw new InvalidOperationException(SR.InvalidOperation_TypeMapMissingEntryAssembly); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| // 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.Runtime.InteropServices; | ||
|
|
||
| // This library defines TypeMap entries that should be used when | ||
| // TypeMapLib5 is specified as the TypeMappingEntryAssembly | ||
| [assembly: TypeMap<AlternateEntryPoint>("lib5_type1", typeof(Lib5Type1))] | ||
| [assembly: TypeMap<AlternateEntryPoint>("lib5_type2", typeof(Lib5Type2))] | ||
|
|
||
| [assembly: TypeMapAssociation<AlternateEntryPoint>(typeof(Lib5Type1), typeof(Lib5Proxy1))] | ||
| [assembly: TypeMapAssociation<AlternateEntryPoint>(typeof(Lib5Type2), typeof(Lib5Proxy2))] | ||
|
|
||
| public class Lib5Type1 { } | ||
| public class Lib5Type2 { } | ||
| public class Lib5Proxy1 { } | ||
| public class Lib5Proxy2 { } | ||
|
|
||
| public class AlternateEntryPoint { } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| // 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.Generic; | ||
| using System.Runtime.InteropServices; | ||
| using Xunit; | ||
|
|
||
| // Note: This test does NOT define any TypeMap attributes. | ||
| // The TypeMappingEntryAssembly is set to TypeMapLib5 via RuntimeHostConfigurationOption, | ||
| // so the type maps should be resolved from TypeMapLib5 instead of this assembly. | ||
|
|
||
| public class TypeMapEntryAssemblyTest | ||
| { | ||
| [Fact] | ||
| public static void Validate_TypeMappingEntryAssembly_ExternalMap() | ||
| { | ||
| Console.WriteLine(nameof(Validate_TypeMappingEntryAssembly_ExternalMap)); | ||
|
|
||
| // These types should be resolved from TypeMapLib5's type maps | ||
| IReadOnlyDictionary<string, Type> externalMap = TypeMapping.GetOrCreateExternalTypeMapping<AlternateEntryPoint>(); | ||
|
|
||
| Assert.Equal(typeof(Lib5Type1), externalMap["lib5_type1"]); | ||
| Assert.Equal(typeof(Lib5Type2), externalMap["lib5_type2"]); | ||
|
|
||
| Assert.True(externalMap.TryGetValue("lib5_type1", out Type? type1)); | ||
| Assert.Equal(typeof(Lib5Type1), type1); | ||
|
|
||
| Assert.True(externalMap.TryGetValue("lib5_type2", out Type? type2)); | ||
| Assert.Equal(typeof(Lib5Type2), type2); | ||
|
|
||
| Assert.False(externalMap.TryGetValue("nonexistent", out Type? _)); | ||
| } | ||
|
|
||
| [Fact] | ||
| public static void Validate_TypeMappingEntryAssembly_ProxyMap() | ||
| { | ||
| Console.WriteLine(nameof(Validate_TypeMappingEntryAssembly_ProxyMap)); | ||
|
|
||
| // These proxy mappings should be resolved from TypeMapLib5's type maps | ||
| IReadOnlyDictionary<Type, Type> proxyMap = TypeMapping.GetOrCreateProxyTypeMapping<AlternateEntryPoint>(); | ||
|
|
||
| Assert.Equal(typeof(Lib5Proxy1), proxyMap[new Lib5Type1().GetType()]); | ||
| Assert.Equal(typeof(Lib5Proxy2), proxyMap[new Lib5Type2().GetType()]); | ||
|
|
||
| Assert.False(proxyMap.TryGetValue(typeof(string), out Type? _)); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| <Project Sdk="Microsoft.NET.Sdk"> | ||
| <PropertyGroup> | ||
| <OutputType>Exe</OutputType> | ||
| <AllowUnsafeBlocks>true</AllowUnsafeBlocks> | ||
| <RequiresProcessIsolation>true</RequiresProcessIsolation> | ||
| <TypeMapEntryAssembly>TypeMapLib5</TypeMapEntryAssembly> | ||
| <MonoAotIncompatible>true</MonoAotIncompatible> | ||
| <DisableProjectBuild Condition="'$(RuntimeFlavor)' == 'mono'">true</DisableProjectBuild> | ||
| <IlcGenerateMstatFile>true</IlcGenerateMstatFile> | ||
| <IlcGenerateDgmlFile>true</IlcGenerateDgmlFile> | ||
| <GenerateRuntimeConfigurationFiles>true</GenerateRuntimeConfigurationFiles> | ||
| </PropertyGroup> | ||
|
|
||
| <ItemGroup> | ||
| <RuntimeHostConfigurationOption Include="System.Runtime.InteropServices.TypeMappingEntryAssembly" | ||
| Value="$(TypeMapEntryAssembly)" /> | ||
| </ItemGroup> | ||
|
|
||
| <ItemGroup> | ||
| <Compile Include="TypeMapEntryAssemblyApp.cs" /> | ||
| </ItemGroup> | ||
|
|
||
| <ItemGroup> | ||
| <ProjectReference Include="TypeMapLib5.csproj" /> | ||
| <ProjectReference Include="TypeMapLib1.csproj" /> | ||
| </ItemGroup> | ||
| </Project> |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| <Project Sdk="Microsoft.NET.Sdk"> | ||
| <PropertyGroup> | ||
| <OutputType>Library</OutputType> | ||
| <AllowUnsafeBlocks>true</AllowUnsafeBlocks> | ||
| </PropertyGroup> | ||
|
|
||
| <ItemGroup> | ||
| <Compile Include="Lib5.cs" /> | ||
| </ItemGroup> | ||
|
|
||
| <ItemGroup> | ||
| <ProjectReference Include="TypeMapLib1.csproj" /> | ||
| </ItemGroup> | ||
| </Project> |
Uh oh!
There was an error while loading. Please reload this page.