Exemplo n.º 1
0
 /// <summary>
 /// Creates an instance of an ImplicitInterceptor which can intercept
 /// interface calls and route them to the implicit instance implementations
 /// of the specific methods.
 /// </summary>
 /// <param name="instance">The instance being wrapped by this interceptor
 /// which does not implement the specific interface.</param>
 /// <param name="methodsCache">A supplied cache that will help increase
 /// performance when fetching for an implementation of a Method.</param>
 /// <param name="proxyWrapperFactory">The proxy factory that will be used
 /// recursively to get any children implicit implementations.</param>
 public ImplicitInterceptor(object instance,
                            IDictionary <MethodInfo, MethodInfo> methodsCache,
                            IProxyWrapperFactory proxyWrapperFactory)
 {
     _instance            = instance;
     _methodsCache        = methodsCache;
     _proxyWrapperFactory = proxyWrapperFactory;
 }
Exemplo n.º 2
0
        /// <summary>
        /// Wraps the current instance as the specific interface by implementing it
        /// implicitly. However, a NotImplementedException exception is thrown if the
        /// interface method is not implemented by the instance.
        /// </summary>
        /// <typeparam name="TInterface">The interface type to</typeparam>
        /// <param name="instance">The instance to wrap.</param>
        /// <param name="factory">The factory engine that generates the
        /// wrapped instance.</param>
        /// <returns>The wrapped instance as the specific interface.</returns>
        public static TInterface As <TInterface>(this object instance,
                                                 IProxyWrapperFactory factory) where TInterface : class
        {
            if (instance == null)
            {
                throw new ArgumentNullException(nameof(instance));
            }
            if (factory == null)
            {
                throw new ArgumentNullException(nameof(factory));
            }

            if (!typeof(TInterface).IsInterface)
            {
                throw new InvalidOperationException(
                          $"{typeof(TInterface).Name} is not an interface!");
            }

            return(factory.Wrap(typeof(TInterface), instance) as TInterface);
        }