Ejemplo n.º 1
0
        /// <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:
                return(Attach(policy,
                              (CapabilityReflection.CreateProxy <TCap>(
                                   LocalCapability.Create(
                                       Skeleton.GetOrCreateSkeleton(cap, false))) as TCap) !));
            }
        }
Ejemplo n.º 2
0
 /// <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(LocalCapability.Create(CapabilityReflection.CreateSkeleton(impl))));
 }