Exemplo n.º 1
0
        /// <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 TypeDefinition targetType,
                                           Func <MethodReference, bool> methodFilter,
                                           Func <FieldReference, bool> fieldFilter)
        {
            var typeWeaver  = new ImplementFieldInterceptionHostWeaver(t => true);
            var fieldWeaver = new InterceptFieldAccess(fieldFilter);

            typeWeaver.Weave(targetType);
            var targetMethods = targetType.Methods.Where(m => methodFilter(m)).ToArray();

            foreach (var method in targetMethods)
            {
                fieldWeaver.Rewrite(method, method.GetILGenerator(), method.Body.Instructions.ToArray());
            }
        }
Exemplo n.º 2
0
        /// <summary>
        ///     Adds field interception support to the target type.
        /// </summary>
        /// <param name="targetAssembly">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 AssemblyDefinition targetAssembly, ITypeFilter hostTypeFilter,
                                           IFieldFilter fieldFilter)
        {
            var typeWeaver  = new ImplementFieldInterceptionHostWeaver(hostTypeFilter.ShouldWeave);
            var fieldWeaver = new InterceptFieldAccess(fieldFilter);

            var module      = targetAssembly.MainModule;
            var targetTypes = module.Types.Where(hostTypeFilter.ShouldWeave).ToArray();

            foreach (var type in targetTypes)
            {
                typeWeaver.Weave(type);
                foreach (var method in type.Methods.Where(m => m.HasBody))
                {
                    fieldWeaver.Rewrite(method, method.GetILGenerator(), method.Body.Instructions.ToArray());
                }
            }
        }
Exemplo n.º 3
0
        /// <summary>
        ///     Adds field interception support to the target type.
        /// </summary>
        /// <param name="targetAssembly">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 AssemblyDefinition targetAssembly,
                                           Func <MethodReference, bool> methodFilter,
                                           Func <FieldReference, bool> fieldFilter)
        {
            var typeWeaver  = new ImplementFieldInterceptionHostWeaver(t => t.IsByReference && !t.IsValueType);
            var fieldWeaver = new InterceptFieldAccess(fieldFilter);

            var module      = targetAssembly.MainModule;
            var targetTypes = module.Types.Where(t => t.Methods.Any(m => methodFilter(m))).ToArray();

            foreach (var type in targetTypes)
            {
                typeWeaver.Weave(type);
                foreach (var method in type.Methods.Where(m => m.HasBody))
                {
                    fieldWeaver.Rewrite(method, method.GetILGenerator(), method.Body.Instructions.ToArray());
                }
            }
        }