/// <summary>
        /// Modifies the <paramref name="target"/> to support intercepting calls to the 'new' operator.
        /// </summary>
        /// <param name="target">The item to be modified.</param>
        /// <param name="newInstanceFilter">The filter that will determine which constructor calls should be intercepted.</param>
        /// <param name="methodFilter">The filter that will determine which host methods should be modified to support new instance interception.</param>
        public static void InterceptNewInstances(this IReflectionStructureVisitable target,
                                                 INewInstanceFilter newInstanceFilter, IMethodFilter methodFilter)
        {
            var redirector = new RedirectNewInstancesToActivator(newInstanceFilter);

            target.InterceptNewInstancesWith(redirector, methodFilter.ShouldWeave);
        }
        /// <summary>
        /// Adds field interception support intercepting all static fields on the target type.
        /// </summary>
        /// <param name="targetType">The type that will be modified.</param>
        public static void InterceptAllStaticFields(this IReflectionStructureVisitable targetType)
        {
            var methodFilter = GetMethodFilter();
            var fieldFilter  = GetFieldFilter();

            targetType.InterceptFields(methodFilter, fieldFilter);
        }
        /// <summary>
        /// Modifies the methods in the given <paramref name="target"/> using the custom <see cref="INewObjectWeaver"/> instance.
        /// </summary>
        /// <param name="target">The host that contains the methods that will be modified.</param>
        /// <param name="weaver">The custom <see cref="INewObjectWeaver"/> that will replace all calls to the new operator with the custom code emitted by the given weaver.</param>
        /// <param name="filter">The method filter that will determine which methods should be modified.</param>
        public static void InterceptNewInstancesWith(this IReflectionStructureVisitable target, INewObjectWeaver weaver,
                                                     Func <MethodReference, bool> filter)
        {
            var interceptNewCalls = new InterceptNewCalls(weaver);

            target.WeaveWith(interceptNewCalls, filter);
        }
Пример #4
0
        /// <summary>
        /// Modifies the current <paramref name="target"/> to support third-party method call interception.
        /// </summary>
        /// <param name="target">The target object.</param>
        /// <param name="typeFilter">The filter that will determine the target types that will be modified.</param>
        /// <param name="hostMethodFilter">The filter that will determine the methods that will be modified on the target type.</param>
        /// <param name="methodCallFilter">The filter that will determine which third-party methods will be intercepted on the target type.</param>
        public static void InterceptMethodCalls(this IReflectionStructureVisitable target, Func <TypeReference, bool> typeFilter, Func <MethodReference, bool> hostMethodFilter, Func <MethodReference, bool> methodCallFilter)
        {
            var rewriter = new InterceptMethodCalls(hostMethodFilter, methodCallFilter);

            target.Accept(new ImplementModifiableType(typeFilter));
            target.WeaveWith(rewriter, hostMethodFilter);
        }
Пример #5
0
        /// <summary>
        /// Adds field interception support intercepting all static fields on the target type.
        /// </summary>
        /// <param name="targetType">The type that will be modified.</param>
        public static void InterceptAllStaticFields(this IReflectionStructureVisitable targetType)
        {
            Func <MethodReference, bool> methodFilter = GetMethodFilter();
            Func <FieldReference, bool>  fieldFilter  = GetFieldFilter(f => f.IsStatic);

            targetType.InterceptFields(methodFilter, fieldFilter);
        }
