Ejemplo n.º 1
0
 private void AssertCsIdentifier(string memberName, string csName)
 {
     if (!m_validCsIdentifier.IsMatch(csName))
     {
         var    type    = m_innerFormatInfo.GetType();
         string message = $@"{type.Name} > ""{csName}"" is not a valid C# identifier.";
         if (type == typeof(FormatInfo))
         {
             throw new InvalidOperationException(message);
         }
         throw new CustomizationException(message);
     }
 }
Ejemplo n.º 2
0
        } // end SetDefaultFormatInfoForObject()

        /// <summary>
        ///    Given a PSObject, choose a ViewDefinitionInfo for it. Optionally specify
        ///    the desired view definition type. The ViewDefinitionInfo may come from the
        ///    list of registered views, or from a special property on the object, or be
        ///    auto-supplied for ISupportColor objects.
        ///
        ///    If an IFormatInfo cannot be found, returns null.
        /// </summary>
        /// <param name="formatInfoType">
        ///    Optional: if null, will return the first view definition it finds,
        ///    excluding AltSingleLineViewDefinitions.
        /// </param>
        public static IFormatInfo ChooseFormatInfoForPSObject(PSObject outputObject,
                                                              Type formatInfoType)
        {
            if (null == outputObject)
            {
                throw new ArgumentNullException("outputObject");
            }

            IFormatInfo fi = null;

            //
            // First check the hidden property for an object-specific view.
            //

            PSPropertyInfo pspi = outputObject.Properties[DefaultViewDefPropName];

            if ((null != pspi) && (null != pspi.Value))
            {
                if (!(pspi.Value is IFormatInfo))
                {
                    Util.Fail("Expected hidden property to be an IFormatInfo.");
                }
                else
                {
                    fi = (IFormatInfo)pspi.Value;
                }
            }

            //
            // But wait... does it match the requested view type?
            //
            // If there is a requested type (formatInfoType is not null), but it matches
            // the object-specific default view... should we use the object specific
            // default view?
            //
            // I think it could go either way, but I'm going to choose to go with the
            // object-specific default view, because say somebody does:
            //
            //    $stuff = Invoke-SomeCommand
            //    $stuff
            //    $stuff | ft
            //
            // Where "$stuff" has object-specific default views attached, and they are
            // table views. The user would probably expect to get the same output for
            // second and third commands.
            //

            if ((null != fi) &&
                ((null == formatInfoType) ||
                 (formatInfoType.IsAssignableFrom(fi.GetType()))))
            {
                return(fi);
            }

            //
            // Okay, now check for views registered for all instances of the object's
            // type.
            //

            var viewInfo = ChooseFormatInfoForPSObjectFromRegistered(outputObject,
                                                                     formatInfoType);

            if (null != viewInfo)
            {
                return(viewInfo.ViewDefinition);
            }
            else if (((null == formatInfoType) || (typeof(AltCustomViewDefinition) == formatInfoType)) &&
                     (outputObject.BaseObject is ISupportColor))
            {
                // For objects that implement ISupportColor but don't have a registered
                // view, we'll supply a view for them.
                return(sm_customIdentityView);
            }

            return(null);
        }