/// <summary> /// Generate the proxy type. /// </summary> /// <param name="proxyType">The interface the proxy type must implement.</param> /// <param name="proxyTargetType">The target type to receive the proxied calls.</param> /// <param name="proxyOptions">The proxy generation options.</param> /// <returns>A <see cref="Type"/> representing the proxy type.</returns> private Type GenerateProxyType( Type proxyType, Type proxyTargetType, ProxyOptions proxyOptions = null) { return(this.GenerateProxyType(proxyType, proxyTargetType, null, null, proxyOptions)); }
/// <summary> /// Generate the proxy instance. /// </summary> /// <param name="proxyType">The proxy type.</param> /// <param name="implementation">The proxy implementation.</param> /// <param name="action">Optional proxy builder action.</param> /// <param name="proxyOptions">Optional proxy options.</param> /// <returns>An instance of the proxy type.</returns> public object CreateProxy(Type proxyType, IProxy implementation, Action <IProxyBuilderContext> action = null, ProxyOptions proxyOptions = null) { return(this.CreateProxy(proxyType, null, implementation, action, proxyOptions)); }
/// <summary> /// Generate the proxy type. /// </summary> /// <param name="proxyType">The interface the proxy type must implement.</param> /// <param name="proxyTargetType">The target type to receive the proxied calls.</param> /// <param name="proxyBaseType">The base type.</param> /// <param name="action">An action to allow build type injection.</param> /// <param name="proxyOptions">The proxy generation options.</param> /// <returns>A <see cref="Type"/> representing the proxy type.</returns> private Type GenerateProxyType( Type proxyType, Type proxyTargetType, Type proxyBaseType, Action <IProxyBuilderContext> action, ProxyOptions proxyOptions = null) { if (proxyBaseType?.IsInterface == true) { throw new ArgumentException("Argument cannot be an interface", "proxyBaseType"); } if (proxyType.IsInterface == false && proxyType != proxyBaseType) { throw new ArgumentException("Argument is not an interface", "proxyType"); } if (typeof(IProxy).IsAssignableFrom(proxyTargetType) == false) { throw new ArgumentException("Argument does not implement <IProxy>", "proxiedType"); } var typeName = proxyOptions?.TypeName ?? TypeName(proxyType, proxyTargetType); var typeBuilder = TypeFactory .Default .NewType(typeName) .Public() .Implements(typeof(IProxiedObject)); if (proxyType.IsInterface == true) { typeBuilder.Implements(proxyType); } if (proxyBaseType != null) { typeBuilder.InheritsFrom(proxyBaseType); } var targetField = typeBuilder .NewField <IProxy>("target") .Private(); var context = new ProxyBuilderContext( typeBuilder, proxyType, proxyTargetType, targetField, null); action?.Invoke(context); if (proxyType.IsInterface == true) { this.ImplementInterfaces(context, context.NewType); } this.EmitConstructor(context); this.EmitIProxiedObjectInterface(context); return(context .TypeBuilder .CreateType()); }
/// <summary> /// Generate the proxy instance. /// </summary> /// <param name="proxyType">The proxy type.</param> /// <param name="proxyBaseType">The proxy base type.</param> /// <param name="implementation">The proxy implementation.</param> /// <param name="action">Optional proxy builder action.</param> /// <param name="proxyOptions">Optional proxy options.</param> /// <returns>An instance of the proxy type.</returns> public object CreateProxy(Type proxyType, Type proxyBaseType, IProxy implementation, Action <IProxyBuilderContext> action = null, ProxyOptions proxyOptions = null) { Utility.ThrowIfArgumentNull(implementation, nameof(implementation)); var typeName = proxyOptions?.TypeName ?? TypeName(proxyType, typeof(IProxy)); Type proxy = TypeFactory .Default .GetType( typeName, true); if (proxy == null) { proxy = this.GenerateProxyType(proxyType, typeof(IProxy), proxyBaseType, action, proxyOptions); } return(Activator.CreateInstance(proxy, implementation)); }