Пример #6
0
        /// <summary>
        /// Transforms the methods in the <paramref name="target"/> using the given method rewriter.
        /// </summary>
        /// <param name="target">The transformation target.</param>
        /// <param name="rewriter">The method rewriter.</param>
        /// <param name="filter">The method filter that determines which methods will be rewritten.</param>
        public static void WeaveWith(this IReflectionStructureVisitable target, IMethodRewriter rewriter,
                                     Func <MethodReference, bool> filter)
        {
            var weaver = new MethodWeaver(rewriter, filter);

            target.Accept(weaver);
        }
        /// <summary>
        /// Modifies the current <paramref name="target"/> to support third-party method call interception for all method calls made inside the target.
        /// </summary>
        /// <param name="target">The target object.</param>
        /// <param name="typeFilter">The type filter that determines which types will be modified for interception.</param>
        public static void InterceptMethodCalls(this IReflectionStructureVisitable target,
                                                Func <TypeReference, bool> typeFilter)
        {
            var hostMethodFilter = GetHostMethodFilter();
            Func <MethodReference, bool> methodCallFilter = m => true;

            InterceptMethodCalls(target, typeFilter, hostMethodFilter, methodCallFilter);
        }
        /// <summary>
        /// Adds field interception support to the target type.
        /// </summary>
        /// <param name="targetType">The type that will be modified.</param>
        /// <param name="methodFilter">The filter that determines which methods on the target type will be modified to support field interception.</param>
        /// <param name="fieldFilter">The filter that determines which fields should be intercepted.</param>
        public static void InterceptFields(this IReflectionStructureVisitable targetType, Func <MethodReference, bool> methodFilter, Func <FieldReference, bool> fieldFilter)
        {
            var typeWeaver  = new ImplementFieldInterceptionHostWeaver(t => true);
            var fieldWeaver = new InterceptFieldAccess(fieldFilter);

            targetType.WeaveWith(fieldWeaver, methodFilter);
            targetType.Accept(typeWeaver);
        }
Пример #9
0
        /// <summary>
        /// Adds field interception support to the target type.
        /// </summary>
        /// <param name="targetType">The type that will be modified.</param>
        /// <param name="hostTypeFilter">The filter that determines the host types to be modified.</param>
        /// <param name="fieldFilter">The field filter that determines the fields that will be intercepted.</param>
        public static void InterceptFields(this IReflectionStructureVisitable targetType, ITypeFilter hostTypeFilter,
                                           IFieldFilter fieldFilter)
        {
            var typeWeaver  = new ImplementFieldInterceptionHostWeaver(hostTypeFilter.ShouldWeave);
            var fieldWeaver = new InterceptFieldAccess(fieldFilter);

            targetType.WeaveWith(fieldWeaver, m => true);
            targetType.Accept(typeWeaver);
        }
Пример #10
0
        public static void InterceptMethodBody(this IReflectionStructureVisitable target,
                                               Func <MethodReference, bool> methodFilter, Func <TypeReference, bool> typeFilter)
        {
            target.Accept(new ImplementModifiableType(typeFilter));

            var interceptMethodBody = new InterceptMethodBody(methodFilter);

            target.WeaveWith(interceptMethodBody, methodFilter);
        }
        /// <summary>
        /// Modifies the current <paramref name="target"/> to support third-party method call interception for all method calls made inside the target.
        /// </summary>
        /// <param name="target">The target object.</param>
        /// <param name="methodCallFilter">The <see cref="IMethodCallFilter"/> instance that determines the method calls that will be intercepted.</param>
        /// <param name="hostMethodFilter">The <see cref="IMethodFilter"/> instance that determines the host method calls that will be modified</param>
        public static void InterceptMethodCalls(this IReflectionStructureVisitable target,
                                                IMethodCallFilter methodCallFilter,
                                                IMethodFilter hostMethodFilter)
        {
            var rewriter = new InterceptMethodCalls(methodCallFilter);

            target.Accept(new ImplementModifiableType(GetDefaultTypeFilter()));
            target.WeaveWith(rewriter, hostMethodFilter.ShouldWeave);
        }
Пример #12
0
        /// <summary>
        /// Modifies a <paramref name="target"/> assembly to support intercepting calls to the 'new' operator.
        /// </summary>
        /// <param name="target">The assembly to be modified.</param>
        /// <param name="constructorFilter">The functor that determines which type instantiations should be intercepted.</param>
        /// <param name="methodFilter">The filter that determines which host methods will be modified</param>
        /// <remarks>
        /// The type filter determines which concrete types and constructors should be intercepted at runtime.
        /// For example, the following functor code intercepts types named "Foo":
        /// <code>
        ///     Func&lt;MethodReference, TypeReference, bool&gt; filter =
        ///     (constructor, concreteType, hostMethod) => concreteType.Name == "Foo";
        /// </code>
        /// </remarks>
        public static void InterceptNewInstances(this IReflectionStructureVisitable target, Func <MethodReference, TypeReference, bool> constructorFilter,
                                                 Func <MethodReference, bool> methodFilter)
        {
            Func <MethodReference, TypeReference, MethodReference, bool> filter =
                (ctor, declaringType, declaringMethod) => constructorFilter(ctor, declaringType) && methodFilter(declaringMethod);

            var redirector = new RedirectNewInstancesToActivator(filter);

            target.InterceptNewInstancesWith(redirector, methodFilter);
        }
        public static void InterceptExceptions(this IReflectionStructureVisitable visitable, Func <MethodReference, bool> methodFilter)
        {
            if (visitable == null)
            {
                throw new ArgumentNullException("visitable");
            }

            var catchAllThrownExceptions = new CatchAllThrownExceptions();

            visitable.WeaveWith(catchAllThrownExceptions, methodFilter);
        }
