private ObjectDataSourceResult InvokeMethod(ObjectDataSourceMethod method) {
     object instance = null;
     return InvokeMethod(method, true, ref instance);
 }
        /// <devdoc>
        /// Invokes a given method with the specified parameters. This will also raise
        /// the appropriate post event after execution.
        /// </devdoc>
        private ObjectDataSourceResult InvokeMethod(ObjectDataSourceMethod method, bool disposeInstance, ref object instance) {
            // If the method is not static, we need to create an instance of the type

            if (method.MethodInfo.IsStatic) {
                if (instance != null) {
                    ReleaseInstance(instance);
                }
                instance = null;
            }
            else {
                if (instance == null) {
                    // Raise event to allow page developer to supply a custom instance of the type
                    ObjectDataSourceEventArgs objectEventArgs = new ObjectDataSourceEventArgs(null);
                    OnObjectCreating(objectEventArgs);
    
                    // If page developer did not create a custom instance, we attempt to instantiate
                    // the object ourselves, and raise the ObjectCreated event
                    if (objectEventArgs.ObjectInstance == null) {
                        objectEventArgs.ObjectInstance = Activator.CreateInstance(method.Type);
    
                        // Raise ObjectCreated event with the same event args
                        OnObjectCreated(objectEventArgs);
                    }
                    instance = objectEventArgs.ObjectInstance;
                }
            }


            // Call the method and if an exception is thrown, hold on to it to let the page developer attempt to handle it
            object returnValue = null;
            int affectedRows = -1;

            bool eventFired = false;
            object[] parameterValues = null;
            if (method.Parameters != null && method.Parameters.Count > 0) {
                parameterValues = new object[method.Parameters.Count];
                for (int i = 0; i < method.Parameters.Count; i++) {
                    parameterValues[i] = method.Parameters[i];
                }
            }
            try {
                returnValue = method.MethodInfo.Invoke(instance, parameterValues);
            }
            catch (Exception ex) {
                // Collect output parameters
                IDictionary outputParameters = GetOutputParameters(method.MethodInfo.GetParameters(), parameterValues);
                ObjectDataSourceStatusEventArgs statusEventArgs = new ObjectDataSourceStatusEventArgs(returnValue, outputParameters, ex);
                eventFired = true;
                switch (method.Operation) {
                    case DataSourceOperation.Delete:
                        OnDeleted(statusEventArgs);
                        break;
                    case DataSourceOperation.Insert:
                        OnInserted(statusEventArgs);
                        break;
                    case DataSourceOperation.Select:
                        OnSelected(statusEventArgs);
                        break;
                    case DataSourceOperation.SelectCount:
                        OnSelected(statusEventArgs);
                        break;
                    case DataSourceOperation.Update:
                        OnUpdated(statusEventArgs);
                        break;
                }
                affectedRows = statusEventArgs.AffectedRows;

                if (!statusEventArgs.ExceptionHandled) {
                    throw;
                }
            }
            finally {
                // This block is to ensure that we always at least try to raise
                // the Disposing event, even if the other event handlers threw
                // exceptions.
                try {
                    if (eventFired == false) {
                        // Collect output parameters
                        IDictionary outputParameters = GetOutputParameters(method.MethodInfo.GetParameters(), parameterValues);
                        ObjectDataSourceStatusEventArgs statusEventArgs = new ObjectDataSourceStatusEventArgs(returnValue, outputParameters);
                        switch (method.Operation) {
                            case DataSourceOperation.Delete:
                                OnDeleted(statusEventArgs);
                                break;
                            case DataSourceOperation.Insert:
                                OnInserted(statusEventArgs);
                                break;
                            case DataSourceOperation.Select:
                                OnSelected(statusEventArgs);
                                break;
                            case DataSourceOperation.SelectCount:
                                OnSelected(statusEventArgs);
                                break;
                            case DataSourceOperation.Update:
                                OnUpdated(statusEventArgs);
                                break;
                        }
                        affectedRows = statusEventArgs.AffectedRows;
                    }
                }
                finally {
                    // Raise ObjectDisposing event
                    if (instance != null && disposeInstance) {
                        ReleaseInstance(instance);
                        instance = null;
                    }
                }
            }

            return new ObjectDataSourceResult(returnValue, affectedRows);
        }
        private ObjectDataSourceResult InvokeMethod(ObjectDataSourceMethod method, bool disposeInstance, ref object instance)
        {
            if (method.MethodInfo.IsStatic)
            {
                if (instance != null)
                {
                    this.ReleaseInstance(instance);
                }
                instance = null;
            }
            else if (instance == null)
            {
                ObjectDataSourceEventArgs e = new ObjectDataSourceEventArgs(null);
                this.OnObjectCreating(e);
                if (e.ObjectInstance == null)
                {
                    e.ObjectInstance = Activator.CreateInstance(method.Type);
                    this.OnObjectCreated(e);
                }
                instance = e.ObjectInstance;
            }
            object returnValue = null;
            int affectedRows = -1;
            bool flag = false;
            object[] parameters = null;
            if ((method.Parameters != null) && (method.Parameters.Count > 0))
            {
                parameters = new object[method.Parameters.Count];
                for (int i = 0; i < method.Parameters.Count; i++)
                {
                    parameters[i] = method.Parameters[i];
                }
            }
            try
            {
                returnValue = method.MethodInfo.Invoke(instance, parameters);
            }
            catch (Exception exception)
            {
                IDictionary outputParameters = this.GetOutputParameters(method.MethodInfo.GetParameters(), parameters);
                ObjectDataSourceStatusEventArgs args2 = new ObjectDataSourceStatusEventArgs(returnValue, outputParameters, exception);
                flag = true;
                switch (method.Operation)
                {
                    case DataSourceOperation.Delete:
                        this.OnDeleted(args2);
                        break;

                    case DataSourceOperation.Insert:
                        this.OnInserted(args2);
                        break;

                    case DataSourceOperation.Select:
                        this.OnSelected(args2);
                        break;

                    case DataSourceOperation.Update:
                        this.OnUpdated(args2);
                        break;

                    case DataSourceOperation.SelectCount:
                        this.OnSelected(args2);
                        break;
                }
                affectedRows = args2.AffectedRows;
                if (!args2.ExceptionHandled)
                {
                    throw;
                }
            }
            finally
            {
                try
                {
                    if (!flag)
                    {
                        IDictionary dictionary2 = this.GetOutputParameters(method.MethodInfo.GetParameters(), parameters);
                        ObjectDataSourceStatusEventArgs args3 = new ObjectDataSourceStatusEventArgs(returnValue, dictionary2);
                        switch (method.Operation)
                        {
                            case DataSourceOperation.Delete:
                                this.OnDeleted(args3);
                                break;

                            case DataSourceOperation.Insert:
                                this.OnInserted(args3);
                                break;

                            case DataSourceOperation.Select:
                                this.OnSelected(args3);
                                break;

                            case DataSourceOperation.Update:
                                this.OnUpdated(args3);
                                break;

                            case DataSourceOperation.SelectCount:
                                this.OnSelected(args3);
                                break;
                        }
                        affectedRows = args3.AffectedRows;
                    }
                }
                finally
                {
                    if ((instance != null) && disposeInstance)
                    {
                        this.ReleaseInstance(instance);
                        instance = null;
                    }
                }
            }
            return new ObjectDataSourceResult(returnValue, affectedRows);
        }