Represents a type that adds method overrides for interface methods.
Пример #1
0
        /// <summary>
        /// Overrides all methods in the given interface type with methods that throw a <see cref="NotImplementedException"/>.
        /// </summary>
        /// <param name="interfaceType">The interface type that will be implemented by the target type.</param>
        /// <param name="type">The target type.</param>
        /// <returns>The list of stubbed methods.</returns>
        private static IEnumerable<MethodDefinition> CreateInterfaceStub(System.Type interfaceType, TypeDefinition type)
        {
            var module = type.Module;
            var overrider = new MethodOverrider();
            var methods = interfaceType.GetMethods();
            var stubbedMethods = new List<MethodDefinition>();
            foreach (var method in methods)
            {
                var newMethod = CreateMethodStub(type, module, overrider, method);
                stubbedMethods.Add(newMethod);
            }

            return stubbedMethods;
        }
Пример #2
0
        /// <summary>
        /// Overrides the target <paramref name="method"/> with a method that throws a <see cref="NotImplementedException"/>.
        /// </summary>
        /// <param name="type">The target type.</param>
        /// <param name="module">The host module.</param>
        /// <param name="overrider">The <see cref="MethodOverrider"/> that will be used to override the target method.</param>
        /// <param name="method">The target method.</param>
        /// <returns>The stubbed method.</returns>
        private static MethodDefinition CreateMethodStub(TypeDefinition type, ModuleDefinition module, MethodOverrider overrider, MethodInfo method)
        {
            // Import the NotImplementedException type
            var notImplementedCtor = module.ImportConstructor<NotImplementedException>();
            var currentMethod = overrider.AddOverrideFor(method, type);

            // Create the method stub
            var body = currentMethod.Body;
            var il = body.GetILProcessor();

            il.Emit(OpCodes.Newobj, notImplementedCtor);
            il.Emit(OpCodes.Throw);

            return currentMethod;
        }
Пример #3
0
        /// <summary>
        /// Overrides all methods in the given interface type with methods that throw a <see cref="NotImplementedException"/>.
        /// </summary>
        /// <param name="interfaceType">The interface type that will be implemented by the target type.</param>
        /// <param name="type">The target type.</param>
        /// <returns>The list of stubbed methods.</returns>
        private static IEnumerable <MethodDefinition> CreateInterfaceStub(System.Type interfaceType, TypeDefinition type)
        {
            var module         = type.Module;
            var overrider      = new MethodOverrider();
            var methods        = interfaceType.GetMethods();
            var stubbedMethods = new List <MethodDefinition>();

            foreach (var method in methods)
            {
                var newMethod = CreateMethodStub(type, module, overrider, method);
                stubbedMethods.Add(newMethod);
            }

            return(stubbedMethods);
        }
Пример #4
0
        /// <summary>
        /// Overrides the target <paramref name="method"/> with a method that throws a <see cref="NotImplementedException"/>.
        /// </summary>
        /// <param name="type">The target type.</param>
        /// <param name="module">The host module.</param>
        /// <param name="overrider">The <see cref="MethodOverrider"/> that will be used to override the target method.</param>
        /// <param name="method">The target method.</param>
        /// <returns>The stubbed method.</returns>
        private static MethodDefinition CreateMethodStub(TypeDefinition type, ModuleDefinition module, MethodOverrider overrider, MethodInfo method)
        {
            // Import the NotImplementedException type
            var notImplementedCtor = module.ImportConstructor <NotImplementedException>();
            var currentMethod      = overrider.AddOverrideFor(method, type);

            // Create the method stub
            var body = currentMethod.Body;
            var il   = body.GetILProcessor();

            il.Emit(OpCodes.Newobj, notImplementedCtor);
            il.Emit(OpCodes.Throw);

            return(currentMethod);
        }