diff --git a/ref/Castle.Core-net462.cs b/ref/Castle.Core-net462.cs index e811e9fd7f..bcf1d6f034 100644 --- a/ref/Castle.Core-net462.cs +++ b/ref/Castle.Core-net462.cs @@ -2476,6 +2476,10 @@ public virtual void MethodsInspected() { } public virtual void NonProxyableMemberNotification(System.Type type, System.Reflection.MemberInfo memberInfo) { } public virtual bool ShouldInterceptMethod(System.Type type, System.Reflection.MethodInfo methodInfo) { } } + public static class AssemblyBuilderAccessDefaults + { + public static System.Reflection.Emit.AssemblyBuilderAccess Default { get; set; } + } public class CustomAttributeInfo : System.IEquatable { public CustomAttributeInfo(System.Reflection.ConstructorInfo constructor, object[] constructorArgs) { } diff --git a/ref/Castle.Core-net6.0.cs b/ref/Castle.Core-net6.0.cs index 38e7bed03c..d6295af16f 100644 --- a/ref/Castle.Core-net6.0.cs +++ b/ref/Castle.Core-net6.0.cs @@ -2444,6 +2444,10 @@ public virtual void MethodsInspected() { } public virtual void NonProxyableMemberNotification(System.Type type, System.Reflection.MemberInfo memberInfo) { } public virtual bool ShouldInterceptMethod(System.Type type, System.Reflection.MethodInfo methodInfo) { } } + public static class AssemblyBuilderAccessDefaults + { + public static System.Reflection.Emit.AssemblyBuilderAccess Default { get; set; } + } public class CustomAttributeInfo : System.IEquatable { public CustomAttributeInfo(System.Reflection.ConstructorInfo constructor, object[] constructorArgs) { } diff --git a/ref/Castle.Core-netstandard2.0.cs b/ref/Castle.Core-netstandard2.0.cs index 9e8efdf470..81a453c224 100644 --- a/ref/Castle.Core-netstandard2.0.cs +++ b/ref/Castle.Core-netstandard2.0.cs @@ -2442,6 +2442,10 @@ public virtual void MethodsInspected() { } public virtual void NonProxyableMemberNotification(System.Type type, System.Reflection.MemberInfo memberInfo) { } public virtual bool ShouldInterceptMethod(System.Type type, System.Reflection.MethodInfo methodInfo) { } } + public static class AssemblyBuilderAccessDefaults + { + public static System.Reflection.Emit.AssemblyBuilderAccess Default { get; set; } + } public class CustomAttributeInfo : System.IEquatable { public CustomAttributeInfo(System.Reflection.ConstructorInfo constructor, object[] constructorArgs) { } diff --git a/ref/Castle.Core-netstandard2.1.cs b/ref/Castle.Core-netstandard2.1.cs index 1a81b5e911..1bd78e01e0 100644 --- a/ref/Castle.Core-netstandard2.1.cs +++ b/ref/Castle.Core-netstandard2.1.cs @@ -2442,6 +2442,10 @@ public virtual void MethodsInspected() { } public virtual void NonProxyableMemberNotification(System.Type type, System.Reflection.MemberInfo memberInfo) { } public virtual bool ShouldInterceptMethod(System.Type type, System.Reflection.MethodInfo methodInfo) { } } + public static class AssemblyBuilderAccessDefaults + { + public static System.Reflection.Emit.AssemblyBuilderAccess Default { get; set; } + } public class CustomAttributeInfo : System.IEquatable { public CustomAttributeInfo(System.Reflection.ConstructorInfo constructor, object[] constructorArgs) { } diff --git a/src/Castle.Core/Components.DictionaryAdapter/DictionaryAdapterFactory.cs b/src/Castle.Core/Components.DictionaryAdapter/DictionaryAdapterFactory.cs index a2cb08a60d..366e2a4c45 100644 --- a/src/Castle.Core/Components.DictionaryAdapter/DictionaryAdapterFactory.cs +++ b/src/Castle.Core/Components.DictionaryAdapter/DictionaryAdapterFactory.cs @@ -27,6 +27,7 @@ namespace Castle.Components.DictionaryAdapter using Castle.Components.DictionaryAdapter.Xml; using Castle.Core.Internal; + using Castle.DynamicProxy; /// /// Uses Reflection.Emit to expose the properties of a dictionary @@ -148,7 +149,7 @@ private object InternalGetAdapter(Type type, IDictionary dictionary, PropertyDes private static TypeBuilder CreateTypeBuilder(Type type) { var assemblyName = new AssemblyName("CastleDictionaryAdapterAssembly"); - var assemblyBuilder = AssemblyBuilder.DefineDynamicAssembly(assemblyName, AssemblyBuilderAccess.Run); + var assemblyBuilder = AssemblyBuilder.DefineDynamicAssembly(assemblyName, AssemblyBuilderAccessDefaults.Default); var moduleBuilder = assemblyBuilder.DefineDynamicModule("CastleDictionaryAdapterModule"); return CreateAdapterType(type, moduleBuilder); } diff --git a/src/Castle.Core/DynamicProxy/AssemblyBuilderAccessDefaults.cs b/src/Castle.Core/DynamicProxy/AssemblyBuilderAccessDefaults.cs new file mode 100644 index 0000000000..43fe377af8 --- /dev/null +++ b/src/Castle.Core/DynamicProxy/AssemblyBuilderAccessDefaults.cs @@ -0,0 +1,40 @@ +// Copyright 2004-2021 Castle Project - http://www.castleproject.org/ +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + + +namespace Castle.DynamicProxy +{ + using System.Reflection.Emit; + + public static class AssemblyBuilderAccessDefaults + { +#if NET + // If Castle.Core is loaded in a collectible AssemblyLoadContext, the generated assembly should be collectible as well by default. + static readonly AssemblyBuilderAccess automatic = System.Runtime.Loader.AssemblyLoadContext.GetLoadContext(typeof(AssemblyBuilderAccessDefaults).Assembly).IsCollectible ? AssemblyBuilderAccess.RunAndCollect : AssemblyBuilderAccess.Run; +#else + static readonly AssemblyBuilderAccess automatic = AssemblyBuilderAccess.Run; +#endif + static AssemblyBuilderAccess? userSpecified; + + /// + /// Get or set the default to use when creating dynamic assemblies. + /// On .Net Core, the default is if Castle.Core is loaded in a collectible AssemblyLoadContext. + /// + public static AssemblyBuilderAccess Default + { + get => userSpecified ?? automatic; + set => userSpecified = value; + } + } +} diff --git a/src/Castle.Core/DynamicProxy/ModuleScope.cs b/src/Castle.Core/DynamicProxy/ModuleScope.cs index b70b10984e..d9f082f4df 100644 --- a/src/Castle.Core/DynamicProxy/ModuleScope.cs +++ b/src/Castle.Core/DynamicProxy/ModuleScope.cs @@ -335,9 +335,9 @@ private ModuleBuilder CreateModule(bool signStrongName) { #if FEATURE_APPDOMAIN var assemblyBuilder = AppDomain.CurrentDomain.DefineDynamicAssembly( - assemblyName, AssemblyBuilderAccess.Run); + assemblyName, AssemblyBuilderAccessDefaults.Default); #else - var assemblyBuilder = AssemblyBuilder.DefineDynamicAssembly(assemblyName, AssemblyBuilderAccess.Run); + var assemblyBuilder = AssemblyBuilder.DefineDynamicAssembly(assemblyName, AssemblyBuilderAccessDefaults.Default); #endif var module = assemblyBuilder.DefineDynamicModule(moduleName);