DefaultProxyBuilder
Default implementation of IProxyBuilder interface producing in-memory proxy assemblies.
using Castle.Core.Logging;
using Castle.DynamicProxy.Generators;
using Castle.DynamicProxy.Internal;
using System;
using System.Collections.Generic;
namespace Castle.DynamicProxy
{
public class DefaultProxyBuilder : IProxyBuilder
{
private readonly ModuleScope scope;
private ILogger logger = NullLogger.Instance;
public ILogger Logger {
get {
return logger;
}
set {
logger = value;
}
}
public ModuleScope ModuleScope => scope;
public DefaultProxyBuilder()
: this(new ModuleScope())
{
}
public DefaultProxyBuilder(ModuleScope scope)
{
this.scope = scope;
}
public Type CreateClassProxyType(Type classToProxy, Type[] additionalInterfacesToProxy, ProxyGenerationOptions options)
{
AssertValidType(classToProxy);
AssertValidTypes(additionalInterfacesToProxy);
ClassProxyGenerator classProxyGenerator = new ClassProxyGenerator(scope, classToProxy);
classProxyGenerator.Logger = logger;
ClassProxyGenerator classProxyGenerator2 = classProxyGenerator;
return classProxyGenerator2.GenerateCode(additionalInterfacesToProxy, options);
}
public Type CreateClassProxyTypeWithTarget(Type classToProxy, Type[] additionalInterfacesToProxy, ProxyGenerationOptions options)
{
AssertValidType(classToProxy);
AssertValidTypes(additionalInterfacesToProxy);
ClassProxyWithTargetGenerator classProxyWithTargetGenerator = new ClassProxyWithTargetGenerator(scope, classToProxy, additionalInterfacesToProxy, options);
classProxyWithTargetGenerator.Logger = logger;
ClassProxyWithTargetGenerator classProxyWithTargetGenerator2 = classProxyWithTargetGenerator;
return classProxyWithTargetGenerator2.GetGeneratedType();
}
public Type CreateInterfaceProxyTypeWithTarget(Type interfaceToProxy, Type[] additionalInterfacesToProxy, Type targetType, ProxyGenerationOptions options)
{
AssertValidType(interfaceToProxy);
AssertValidTypes(additionalInterfacesToProxy);
InterfaceProxyWithTargetGenerator interfaceProxyWithTargetGenerator = new InterfaceProxyWithTargetGenerator(scope, interfaceToProxy);
interfaceProxyWithTargetGenerator.Logger = logger;
InterfaceProxyWithTargetGenerator interfaceProxyWithTargetGenerator2 = interfaceProxyWithTargetGenerator;
return interfaceProxyWithTargetGenerator2.GenerateCode(targetType, additionalInterfacesToProxy, options);
}
public Type CreateInterfaceProxyTypeWithTargetInterface(Type interfaceToProxy, Type[] additionalInterfacesToProxy, ProxyGenerationOptions options)
{
AssertValidType(interfaceToProxy);
AssertValidTypes(additionalInterfacesToProxy);
InterfaceProxyWithTargetInterfaceGenerator interfaceProxyWithTargetInterfaceGenerator = new InterfaceProxyWithTargetInterfaceGenerator(scope, interfaceToProxy);
interfaceProxyWithTargetInterfaceGenerator.Logger = logger;
InterfaceProxyWithTargetInterfaceGenerator interfaceProxyWithTargetInterfaceGenerator2 = interfaceProxyWithTargetInterfaceGenerator;
return interfaceProxyWithTargetInterfaceGenerator2.GenerateCode(interfaceToProxy, additionalInterfacesToProxy, options);
}
public Type CreateInterfaceProxyTypeWithoutTarget(Type interfaceToProxy, Type[] additionalInterfacesToProxy, ProxyGenerationOptions options)
{
AssertValidType(interfaceToProxy);
AssertValidTypes(additionalInterfacesToProxy);
InterfaceProxyWithoutTargetGenerator interfaceProxyWithoutTargetGenerator = new InterfaceProxyWithoutTargetGenerator(scope, interfaceToProxy);
interfaceProxyWithoutTargetGenerator.Logger = logger;
InterfaceProxyWithoutTargetGenerator interfaceProxyWithoutTargetGenerator2 = interfaceProxyWithoutTargetGenerator;
return interfaceProxyWithoutTargetGenerator2.GenerateCode(typeof(object), additionalInterfacesToProxy, options);
}
private void AssertValidType(Type target)
{
if (target.IsGenericTypeDefinition)
throw new GeneratorException("Type " + target.FullName + " is a generic type definition. Can not create proxy for open generic types.");
if (!IsPublic(target) && !IsAccessible(target))
throw new GeneratorException("Type " + target.FullName + " is not visible to DynamicProxy. Can not create proxy for types that are not accessible. Make the type public, or internal and mark your assembly with [assembly: InternalsVisibleTo(InternalsVisible.ToDynamicProxyGenAssembly2)] attribute.");
}
private void AssertValidTypes(IEnumerable<Type> targetTypes)
{
if (targetTypes != null) {
foreach (Type targetType in targetTypes) {
AssertValidType(targetType);
}
}
}
private bool IsAccessible(Type target)
{
if (IsInternal(target))
return target.Assembly.IsInternalToDynamicProxy();
return false;
}
private bool IsPublic(Type target)
{
if (!target.IsPublic)
return target.IsNestedPublic;
return true;
}
private static bool IsInternal(Type target)
{
bool isNested = target.IsNested;
bool result = isNested && (target.IsNestedAssembly || target.IsNestedFamORAssem);
if (target.IsVisible || isNested)
return result;
return true;
}
}
}