protected virtual void Dispose(bool disposing)
 {
     if (!_disposed)
     {
         _connection.Dispose();
         _connection = null;
         _disposed   = true;
     }
 }
        public IMethodReturn Invoke(IMethodInvocation input, GetNextInterceptionBehaviorDelegate getNext)
        {
            var methodInfo = (MethodInfo)input.MethodBase;

            // Catch the Dispose() function
            if (ReflectionHelper.IsExplicitMethod(methodInfo, typeof(IDisposable), "Dispose"))
            {
                Dispose();
                return(input.CreateMethodReturn(null));
            }

            if (_connection == null)
            {
                _connection = new PowerShellConnection(_filePath);
            }

            try
            {
                object result    = null;
                var    arguments = input.Arguments.Cast <object>().ToArray();

                // Handle GET property
                if (ReflectionHelper.IsGetProperty(methodInfo))
                {
                    result = _connection.ReadGlobalVariable(ReflectionHelper.GetPropertyName(methodInfo), methodInfo.ReturnType);
                }
                // Handle SET property
                else if (ReflectionHelper.IsSetProperty(methodInfo))
                {
                    _connection.AssignGlobalVariable(ReflectionHelper.GetPropertyName(methodInfo), arguments[0]);
                }
                // Run the function
                else
                {
                    result = _connection.Run(input.MethodBase.Name, methodInfo.ReturnType, arguments);
                }

                // Return the results from the script
                return(input.CreateMethodReturn(result, arguments));
            }
            catch (Exception ex)
            {
                return(input.CreateExceptionMethodReturn(ex));
            }
        }