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

Support collectible assemblies via new CollectibleProxyBuilder #685

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Castle Core Changelog

## Unreleased

Enhancements:
- Support for collectible assemblies via new `CollectibleProxyBuilder` (@stakx, #685)

## 5.2.0 (2024-08-30)

Enhancements:
Expand Down
4 changes: 4 additions & 0 deletions ref/Castle.Core-net462.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2477,6 +2477,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 class CollectibleProxyBuilder : Castle.DynamicProxy.DefaultProxyBuilder
{
public CollectibleProxyBuilder() { }
}
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 @@ -2445,6 +2445,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 class CollectibleProxyBuilder : Castle.DynamicProxy.DefaultProxyBuilder
{
public CollectibleProxyBuilder() { }
}
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 @@ -2443,6 +2443,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 class CollectibleProxyBuilder : Castle.DynamicProxy.DefaultProxyBuilder
{
public CollectibleProxyBuilder() { }
}
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 @@ -2443,6 +2443,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 class CollectibleProxyBuilder : Castle.DynamicProxy.DefaultProxyBuilder
{
public CollectibleProxyBuilder() { }
}
public class CustomAttributeInfo : System.IEquatable<Castle.DynamicProxy.CustomAttributeInfo>
{
public CustomAttributeInfo(System.Reflection.ConstructorInfo constructor, object?[] constructorArgs) { }
Expand Down
33 changes: 33 additions & 0 deletions src/Castle.Core/DynamicProxy/CollectibleProxyBuilder.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// Copyright 2004-2024 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.

#nullable enable

namespace Castle.DynamicProxy
{
using System.Reflection.Emit;

/// <summary>
/// Proxy builder that causes generated assemblies to be collectible.
/// </summary>
public class CollectibleProxyBuilder : DefaultProxyBuilder
{
/// <summary>
/// Initializes a new instance of the <see cref="CollectibleProxyBuilder" /> class.
/// </summary>
public CollectibleProxyBuilder() : base(new ModuleScope(AssemblyBuilderAccess.RunAndCollect))
{
}
}
}
42 changes: 32 additions & 10 deletions src/Castle.Core/DynamicProxy/ModuleScope.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public class ModuleScope
private readonly object moduleLocker = new object();

// Specified whether the generated assemblies are intended to be saved
private readonly bool savePhysicalAssembly;
private readonly AssemblyBuilderAccess assemblyBuilderAccess;
private readonly bool disableSignedModule;
private readonly INamingScope namingScope;

Expand All @@ -81,6 +81,16 @@ public ModuleScope(bool savePhysicalAssembly)
{
}

/// <summary>
/// Initializes a new instance of the <see cref="ModuleScope" /> class,
/// using the specified <see cref="AssemblyBuilderAccess" /> for generated assemblies.
/// </summary>
/// <param name = "assemblyBuilderAccess">The desired <see cref="AssemblyBuilderAccess" /> to be used for generated assemblies.</param>
internal ModuleScope(AssemblyBuilderAccess assemblyBuilderAccess)
: this(assemblyBuilderAccess, disableSignedModule: false, new NamingScope(), DEFAULT_ASSEMBLY_NAME, DEFAULT_FILE_NAME, DEFAULT_ASSEMBLY_NAME, DEFAULT_FILE_NAME)
{
}
stakx marked this conversation as resolved.
Show resolved Hide resolved

/// <summary>
/// Initializes a new instance of the <see cref = "ModuleScope" /> class, allowing to specify whether the assemblies generated by this instance
/// should be saved.
Expand Down Expand Up @@ -133,8 +143,21 @@ public ModuleScope(bool savePhysicalAssembly, bool disableSignedModule, string s
internal ModuleScope(bool savePhysicalAssembly, bool disableSignedModule, INamingScope namingScope,
string strongAssemblyName, string strongModulePath,
string weakAssemblyName, string weakModulePath)
: this(
#if FEATURE_ASSEMBLYBUILDER_SAVE
assemblyBuilderAccess: savePhysicalAssembly ? AssemblyBuilderAccess.RunAndSave : AssemblyBuilderAccess.Run,
#else
assemblyBuilderAccess: AssemblyBuilderAccess.Run,
#endif
stakx marked this conversation as resolved.
Show resolved Hide resolved
disableSignedModule, namingScope, strongAssemblyName, strongModulePath, weakAssemblyName, weakModulePath)
{
}

internal ModuleScope(AssemblyBuilderAccess assemblyBuilderAccess, bool disableSignedModule, INamingScope namingScope,
string strongAssemblyName, string strongModulePath,
string weakAssemblyName, string weakModulePath)
{
this.savePhysicalAssembly = savePhysicalAssembly;
this.assemblyBuilderAccess = assemblyBuilderAccess;
this.disableSignedModule = disableSignedModule;
this.namingScope = namingScope;
this.strongAssemblyName = strongAssemblyName;
Expand Down Expand Up @@ -309,14 +332,14 @@ private ModuleBuilder CreateModule(bool signStrongName)
{
var assemblyName = GetAssemblyName(signStrongName);
var moduleName = signStrongName ? StrongNamedModuleName : WeakNamedModuleName;
#if FEATURE_APPDOMAIN
if (savePhysicalAssembly)
#if FEATURE_APPDOMAIN && FEATURE_ASSEMBLYBUILDER_SAVE
if ((assemblyBuilderAccess & AssemblyBuilderAccess.Save) != 0)
{
AssemblyBuilder assemblyBuilder;
try
{
assemblyBuilder = AppDomain.CurrentDomain.DefineDynamicAssembly(
assemblyName, AssemblyBuilderAccess.RunAndSave, signStrongName ? StrongNamedModuleDirectory : WeakNamedModuleDirectory);
assemblyName, assemblyBuilderAccess, signStrongName ? StrongNamedModuleDirectory : WeakNamedModuleDirectory);
}
catch (ArgumentException e)
{
Expand All @@ -339,10 +362,9 @@ private ModuleBuilder CreateModule(bool signStrongName)
#endif
{
#if FEATURE_APPDOMAIN
var assemblyBuilder = AppDomain.CurrentDomain.DefineDynamicAssembly(
assemblyName, AssemblyBuilderAccess.Run);
var assemblyBuilder = AppDomain.CurrentDomain.DefineDynamicAssembly(assemblyName, assemblyBuilderAccess);
#else
var assemblyBuilder = AssemblyBuilder.DefineDynamicAssembly(assemblyName, AssemblyBuilderAccess.Run);
var assemblyBuilder = AssemblyBuilder.DefineDynamicAssembly(assemblyName, assemblyBuilderAccess);
#endif

var module = assemblyBuilder.DefineDynamicModule(moduleName);
Expand Down Expand Up @@ -387,7 +409,7 @@ private AssemblyName GetAssemblyName(bool signStrongName)
/// <returns>The path of the generated assembly file, or null if no file has been generated.</returns>
public string? SaveAssembly()
{
if (!savePhysicalAssembly)
if ((assemblyBuilderAccess & AssemblyBuilderAccess.Save) == 0)
{
return null;
}
Expand Down Expand Up @@ -433,7 +455,7 @@ private AssemblyName GetAssemblyName(bool signStrongName)
/// <returns>The path of the generated assembly file, or null if no file has been generated.</returns>
public string? SaveAssembly(bool strongNamed)
{
if (!savePhysicalAssembly)
if ((assemblyBuilderAccess & AssemblyBuilderAccess.Save) == 0)
{
return null;
}
Expand Down
Loading