/// <summary> /// Attach this policy to given capability. /// </summary> /// <typeparam name="TCap">Capability interface type</typeparam> /// <param name="policy">Policy to attach</param> /// <param name="cap">Capability to censor</param> /// <returns>Censored capability instance</returns> /// <exception cref="ArgumentNullException"><paramref name="policy"/> is null or /// <paramref name="cap"/> is null</exception> public static TCap Attach <TCap>(this IInterceptionPolicy policy, TCap cap) where TCap : class { if (policy == null) { throw new ArgumentNullException(nameof(policy)); } if (cap == null) { throw new ArgumentNullException(nameof(cap)); } var cur = cap as CensorCapability; while (cur != null) { if (policy.Equals(cur.Policy)) { return(cap); } cur = cur.InterceptedCapability as CensorCapability; } switch (cap) { case Proxy proxy: return((CapabilityReflection.CreateProxy <TCap>( Attach(policy, proxy.ConsumedCap !)) as TCap) !); case ConsumedCapability ccap: return((new CensorCapability(ccap, policy) as TCap) !); default: var temp = (CapabilityReflection.CreateProxy <TCap>( CapabilityReflection.CreateSkeletonInternal(cap).AsCapability())) as TCap; return(Attach(policy, temp !) !); } }
/// <summary> /// Wraps a capability implementation in a Proxy. /// </summary> /// <param name="impl">Capability implementation</param> /// <returns>Proxy</returns> /// <exception cref="System.ArgumentNullException"><paramref name="impl"/> is null.</exception> /// <exception cref="InvalidCapabilityInterfaceException">No <see cref="SkeletonAttribute"/> found on implemented interface(s).</exception> /// <exception cref="System.InvalidOperationException">Mismatch between generic type arguments (if capability interface is generic).</exception> /// <exception cref="System.ArgumentException">Mismatch between generic type arguments (if capability interface is generic).</exception> /// <exception cref="System.Reflection.TargetInvocationException">Problem with instatiating the Skeleton (constructor threw exception).</exception> /// <exception cref="System.MemberAccessException">Caller does not have permission to invoke the Skeleton constructor.</exception> /// <exception cref="System.TypeLoadException">Problem with building the Skeleton type, or problem with loading some dependent class.</exception> public static BareProxy FromImpl(object impl) { return(new BareProxy(CapabilityReflection.CreateSkeletonInternal(impl).AsCapability())); }