/// <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 IReflectionVisitable target, INewInstanceFilter newInstanceFilter,
                                                 IMethodFilter methodFilter)
        {
            var redirector = new RedirectNewInstancesToActivator(newInstanceFilter);

            target.InterceptNewInstancesWith(redirector, methodFilter.ShouldWeave);
        }
        /// <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 the types that will be modified.</param>
        public static void InterceptMethodCalls(this IReflectionVisitable target, Func <TypeReference, bool> typeFilter)
        {
            var hostMethodFilter = GetHostMethodFilter();
            Func <MethodReference, bool> methodCallFilter = m => true;

            InterceptMethodCalls(target, typeFilter, hostMethodFilter, methodCallFilter);
        }
예제 #3
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 IReflectionVisitable 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);
        }
        /// <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 IReflectionVisitable target, INewObjectWeaver weaver,
                                                     Func <MethodReference, bool> filter)
        {
            var interceptNewCalls = new InterceptNewCalls(weaver);

            target.WeaveWith(interceptNewCalls, filter);
        }
예제 #5
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 IReflectionVisitable target)
        {
            Func <MethodReference, bool> hostMethodFilter = GetHostMethodFilter();
            Func <MethodReference, bool> methodCallFilter = m => true;

            InterceptMethodCalls(target, GetDefaultTypeFilter(), hostMethodFilter, methodCallFilter);
        }
예제 #6
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 IReflectionVisitable targetType)
        {
            Func <MethodReference, bool> methodFilter = GetMethodFilter();
            Func <FieldReference, bool>  fieldFilter  = GetFieldFilter(actualField => actualField.IsStatic);

            targetType.InterceptFields(methodFilter, fieldFilter);
        }
        /// <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 IReflectionVisitable targetType)
        {
            var methodFilter = GetMethodFilter();
            var fieldFilter  = GetFieldFilter();

            targetType.InterceptFields(methodFilter, fieldFilter);
        }
예제 #8
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 IReflectionVisitable target, IMethodRewriter rewriter,
                                     Func <MethodReference, bool> filter)
        {
            var weaver = new MethodWeaver(rewriter, filter);

            target.Accept(weaver);
        }
        /// <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 IReflectionVisitable 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);
        }
        /// <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 IReflectionVisitable target, IMethodCallFilter methodCallFilter,
                                                IMethodFilter hostMethodFilter)
        {
            var rewriter = new InterceptMethodCalls(methodCallFilter);

            target.Accept(new ImplementModifiableType(GetDefaultTypeFilter()));
            target.WeaveWith(rewriter, hostMethodFilter.ShouldWeave);
        }
예제 #11
0
        public static void InterceptMethodBody(this IReflectionVisitable target,
                                               Func <MethodReference, bool> methodFilter, Func <TypeReference, bool> typeFilter)
        {
            target.Accept(new ImplementModifiableType(typeFilter));

            var interceptMethodBody = new InterceptMethodBody(methodFilter);

            target.WeaveWith(interceptMethodBody, methodFilter);
        }
예제 #12
0
        /// <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="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 IReflectionVisitable 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 IReflectionVisitable visitable, Func <MethodReference, bool> methodFilter)
        {
            if (visitable == null)
            {
                throw new ArgumentNullException("visitable");
            }

            var catchAllThrownExceptions = new CatchAllThrownExceptions();

            visitable.WeaveWith(catchAllThrownExceptions, methodFilter);
        }
예제 #14
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 IReflectionVisitable 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);
        }
        private void LoadNodeOnDemand(TreeNode node)
        {
            if (node.Nodes.ContainsKey(EXPANDER_NODE_KEY))
            {
                node.Nodes.RemoveAt(node.Nodes.IndexOfKey(EXPANDER_NODE_KEY));
            }
            if ((node.Tag) is IReflectionVisitable)
            {
                IReflectionVisitable visitable = (IReflectionVisitable)node.Tag;
                if (!m_visiteditems.ContainsKey(visitable))
                {
                    visitable.Accept(this);
                    m_visiteditems.Add(visitable, visitable);
                }
            }
            else if ((node.Tag) is IAssemblyWrapper)
            {
                IAssemblyWrapper iasm = (IAssemblyWrapper)node.Tag;

                IAssemblyContext context = PluginFactory.GetInstance().GetAssemblyContext(iasm.Location);
                if (context != null)
                {
                    AssemblyDefinition asmdef = context.AssemblyDefinition;

                    if ((AssemblyRestriction == null) || (asmdef == AssemblyRestriction))
                    {
                        m_nodes.Remove(node.Tag);
                        m_nodes.Add(asmdef, node);
                        node.Tag = asmdef;

                        foreach (ModuleDefinition moddef in asmdef.Modules)
                        {
                            AppendNode(asmdef, moddef, moddef.Types.Count > 0);
                        }
                    }
                    else
                    {
                        node.Tag = "restricted";
                        node.Text += String.Format(" (You can't use this assembly for selection -> restricted to {0})", AssemblyRestriction.Name.Name);
                    }
                }
            }
        }
        /// <summary>
        /// Enables exception interception on the given type.
        /// </summary>
        /// <param name="visitable">The target type.</param>
        public static void InterceptAllExceptions(this IReflectionVisitable visitable)
        {
            Func <MethodReference, bool> filter = GetMethodFilter();

            InterceptExceptions(visitable, filter);
        }
예제 #17
0
 /// <summary>
 /// Intercepts all method bodies on the target item.
 /// </summary>
 /// <param name="target">The target to be modified.</param>
 /// <param name="methodFilter">The method filter that will determine the methods that will be modified.</param>
 public static void InterceptMethodBody(this IReflectionVisitable target, IMethodFilter methodFilter)
 {
     target.InterceptMethodBody(methodFilter.ShouldWeave);
 }
        /// <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 IReflectionVisitable target)
        {
            Func <TypeReference, bool> 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 IReflectionVisitable visitable)
        {
            var filter = GetMethodFilter();

            InterceptExceptions(visitable, filter);
        }
 /// <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 IReflectionVisitable target, Func <TypeReference, bool> typeFilter)
 {
     target.InterceptNewInstances(typeFilter, m => true);
 }
예제 #21
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 IReflectionVisitable visitable, IMethodWeaver methodWeaver)
        {
            var visitor = new MethodWeaverVisitor(methodWeaver);

            visitable.Accept(visitor);
        }
예제 #22
0
        /// <summary>
        /// Allows a <see cref="ITypeWeaver"/> instance to traverse any <see cref="IReflectionVisitable"/>
        /// instance.
        /// </summary>
        /// <param name="visitable">The visitable object.</param>
        /// <param name="typeWeaver">The type weaver.</param>
        public static void Accept(this IReflectionVisitable visitable, ITypeWeaver typeWeaver)
        {
            var visitor = new TypeWeaverVisitor(typeWeaver);

            visitable.Accept(visitor);
        }
 /// <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 IReflectionVisitable visitable, IMethodFilter methodFilter)
 {
     visitable.InterceptExceptions(methodFilter.ShouldWeave);
 }
예제 #24
0
 public static void InterceptAllMethodBodies(this IReflectionVisitable target)
 {
     target.InterceptMethodBody(m => true);
 }
예제 #25
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 IReflectionVisitable target)
        {
            var typeFilter = GetTypeFilter();

            target.InterceptNewInstances(typeFilter);
        }
예제 #26
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 IReflectionVisitable targetType)
        {
            Func <MethodReference, bool> methodFilter = GetMethodFilter();

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