Skip to content

Commit 617627e

Browse files
committed
Introduce CollectibleProxyBuilder
1 parent 17fd99c commit 617627e

File tree

2 files changed

+65
-10
lines changed

2 files changed

+65
-10
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// Copyright 2004-2024 Castle Project - http://www.castleproject.org/
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
#nullable enable
16+
17+
namespace Castle.DynamicProxy
18+
{
19+
using System.Reflection.Emit;
20+
21+
/// <summary>
22+
/// ProxyBuilder that causes generated assemblies to be collectible.
23+
/// </summary>
24+
public class CollectibleProxyBuilder : DefaultProxyBuilder
25+
{
26+
/// <summary>
27+
/// Initializes a new instance of the <see cref="CollectibleProxyBuilder" /> class.
28+
/// </summary>
29+
public CollectibleProxyBuilder() : base(new ModuleScope(AssemblyBuilderAccess.RunAndCollect))
30+
{
31+
}
32+
}
33+
}

src/Castle.Core/DynamicProxy/ModuleScope.cs

Lines changed: 32 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ public class ModuleScope
6060
private readonly object moduleLocker = new object();
6161

6262
// Specified whether the generated assemblies are intended to be saved
63-
private readonly bool savePhysicalAssembly;
63+
private readonly AssemblyBuilderAccess assemblyBuilderAccess;
6464
private readonly bool disableSignedModule;
6565
private readonly INamingScope namingScope;
6666

@@ -81,6 +81,16 @@ public ModuleScope(bool savePhysicalAssembly)
8181
{
8282
}
8383

84+
/// <summary>
85+
/// Initializes a new instance of the <see cref = "ModuleScope" /> class,
86+
/// allowing to specify the <see cref="AssemblyBuilderAccess" /> to be used for generated assemblies.
87+
/// </summary>
88+
/// <param name = "assemblyBuilderAccess">The desired <see cref="AssemblyBuilderAccess" /> to be used for the generated assemblies.</param>
89+
public ModuleScope(AssemblyBuilderAccess assemblyBuilderAccess)
90+
: this(assemblyBuilderAccess, disableSignedModule: false, new NamingScope(), DEFAULT_ASSEMBLY_NAME, DEFAULT_FILE_NAME, DEFAULT_ASSEMBLY_NAME, DEFAULT_FILE_NAME)
91+
{
92+
}
93+
8494
/// <summary>
8595
/// Initializes a new instance of the <see cref = "ModuleScope" /> class, allowing to specify whether the assemblies generated by this instance
8696
/// should be saved.
@@ -133,8 +143,21 @@ public ModuleScope(bool savePhysicalAssembly, bool disableSignedModule, string s
133143
internal ModuleScope(bool savePhysicalAssembly, bool disableSignedModule, INamingScope namingScope,
134144
string strongAssemblyName, string strongModulePath,
135145
string weakAssemblyName, string weakModulePath)
146+
: this(
147+
#if FEATURE_ASSEMBLYBUILDER_SAVE
148+
assemblyBuilderAccess: savePhysicalAssembly ? AssemblyBuilderAccess.RunAndSave : AssemblyBuilderAccess.Run,
149+
#else
150+
assemblyBuilderAccess: AssemblyBuilderAccess.Run,
151+
#endif
152+
disableSignedModule, namingScope, strongAssemblyName, strongModulePath, weakAssemblyName, weakModulePath)
153+
{
154+
}
155+
156+
internal ModuleScope(AssemblyBuilderAccess assemblyBuilderAccess, bool disableSignedModule, INamingScope namingScope,
157+
string strongAssemblyName, string strongModulePath,
158+
string weakAssemblyName, string weakModulePath)
136159
{
137-
this.savePhysicalAssembly = savePhysicalAssembly;
160+
this.assemblyBuilderAccess = assemblyBuilderAccess;
138161
this.disableSignedModule = disableSignedModule;
139162
this.namingScope = namingScope;
140163
this.strongAssemblyName = strongAssemblyName;
@@ -309,14 +332,14 @@ private ModuleBuilder CreateModule(bool signStrongName)
309332
{
310333
var assemblyName = GetAssemblyName(signStrongName);
311334
var moduleName = signStrongName ? StrongNamedModuleName : WeakNamedModuleName;
312-
#if FEATURE_APPDOMAIN
313-
if (savePhysicalAssembly)
335+
#if FEATURE_APPDOMAIN && FEATURE_ASSEMBLYBUILDER_SAVE
336+
if ((assemblyBuilderAccess & AssemblyBuilderAccess.Save) != 0)
314337
{
315338
AssemblyBuilder assemblyBuilder;
316339
try
317340
{
318341
assemblyBuilder = AppDomain.CurrentDomain.DefineDynamicAssembly(
319-
assemblyName, AssemblyBuilderAccess.RunAndSave, signStrongName ? StrongNamedModuleDirectory : WeakNamedModuleDirectory);
342+
assemblyName, assemblyBuilderAccess, signStrongName ? StrongNamedModuleDirectory : WeakNamedModuleDirectory);
320343
}
321344
catch (ArgumentException e)
322345
{
@@ -339,10 +362,9 @@ private ModuleBuilder CreateModule(bool signStrongName)
339362
#endif
340363
{
341364
#if FEATURE_APPDOMAIN
342-
var assemblyBuilder = AppDomain.CurrentDomain.DefineDynamicAssembly(
343-
assemblyName, AssemblyBuilderAccess.Run);
365+
var assemblyBuilder = AppDomain.CurrentDomain.DefineDynamicAssembly(assemblyName, assemblyBuilderAccess);
344366
#else
345-
var assemblyBuilder = AssemblyBuilder.DefineDynamicAssembly(assemblyName, AssemblyBuilderAccess.Run);
367+
var assemblyBuilder = AssemblyBuilder.DefineDynamicAssembly(assemblyName, assemblyBuilderAccess);
346368
#endif
347369

348370
var module = assemblyBuilder.DefineDynamicModule(moduleName);
@@ -387,7 +409,7 @@ private AssemblyName GetAssemblyName(bool signStrongName)
387409
/// <returns>The path of the generated assembly file, or null if no file has been generated.</returns>
388410
public string? SaveAssembly()
389411
{
390-
if (!savePhysicalAssembly)
412+
if ((assemblyBuilderAccess & AssemblyBuilderAccess.Save) == 0)
391413
{
392414
return null;
393415
}
@@ -433,7 +455,7 @@ private AssemblyName GetAssemblyName(bool signStrongName)
433455
/// <returns>The path of the generated assembly file, or null if no file has been generated.</returns>
434456
public string? SaveAssembly(bool strongNamed)
435457
{
436-
if (!savePhysicalAssembly)
458+
if ((assemblyBuilderAccess & AssemblyBuilderAccess.Save) == 0)
437459
{
438460
return null;
439461
}

0 commit comments

Comments
 (0)