示例#1
0
        private static string GetSmartToStringDisplayName(
            object x,
            MshExpressionFactory expressionFactory)
        {
            MshExpressionResult displayName = PSObjectHelper.GetDisplayName(PSObjectHelper.AsPSObject(x), expressionFactory);

            return(displayName != null && displayName.Exception == null?PSObjectHelper.AsPSObject(displayName.Result).ToString() : PSObjectHelper.AsPSObject(x).ToString());
        }
示例#2
0
        private static string GetSmartToStringDisplayName(object x, PSPropertyExpressionFactory expressionFactory)
        {
            PSPropertyExpressionResult r = PSObjectHelper.GetDisplayName(PSObjectHelper.AsPSObject(x), expressionFactory);

            if ((r != null) && (r.Exception == null))
            {
                return(PSObjectHelper.AsPSObject(r.Result).ToString());
            }
            else
            {
                return(PSObjectHelper.AsPSObject(x).ToString());
            }
        }
示例#3
0
        private static string GetObjectName(object x, PSPropertyExpressionFactory expressionFactory)
        {
            string objName;

            // check if the underlying object is of primitive type
            // if so just return its value
            if (x is PSObject &&
                (LanguagePrimitives.IsBoolOrSwitchParameterType((((PSObject)x).BaseObject).GetType()) ||
                 LanguagePrimitives.IsNumeric(((((PSObject)x).BaseObject).GetType()).GetTypeCode()) ||
                 LanguagePrimitives.IsNull(x)))
            {
                objName = x.ToString();
            }
            else if (x == null)
            {
                // use PowerShell's $null variable to indicate that the value is null...
                objName = "$null";
            }
            else
            {
                MethodInfo toStringMethod = x.GetType().GetMethod("ToString", PSTypeExtensions.EmptyTypes);
                // TODO:CORECLR double check with CORE CLR that x.GetType() == toStringMethod.ReflectedType
                // Check if the given object "x" implements "toString" method. Do that by comparing "DeclaringType" which 'Gets the class that declares this member' and the object type
                if (toStringMethod.DeclaringType == x.GetType())
                {
                    objName = PSObjectHelper.AsPSObject(x).ToString();
                }
                else
                {
                    PSPropertyExpressionResult r = PSObjectHelper.GetDisplayName(PSObjectHelper.AsPSObject(x), expressionFactory);
                    if ((r != null) && (r.Exception == null))
                    {
                        objName = PSObjectHelper.AsPSObject(r.Result).ToString();;
                    }
                    else
                    {
                        objName = PSObjectHelper.AsPSObject(x).ToString();
                        if (objName == string.Empty)
                        {
                            var baseObj = PSObject.Base(x);
                            if (baseObj != null)
                            {
                                objName = baseObj.ToString();
                            }
                        }
                    }
                }
            }

            return(objName);
        }
示例#4
0
 internal static string SmartToString(
     PSObject so,
     MshExpressionFactory expressionFactory,
     int enumerationLimit,
     StringFormatError formatErrorObject)
 {
     if (so == null)
     {
         return("");
     }
     try
     {
         IEnumerable enumerable = PSObjectHelper.GetEnumerable((object)so);
         if (enumerable == null)
         {
             return(so.ToString());
         }
         StringBuilder stringBuilder = new StringBuilder();
         stringBuilder.Append("{");
         bool flag = true;
         int  num  = 0;
         if (enumerable.GetEnumerator() != null)
         {
             foreach (object obj in enumerable)
             {
                 if (LocalPipeline.GetExecutionContextFromTLS().CurrentPipelineStopping)
                 {
                     throw new PipelineStoppedException();
                 }
                 if (enumerationLimit >= 0)
                 {
                     if (num == enumerationLimit)
                     {
                         stringBuilder.Append("...");
                         break;
                     }
                     ++num;
                 }
                 string str;
                 if (obj is PSObject && (LanguagePrimitives.IsBoolOrSwitchParameterType(((PSObject)obj).BaseObject.GetType()) || LanguagePrimitives.IsNumeric(Type.GetTypeCode(((PSObject)obj).BaseObject.GetType())) || LanguagePrimitives.IsNull(obj)))
                 {
                     str = obj.ToString();
                 }
                 else if (obj == null)
                 {
                     str = "$null";
                 }
                 else
                 {
                     MethodInfo method = obj.GetType().GetMethod("ToString", Type.EmptyTypes, (ParameterModifier[])null);
                     if (method.DeclaringType.Equals(method.ReflectedType))
                     {
                         str = PSObjectHelper.AsPSObject(obj).ToString();
                     }
                     else
                     {
                         MshExpressionResult displayName = PSObjectHelper.GetDisplayName(PSObjectHelper.AsPSObject(obj), expressionFactory);
                         str = displayName == null || displayName.Exception != null?PSObjectHelper.AsPSObject(obj).ToString() : PSObjectHelper.AsPSObject(displayName.Result).ToString();
                     }
                 }
                 if (!flag)
                 {
                     stringBuilder.Append(", ");
                 }
                 stringBuilder.Append(str);
                 if (flag)
                 {
                     flag = false;
                 }
             }
         }
         stringBuilder.Append("}");
         return(stringBuilder.ToString());
     }
     catch (ExtendedTypeSystemException ex)
     {
         if (formatErrorObject != null)
         {
             formatErrorObject.sourceObject = (object)so;
             formatErrorObject.exception    = (Exception)ex;
         }
         return("");
     }
 }