Skip to content
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

When Castle.Core is loaded in a collectible Assembly Load Context, use AssemblyBuilderAccess.RunAndCollect by default #679

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions ref/Castle.Core-net462.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Castle.DynamicProxy.CustomAttributeInfo>
{
public CustomAttributeInfo(System.Reflection.ConstructorInfo constructor, object[] constructorArgs) { }
Expand Down
4 changes: 4 additions & 0 deletions ref/Castle.Core-net6.0.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Castle.DynamicProxy.CustomAttributeInfo>
{
public CustomAttributeInfo(System.Reflection.ConstructorInfo constructor, object[] constructorArgs) { }
Expand Down
4 changes: 4 additions & 0 deletions ref/Castle.Core-netstandard2.0.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Castle.DynamicProxy.CustomAttributeInfo>
{
public CustomAttributeInfo(System.Reflection.ConstructorInfo constructor, object[] constructorArgs) { }
Expand Down
4 changes: 4 additions & 0 deletions ref/Castle.Core-netstandard2.1.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Castle.DynamicProxy.CustomAttributeInfo>
{
public CustomAttributeInfo(System.Reflection.ConstructorInfo constructor, object[] constructorArgs) { }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ namespace Castle.Components.DictionaryAdapter

using Castle.Components.DictionaryAdapter.Xml;
using Castle.Core.Internal;
using Castle.DynamicProxy;

/// <summary>
/// Uses Reflection.Emit to expose the properties of a dictionary
Expand Down Expand Up @@ -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);
}
Expand Down
40 changes: 40 additions & 0 deletions src/Castle.Core/DynamicProxy/AssemblyBuilderAccessDefaults.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// Copyright 2004-2021 Castle Project - http://www.castleproject.org/
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wrong year, should be -2024.

//
// 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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note to self: need to research what this conditional compilation symbol is for, and whether it's needed.

Does it guard the RunAndCollect enum value, or the AssemblyLoadContext.GetLoadContext? If the former, then perhapa the whole type being added here shouldn't be exposed publicly for those TFMs; so I'm suspecting it's probably guarding the latter.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is to guard against usage of AssemblyLoadContext (it is not in netatandard / netfx, only in .net core).

However, it would be nice if the check could be done at runtime (in case of loading netatandard assemblies in a .net core executable). We have usage of reflection based wrappers at Unity for this kind of thing. I can probably do the same here.

// 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;

/// <summary>
/// Get or set the default <see cref="System.Reflection.Emit.AssemblyBuilderAccess"/> to use when creating dynamic assemblies.
/// On .Net Core, the default is <see cref="System.Reflection.Emit.AssemblyBuilderAccess.RunAndCollect"/> if Castle.Core is loaded in a collectible AssemblyLoadContext.
/// </summary>
public static AssemblyBuilderAccess Default
{
get => userSpecified ?? automatic;
set => userSpecified = value;
}
}
}
4 changes: 2 additions & 2 deletions src/Castle.Core/DynamicProxy/ModuleScope.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Loading