Example #1
0
        /// <summary>
        /// helper to convert an PSObject into a string
        /// It takes into account enumerations (use display name)
        /// </summary>
        /// <param name="so">shell object to process</param>
        /// <param name="expressionFactory">expression factory to create PSPropertyExpression</param>
        /// <param name="enumerationLimit">limit on IEnumerable enumeration</param>
        /// <param name="formatErrorObject">stores errors during string conversion</param>
        /// <returns>string representation</returns>
        internal static string SmartToString(PSObject so, PSPropertyExpressionFactory expressionFactory, int enumerationLimit, StringFormatError formatErrorObject)
        {
            if (so == null)
            {
                return(string.Empty);
            }

            try
            {
                IEnumerable e = PSObjectHelper.GetEnumerable(so);
                if (e != null)
                {
                    StringBuilder sb = new StringBuilder();
                    sb.Append("{");

                    bool        first      = true;
                    int         enumCount  = 0;
                    IEnumerator enumerator = e.GetEnumerator();
                    if (enumerator != null)
                    {
                        IBlockingEnumerator <object> be = enumerator as IBlockingEnumerator <object>;
                        if (be != null)
                        {
                            while (be.MoveNext(false))
                            {
                                if (LocalPipeline.GetExecutionContextFromTLS().CurrentPipelineStopping)
                                {
                                    throw new PipelineStoppedException();
                                }

                                if (enumerationLimit >= 0)
                                {
                                    if (enumCount == enumerationLimit)
                                    {
                                        sb.Append(Ellipsis);
                                        break;
                                    }

                                    enumCount++;
                                }

                                if (!first)
                                {
                                    sb.Append(", ");
                                }

                                sb.Append(GetObjectName(be.Current, expressionFactory));
                                if (first)
                                {
                                    first = false;
                                }
                            }
                        }
                        else
                        {
                            foreach (object x in e)
                            {
                                if (LocalPipeline.GetExecutionContextFromTLS().CurrentPipelineStopping)
                                {
                                    throw new PipelineStoppedException();
                                }

                                if (enumerationLimit >= 0)
                                {
                                    if (enumCount == enumerationLimit)
                                    {
                                        sb.Append(Ellipsis);
                                        break;
                                    }

                                    enumCount++;
                                }

                                if (!first)
                                {
                                    sb.Append(", ");
                                }

                                sb.Append(GetObjectName(x, expressionFactory));
                                if (first)
                                {
                                    first = false;
                                }
                            }
                        }
                    }

                    sb.Append("}");
                    return(sb.ToString());
                }

                // take care of the case there is no base object
                return(so.ToString());
            }
            catch (ExtendedTypeSystemException e)
            {
                // NOTE: we catch all the exceptions, since we do not know
                // what the underlying object access would throw
                if (formatErrorObject != null)
                {
                    formatErrorObject.sourceObject = so;
                    formatErrorObject.exception    = e;
                }

                return(string.Empty);
            }
        }
