Skip to content

Commit

Permalink
Support collectible assemblies via new CollectibleProxyBuilder
Browse files Browse the repository at this point in the history
  • Loading branch information
stakx committed Oct 19, 2024
1 parent 28b8715 commit 40b30ed
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 10 deletions.
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)
{
}

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

0 comments on commit 40b30ed

Please sign in to comment.