Пример #14
0
        private static void InterceptMethodBodies(IReflectionStructureVisitable assembly, string targetDirectory)
        {
            var methodFilter = LoadFirstInstanceOf <IMethodFilter>(targetDirectory);

            if (methodFilter != null)
            {
                assembly.InterceptMethodBody(methodFilter);
                return;
            }

            assembly.InterceptAllMethodBodies();
        }
Пример #15
0
        private static void InterceptFields(IReflectionStructureVisitable assembly, string targetDirectory)
        {
            var fieldFilter    = LoadFirstInstanceOf <IFieldFilter>(targetDirectory);
            var hostTypeFilter = LoadFirstInstanceOf <ITypeFilter>(targetDirectory);

            if (fieldFilter != null && hostTypeFilter != null)
            {
                assembly.InterceptFields(hostTypeFilter, fieldFilter);
                return;
            }

            assembly.InterceptAllFields();
        }
Пример #16
0
        private static void InterceptMethodCalls(IReflectionStructureVisitable assembly, string sourceDirectory)
        {
            var methodCallFilter = LoadFirstInstanceOf <IMethodCallFilter>(sourceDirectory);
            var hostMethodFilter = LoadFirstInstanceOf <IMethodFilter>(sourceDirectory);

            if (methodCallFilter != null && hostMethodFilter != null)
            {
                assembly.InterceptMethodCalls(methodCallFilter, hostMethodFilter);
                return;
            }

            assembly.InterceptAllMethodCalls();
        }
Пример #17
0
        /// <summary>
        /// Modifies the current <paramref name="target"/> to support third-party method call interception for all method calls made inside the target.
        /// </summary>
        /// <param name="target">The target object.</param>
        public static void InterceptAllMethodCalls(this IReflectionStructureVisitable target)
        {
            Func <TypeReference, bool> typeFilter = type =>
            {
                var actualType = type.Resolve();
                return(!actualType.IsValueType && !actualType.IsInterface);
            };

            var hostMethodFilter = GetHostMethodFilter();
            Func <MethodReference, bool> methodCallFilter = m => true;

            InterceptMethodCalls(target, typeFilter, hostMethodFilter, methodCallFilter);
        }
Пример #18
0
        private static void InterceptNewInstances(IReflectionStructureVisitable assembly, string targetDirectory)
        {
            var newInstanceFilter = LoadFirstInstanceOf <INewInstanceFilter>(targetDirectory);
            var methodFilter      = LoadFirstInstanceOf <IMethodFilter>(targetDirectory);

            if (newInstanceFilter != null && methodFilter != null)
            {
                assembly.InterceptNewInstances(newInstanceFilter, methodFilter);
                return;
            }

            assembly.InterceptAllNewInstances();
        }
        /// <summary>
        /// Enables exception interception on the given type.
        /// </summary>
        /// <param name="visitable">The target type.</param>
        public static void InterceptAllExceptions(this IReflectionStructureVisitable visitable)
        {
            Func <MethodReference, bool> filter = GetMethodFilter();

            InterceptExceptions(visitable, filter);
        }
Пример #20
0
 public static void InterceptMethodBody(this IReflectionStructureVisitable target, IMethodFilter methodFilter)
 {
     target.InterceptMethodBody(methodFilter.ShouldWeave);
 }
 /// <summary>
 /// Enables exception interception on the given type.
 /// </summary>
 /// <param name="visitable">The target type.</param>
 /// <param name="methodFilter">The <see cref="IMethodFilter"/> instance that will determine which methods should support exception interception.</param>
 public static void InterceptExceptions(this IReflectionStructureVisitable visitable, IMethodFilter methodFilter)
 {
     visitable.InterceptExceptions(methodFilter.ShouldWeave);
 }
