Skip to content

Create separate proxy generator for each type #938

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

Closed
wants to merge 1 commit into from
Closed
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
6 changes: 4 additions & 2 deletions src/Moq/ProxyFactories/CastleProxyFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// All rights reserved. Licensed under the BSD 3-Clause License; see License.txt.

using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
Expand Down Expand Up @@ -30,7 +31,7 @@ namespace Moq
internal sealed class CastleProxyFactory : ProxyFactory
{
private ProxyGenerationOptions generationOptions;
private ProxyGenerator generator;
private readonly ConcurrentDictionary<string, ProxyGenerator> generators = new ConcurrentDictionary<string, ProxyGenerator>();

#if FEATURE_CAS || FEATURE_COM
static CastleProxyFactory()
Expand All @@ -52,12 +53,13 @@ static CastleProxyFactory()
public CastleProxyFactory()
{
this.generationOptions = new ProxyGenerationOptions { Hook = new IncludeObjectMethodsHook(), BaseTypeForInterfaceProxy = typeof(InterfaceProxy) };
this.generator = new ProxyGenerator();
}

/// <inheritdoc />
public override object CreateProxy(Type mockType, Moq.IInterceptor interceptor, Type[] interfaces, object[] arguments)
{
var generator = generators.GetOrAdd(mockType.Name, key => new ProxyGenerator());

// All generated proxies need to implement `IProxy`:
var additionalInterfaces = new Type[1 + interfaces.Length];
additionalInterfaces[0] = typeof(IProxy);
Expand Down