Example #2
0
        /// <summary>
        /// Helper to convert an PSObject into a string
        /// It takes into account enumerations (use display name)
        /// </summary>
        /// <param name="so">Shell object to process.</param>
        /// <param name="expressionFactory">Expression factory to create PSPropertyExpression.</param>
        /// <param name="enumerationLimit">Limit on IEnumerable enumeration.</param>
        /// <param name="formatErrorObject">Stores errors during string conversion.</param>
        /// <returns>String representation.</returns>
        internal static string SmartToString(PSObject so, PSPropertyExpressionFactory expressionFactory, int enumerationLimit, StringFormatError formatErrorObject)
        {
            if (so == null)
            {
                return(string.Empty);
            }

            try
            {
                IEnumerable e = PSObjectHelper.GetEnumerable(so);
                if (e != null)
                {
                    StringBuilder sb = new StringBuilder();
                    sb.Append('{');

                    bool        first      = true;
                    int         enumCount  = 0;
                    IEnumerator enumerator = e.GetEnumerator();
                    if (enumerator != null)
                    {
                        IBlockingEnumerator <object> be = enumerator as IBlockingEnumerator <object>;
                        if (be != null)
                        {
                            while (be.MoveNext(false))
                            {
                                if (LocalPipeline.GetExecutionContextFromTLS().CurrentPipelineStopping)
                                {
                                    throw new PipelineStoppedException();
                                }

                                if (enumerationLimit >= 0)
                                {
                                    if (enumCount == enumerationLimit)
                                    {
                                        sb.Append(Ellipsis);
                                        break;
                                    }

                                    enumCount++;
                                }

                                if (!first)
                                {
                                    sb.Append(", ");
                                }

                                sb.Append(GetObjectName(be.Current, expressionFactory));
                                if (first)
                                {
                                    first = false;
                                }
                            }
                        }
                        else
                        {
                            foreach (object x in e)
                            {
                                if (LocalPipeline.GetExecutionContextFromTLS().CurrentPipelineStopping)
                                {
                                    throw new PipelineStoppedException();
                                }

                                if (enumerationLimit >= 0)
                                {
                                    if (enumCount == enumerationLimit)
                                    {
                                        sb.Append(Ellipsis);
                                        break;
                                    }

                                    enumCount++;
                                }

                                if (!first)
                                {
                                    sb.Append(", ");
                                }

                                sb.Append(GetObjectName(x, expressionFactory));
                                if (first)
                                {
                                    first = false;
                                }
                            }
                        }
                    }

                    sb.Append('}');
                    return(sb.ToString());
                }

                // take care of the case there is no base object
                return(so.ToString());
            }
            catch (Exception e) when(e is ExtendedTypeSystemException || e is InvalidOperationException)
            {
                // These exceptions are being caught and handled by returning an empty string when
                // the object cannot be stringified due to ETS or an instance in the collection has been modified
                s_tracer.TraceWarning($"SmartToString method: Exception during conversion to string, emitting empty string: {e.Message}");

                if (formatErrorObject != null)
                {
                    formatErrorObject.sourceObject = so;
                    formatErrorObject.exception    = e;
                }

                return(string.Empty);
            }
        }
Example #3
0
 public BlockingThreadPool(IBlockingEnumerator <T> blockingEnumerator, ProcessItemDelegate <T> processItem)
 {
     _blockingEnumerator = blockingEnumerator;
     _processItem        = processItem;
 }
Example #4
0
 internal static string SmartToString(PSObject so, MshExpressionFactory expressionFactory, int enumerationLimit, StringFormatError formatErrorObject)
 {
     if (so == null)
     {
         return("");
     }
     try
     {
         IEnumerable enumerable = GetEnumerable(so);
         if (enumerable != null)
         {
             StringBuilder builder = new StringBuilder();
             builder.Append("{");
             bool        flag       = true;
             int         num        = 0;
             IEnumerator enumerator = enumerable.GetEnumerator();
             if (enumerator != null)
             {
                 IBlockingEnumerator <object> enumerator2 = enumerator as IBlockingEnumerator <object>;
                 if (enumerator2 != null)
                 {
                     while (enumerator2.MoveNext(false))
                     {
                         if (LocalPipeline.GetExecutionContextFromTLS().CurrentPipelineStopping)
                         {
                             throw new PipelineStoppedException();
                         }
                         if (enumerationLimit >= 0)
                         {
                             if (num == enumerationLimit)
                             {
                                 builder.Append("...");
                                 break;
                             }
                             num++;
                         }
                         if (!flag)
                         {
                             builder.Append(", ");
                         }
                         builder.Append(GetObjectName(enumerator2.Current, expressionFactory));
                         if (flag)
                         {
                             flag = false;
                         }
                     }
                 }
                 else
                 {
                     foreach (object obj2 in enumerable)
                     {
                         if (LocalPipeline.GetExecutionContextFromTLS().CurrentPipelineStopping)
                         {
                             throw new PipelineStoppedException();
                         }
                         if (enumerationLimit >= 0)
                         {
                             if (num == enumerationLimit)
                             {
                                 builder.Append("...");
                                 break;
                             }
                             num++;
                         }
                         if (!flag)
                         {
                             builder.Append(", ");
                         }
                         builder.Append(GetObjectName(obj2, expressionFactory));
                         if (flag)
                         {
                             flag = false;
                         }
                     }
                 }
             }
             builder.Append("}");
             return(builder.ToString());
         }
         return(so.ToString());
     }
     catch (ExtendedTypeSystemException exception)
     {
         if (formatErrorObject != null)
         {
             formatErrorObject.sourceObject = so;
             formatErrorObject.exception    = exception;
         }
         return("");
     }
 }