Пример #22
0
 public static void InterceptAllMethodBodies(this IReflectionStructureVisitable target)
 {
     target.InterceptMethodBody(m => true);
 }
        /// <summary>
        /// Modifies a <paramref name="target"/> to support intercepting all calls to the 'new' operator.
        /// </summary>
        /// <param name="target">The assembly to be modified.</param>
        public static void InterceptAllNewInstances(this IReflectionStructureVisitable target)
        {
            Func <TypeReference, bool> typeFilter = GetTypeFilter();

            target.InterceptNewInstances(typeFilter);
        }
 /// <summary>
 /// Modifies a <paramref name="target"/> assembly to support intercepting calls to the 'new' operator.
 /// </summary>
 /// <param name="target">The assembly to be modified.</param>
 /// <param name="typeFilter">The functor that determines which type instantiations should be intercepted.</param>
 /// <remarks>
 /// The type filter determines the concrete types that should be intercepted at runtime.
 /// For example, the following functor code intercepts types named "Foo":
 /// <code>
 ///     Func&lt;TypeReference, bool&gt; filter =
 ///     concreteType => concreteType.Name == "Foo";
 /// </code>
 /// </remarks>
 public static void InterceptNewInstances(this IReflectionStructureVisitable target,
                                          Func <TypeReference, bool> typeFilter)
 {
     target.InterceptNewInstances(typeFilter, m => true);
 }
Пример #25
0
        /// <summary>
        /// Allows a <see cref="IMethodWeaver"/> instance to traverse any <see cref="IReflectionVisitable"/>
        /// instance.
        /// </summary>
        /// <param name="visitable">The visitable object.</param>
        /// <param name="methodWeaver">The method weaver.</param>
        public static void Accept(this IReflectionStructureVisitable visitable, IMethodWeaver methodWeaver)
        {
            var visitor = new MethodWeaverVisitor(methodWeaver);

            visitable.Accept(visitor);
        }
Пример #26
0
        /// <summary>
        /// Allows a <see cref="ITypeWeaver"/> instance to traverse any <see cref="IReflectionStructureVisitable"/>
        /// instance.
        /// </summary>
        /// <param name="visitable">The visitable object.</param>
        /// <param name="typeWeaver">The type weaver.</param>
        public static void Accept(this IReflectionStructureVisitable visitable, ITypeWeaver typeWeaver)
        {
            var visitor = new TypeWeaverVisitor(typeWeaver);

            visitable.Accept(visitor);
        }
Пример #27
0
        /// <summary>
        /// Modifies a <paramref name="target"/> to support intercepting all calls to the 'new' operator.
        /// </summary>
        /// <param name="target">The assembly to be modified.</param>
        public static void InterceptAllNewInstances(this IReflectionStructureVisitable target)
        {
            var typeFilter = GetTypeFilter();

            target.InterceptNewInstances(typeFilter);
        }
        /// <summary>
        /// Enables exception interception on the given type.
        /// </summary>
        /// <param name="visitable">The target type.</param>
        public static void InterceptAllExceptions(this IReflectionStructureVisitable visitable)
        {
            var filter = GetMethodFilter();

            InterceptExceptions(visitable, filter);
        }
 /// <summary>
 /// Modifies the current <paramref name="target"/> to support third-party method call interception for all method calls made inside the target.
 /// </summary>
 /// <param name="target">The target object.</param>
 public static void InterceptAllMethodCalls(this IReflectionStructureVisitable target)
 {
     target.InterceptMethodCalls(GetDefaultTypeFilter());
 }
Пример #30
0
        /// <summary>
        /// Adds field interception support to the target type.
        /// </summary>
        /// <param name="targetType">The type that will be modified.</param>
        public static void InterceptAllFields(this IReflectionStructureVisitable targetType)
        {
            Func <MethodReference, bool> methodFilter = GetMethodFilter();

            targetType.InterceptFields(methodFilter, f => true);
        }