<PackageReference Include="Castle.Core" Version="3.2.1" />

DefaultProxyBuilder

Default implementation of IProxyBuilder interface producing in-memory proxy assemblies.
using Castle.Core.Logging; using Castle.DynamicProxy.Generators; using Castle.DynamicProxy.Generators.Emitters; using Castle.DynamicProxy.Internal; using System; using System.Collections.Generic; using System.Linq; using System.Reflection; 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(BuildInternalsVisibleMessageForType(target)); } 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; } private static string BuildInternalsVisibleMessageForType(Type target) { Assembly assembly = target.Assembly; string text = " not"; string text2 = "\"DynamicProxyGenAssembly2\""; if (assembly.IsAssemblySigned()) { text = ""; text2 = ((!ReferencesCastleCore(assembly)) ? ('"' + "DynamicProxyGenAssembly2, PublicKey=0024000004800000940000000602000000240000525341310004000001000100c547cac37abd99c8db225ef2f6c8a3602f3b3606cc9891605d02baa56104f4cfc0734aa39b93bf7852f7d9266654753cc297e7d2edfe0bac1cdcf9f717241550e0a7b191195b7667bb4f64bcb8e2121380fd1d9d46ad2d92d2d15605093924cceaf74c4861eff62abf69b9291ed0a340e113be11e6a7d3113e92484cf7045cc7" + '"') : "InternalsVisible.ToDynamicProxyGenAssembly2"); } return $"""{target.FullName}""{text2}""{assembly.GetName().Name}""{text}"""; } private static bool ReferencesCastleCore(Assembly inspectedAssembly) { return inspectedAssembly.GetReferencedAssemblies().Any((AssemblyName r) => r.FullName == Assembly.GetExecutingAssembly().FullName); } } }