Example #1
0
        private void InvokeActions(Type attribute, object testClass)
        {
            if (attribute == null)
            {
                throw new ArgumentNullException("attribute");
            }
            if (testClass == null)
            {
                throw new ArgumentNullException("testClass");
            }
            IList <MethodInfo> actions = new List <MethodInfo>();
            Type testClassType         = testClass.GetType();

            if (attribute == typeof(ProtocolTestCleanupAttribute))
            {
                if (!this.cleanupActions.ContainsKey(testClassType))
                {
                    this.cleanupActions[testClassType] =
                        TestToolHelpers.GetMethodsByAttribute(attribute, testClassType, true);
                }
                actions = this.cleanupActions[testClassType];
            }
            else if (attribute == typeof(ProtocolTestInitializeAttribute))
            {
                if (!this.initializeActions.ContainsKey(testClassType))
                {
                    this.initializeActions[testClassType] =
                        TestToolHelpers.GetMethodsByAttribute(attribute, testClassType, true);
                }
                actions = this.initializeActions[testClassType];
            }
            else
            {
                throw new InvalidOperationException(
                          "Unexpected attribute found while invoking protocol testing actions.");
            }
            //invoke all actions
            foreach (MethodInfo mi in actions)
            {
                // we don't use MethodInfo.Invoke() because
                // that will wrap any exceptions thrown from the method
                // being invoked into TargetInvocationException.
                Action action = (Action)Delegate.CreateDelegate(
                    typeof(Action),
                    testClass,
                    mi);

                action();
            }
        }