IsFormatApplicable() private method

Gets value indicating whether this formatter can format string which represents instance of given data type.
private IsFormatApplicable ( Type dataType ) : bool
dataType System.Type Data type for which this format provider is tested.
return bool
Beispiel #1
0
        /// <summary>
        ///     Gets value indicating whether this format provider can be applied to specified data type or not.
        ///     This format provider is applicable to matrices (arrays of rank 2) and to jagged arrays, but only
        ///     if element type is supported by ScalarFormatInfo format provider.
        /// </summary>
        /// <param name="dataType">Data type agianst which this format provider is tested.</param>
        /// <returns>true if this format provider is applicable; otherwise false.</returns>
        internal override bool IsFormatApplicable(Type dataType)
        {
            var elementType = GetElementType(dataType);

            var applicable = false;

            if (elementType != null)
            {
                // Existence of elementType proves that dataType is matrix or jagged array
                var sfi = new ScalarFormatInfo(this);
                applicable = sfi.IsFormatApplicable(elementType);
            }

            return(applicable);
        }
        /// <summary>
        ///     Gets value indicating whether current format provider is applicable to format strings
        ///     that represent instances of given data type. This format provider is applicable if
        ///     data type is an array of values to which ScalarFormatInfo format provider is applicable.
        ///     This format provider can also be applied to last dimension of a multi-dimensional array
        ///     if specified so by the internal properties.
        /// </summary>
        /// <param name="dataType">Data type for which current format provider is tested.</param>
        /// <returns>
        ///     true if current format provider is capable to format string representing instance
        ///     of given data type; otherwise false.
        /// </returns>
        internal override bool IsFormatApplicable(Type dataType)
        {
            var applicable = false;

            if (dataType != null && dataType.IsArray)
            {
                if (_showLastDimension || dataType.GetArrayRank() == 1)
                {
                    var elementType = dataType.GetElementType();
                    var sfi         = new ScalarFormatInfo(this);

                    if (sfi.IsFormatApplicable(elementType))
                    {
                        applicable = true;
                    }
                }
            }

            return(applicable);
        }
Beispiel #3
0
        /// <summary>
        ///     Converts the value of a specified object to an equivalent string representation using
        ///     specified format and culture-specific formatting information.
        /// </summary>
        /// <param name="sb">String builder to which formatted string should be appended.</param>
        /// <param name="format">A format string containing formatting specifications.</param>
        /// <param name="arg">An object to format.</param>
        /// <param name="formatProvider">An object that supplies format information about the current instance.</param>
        /// <param name="maxLength">
        ///     Maximum number of characters allowed to the formatter. Formatting should fail (and return false)
        ///     if this number of characters is breached. Multi-lined formatters should ignore this parameter.
        ///     Negative value indicates that formatter has unlimited space available. On output contains remaining number of characters available.
        /// </param>
        /// <returns>
        ///     true if representation of <paramref name="arg" /> has been successfully appended to <paramref name="sb" />
        ///     within given number of allowed characters; otherwise false.
        /// </returns>
        internal override bool Format(StringBuilder sb, string format, object arg, IFormatProvider formatProvider, ref int maxLength)
        {
            var success        = true;
            var originalLength = sb.Length;

            var formatters        = new List <VerboseFormatInfoBase>();
            var singleLinedFormat = SingleLinedFormat;

            if (arg != null || InstanceDataType != null)
            {
                var type = GetInstanceType(arg);

                var sfi = new ScalarFormatInfo(this);
                sfi.MaximumFormattedLength = maxLength;

                if (sfi.IsFormatApplicable(type))
                {
                    if (IsMultiLinedFormat)
                    {
                        var singleLinedSfi = new ScalarFormatInfo(this);
                        FormatInfoUtils.CopyFormatting(singleLinedFormat, singleLinedSfi);
                        singleLinedSfi.CombineMaximumFormattedLength(FormatInfoUtils.DefaultMaxFormattedLength);
                        formatters.Add(singleLinedSfi);
                    }

                    formatters.Add(sfi);
                }

                if (formatters.Count == 0)
                {
                    var dfi = new DictionaryFormatInfo(this);
                    dfi.MaximumFormattedLength = maxLength;

                    if (dfi.IsFormatApplicable(type))
                    {
                        if (IsMultiLinedFormat)
                        {
                            var singleLinedDfi = new DictionaryFormatInfo(this);
                            FormatInfoUtils.CopyFormatting(singleLinedFormat, singleLinedDfi);
                            singleLinedDfi.CombineMaximumFormattedLength(FormatInfoUtils.DefaultMaxFormattedLength);
                            formatters.Add(singleLinedDfi);
                        }
                        formatters.Add(dfi);
                    }
                }

                if (formatters.Count == 0)
                {
                    var cmfi = new CompactMatrixFormatInfo(this);
                    cmfi.MaximumFormattedLength = maxLength;

                    if (cmfi.IsFormatApplicable(type))
                    {
                        //if (IsMultiLinedFormat)   // Uncomment these lines to enable inlining compactly presented matrices; that operation is not suggested
                        //{
                        //    CompactMatrixFormatInfo singleLinedCmfi = new CompactMatrixFormatInfo(this);
                        //    FormatInfoUtils.CopyFormatting(singleLinedFormat, singleLinedCmfi);
                        //    singleLinedCmfi.CombineMaximumFormattedLength(FormatInfoUtils.DefaultMaxFormattedLength);
                        //    formatters.Add(singleLinedCmfi);
                        //}
                        formatters.Add(cmfi);
                    }
                }

                if (formatters.Count == 0)
                {
                    var cafi = new CompactArrayFormatInfo(this);
                    cafi.MaximumFormattedLength = maxLength;

                    if (cafi.IsFormatApplicable(type))
                    {
                        if (IsMultiLinedFormat)
                        {
                            var singleLinedCafi = new CompactArrayFormatInfo(this);
                            FormatInfoUtils.CopyFormatting(singleLinedFormat, singleLinedCafi);
                            singleLinedCafi.CombineMaximumFormattedLength(FormatInfoUtils.DefaultMaxFormattedLength);
                            formatters.Add(singleLinedCafi);
                        }
                        formatters.Add(cafi);
                    }
                }

                if (formatters.Count == 0)
                {
                    var afi = new ArrayFormatInfo(this);
                    afi.MaximumFormattedLength = maxLength;

                    if (afi.IsFormatApplicable(type))
                    {
                        if (IsMultiLinedFormat)
                        {
                            var singleLinedAfi = new ArrayFormatInfo(this);
                            FormatInfoUtils.CopyFormatting(singleLinedFormat, singleLinedAfi);
                            singleLinedAfi.CombineMaximumFormattedLength(FormatInfoUtils.DefaultMaxFormattedLength);
                            formatters.Add(singleLinedAfi);
                        }
                        formatters.Add(afi);
                    }
                }

                if (formatters.Count == 0)
                {
                    var efi = new EnumerableFormatInfo(this);
                    efi.MaximumFormattedLength = maxLength;

                    if (efi.IsFormatApplicable(type))
                    {
                        if (IsMultiLinedFormat)
                        {
                            var singleLinedEfi = new EnumerableFormatInfo(this);
                            FormatInfoUtils.CopyFormatting(singleLinedFormat, singleLinedEfi);
                            singleLinedEfi.CombineMaximumFormattedLength(FormatInfoUtils.DefaultMaxFormattedLength);
                            formatters.Add(singleLinedEfi);
                        }
                        formatters.Add(efi);
                    }
                }

                if (formatters.Count == 0)
                {
                    if (IsMultiLinedFormat)
                    {
                        var singleLinedGfi = new GeneralFormatInfo(this);
                        FormatInfoUtils.CopyFormatting(singleLinedFormat, singleLinedGfi);
                        singleLinedGfi.CombineMaximumFormattedLength(FormatInfoUtils.DefaultMaxFormattedLength);
                        formatters.Add(singleLinedGfi);
                    }

                    var gfi = new GeneralFormatInfo(this);
                    gfi.MaximumFormattedLength = maxLength;

                    formatters.Add(gfi);
                }
            }

            if (formatters.Count > 0)
            {
                success = success && FormatInfoUtils.BestTryFormat(sb, format, arg, formatters, ref maxLength);
            }

            if (!success)
            {
                sb.Length = originalLength;
            }

            return(success);
        }
		/// <summary>
		///     Converts the value of a specified object to an equivalent string representation using
		///     specified format and culture-specific formatting information.
		/// </summary>
		/// <param name="sb">String builder to which formatted string should be appended.</param>
		/// <param name="format">A format string containing formatting specifications.</param>
		/// <param name="arg">An object to format.</param>
		/// <param name="formatProvider">An object that supplies format information about the current instance.</param>
		/// <param name="maxLength">
		///     Maximum number of characters allowed to the formatter. Formatting should fail (and return false)
		///     if this number of characters is breached. Multi-lined formatters should ignore this parameter.
		///     Negative value indicates that formatter has unlimited space available. On output contains remaining number of characters available.
		/// </param>
		/// <returns>
		///     true if representation of <paramref name="arg" /> has been successfully appended to <paramref name="sb" />
		///     within given number of allowed characters; otherwise false.
		/// </returns>
		internal override bool Format(StringBuilder sb, string format, object arg, IFormatProvider formatProvider, ref int maxLength)
		{
			var success = true;
			var originalLength = sb.Length;

			var formatters = new List<VerboseFormatInfoBase>();
			var singleLinedFormat = SingleLinedFormat;

			if (arg != null || InstanceDataType != null)
			{
				var type = GetInstanceType(arg);

				var sfi = new ScalarFormatInfo(this);
				sfi.MaximumFormattedLength = maxLength;

				if (sfi.IsFormatApplicable(type))
				{
					if (IsMultiLinedFormat)
					{
						var singleLinedSfi = new ScalarFormatInfo(this);
						FormatInfoUtils.CopyFormatting(singleLinedFormat, singleLinedSfi);
						singleLinedSfi.CombineMaximumFormattedLength(FormatInfoUtils.DefaultMaxFormattedLength);
						formatters.Add(singleLinedSfi);
					}

					formatters.Add(sfi);
				}

				if (formatters.Count == 0)
				{
					var dfi = new DictionaryFormatInfo(this);
					dfi.MaximumFormattedLength = maxLength;

					if (dfi.IsFormatApplicable(type))
					{
						if (IsMultiLinedFormat)
						{
							var singleLinedDfi = new DictionaryFormatInfo(this);
							FormatInfoUtils.CopyFormatting(singleLinedFormat, singleLinedDfi);
							singleLinedDfi.CombineMaximumFormattedLength(FormatInfoUtils.DefaultMaxFormattedLength);
							formatters.Add(singleLinedDfi);
						}
						formatters.Add(dfi);
					}
				}

				if (formatters.Count == 0)
				{
					var cmfi = new CompactMatrixFormatInfo(this);
					cmfi.MaximumFormattedLength = maxLength;

					if (cmfi.IsFormatApplicable(type))
					{
						//if (IsMultiLinedFormat)   // Uncomment these lines to enable inlining compactly presented matrices; that operation is not suggested
						//{
						//    CompactMatrixFormatInfo singleLinedCmfi = new CompactMatrixFormatInfo(this);
						//    FormatInfoUtils.CopyFormatting(singleLinedFormat, singleLinedCmfi);
						//    singleLinedCmfi.CombineMaximumFormattedLength(FormatInfoUtils.DefaultMaxFormattedLength);
						//    formatters.Add(singleLinedCmfi);
						//}
						formatters.Add(cmfi);
					}
				}

				if (formatters.Count == 0)
				{
					var cafi = new CompactArrayFormatInfo(this);
					cafi.MaximumFormattedLength = maxLength;

					if (cafi.IsFormatApplicable(type))
					{
						if (IsMultiLinedFormat)
						{
							var singleLinedCafi = new CompactArrayFormatInfo(this);
							FormatInfoUtils.CopyFormatting(singleLinedFormat, singleLinedCafi);
							singleLinedCafi.CombineMaximumFormattedLength(FormatInfoUtils.DefaultMaxFormattedLength);
							formatters.Add(singleLinedCafi);
						}
						formatters.Add(cafi);
					}
				}

				if (formatters.Count == 0)
				{
					var afi = new ArrayFormatInfo(this);
					afi.MaximumFormattedLength = maxLength;

					if (afi.IsFormatApplicable(type))
					{
						if (IsMultiLinedFormat)
						{
							var singleLinedAfi = new ArrayFormatInfo(this);
							FormatInfoUtils.CopyFormatting(singleLinedFormat, singleLinedAfi);
							singleLinedAfi.CombineMaximumFormattedLength(FormatInfoUtils.DefaultMaxFormattedLength);
							formatters.Add(singleLinedAfi);
						}
						formatters.Add(afi);
					}
				}

				if (formatters.Count == 0)
				{
					var efi = new EnumerableFormatInfo(this);
					efi.MaximumFormattedLength = maxLength;

					if (efi.IsFormatApplicable(type))
					{
						if (IsMultiLinedFormat)
						{
							var singleLinedEfi = new EnumerableFormatInfo(this);
							FormatInfoUtils.CopyFormatting(singleLinedFormat, singleLinedEfi);
							singleLinedEfi.CombineMaximumFormattedLength(FormatInfoUtils.DefaultMaxFormattedLength);
							formatters.Add(singleLinedEfi);
						}
						formatters.Add(efi);
					}
				}

				if (formatters.Count == 0)
				{
					if (IsMultiLinedFormat)
					{
						var singleLinedGfi = new GeneralFormatInfo(this);
						FormatInfoUtils.CopyFormatting(singleLinedFormat, singleLinedGfi);
						singleLinedGfi.CombineMaximumFormattedLength(FormatInfoUtils.DefaultMaxFormattedLength);
						formatters.Add(singleLinedGfi);
					}

					var gfi = new GeneralFormatInfo(this);
					gfi.MaximumFormattedLength = maxLength;

					formatters.Add(gfi);
				}
			}

			if (formatters.Count > 0)
			{
				success = success && FormatInfoUtils.BestTryFormat(sb, format, arg, formatters, ref maxLength);
			}

			if (!success)
			{
				sb.Length = originalLength;
			}

			return success;
		}
		/// <summary>
		///     Gets value indicating whether this format provider can be applied to specified data type or not.
		///     This format provider is applicable to matrices (arrays of rank 2) and to jagged arrays, but only
		///     if element type is supported by ScalarFormatInfo format provider.
		/// </summary>
		/// <param name="dataType">Data type agianst which this format provider is tested.</param>
		/// <returns>true if this format provider is applicable; otherwise false.</returns>
		internal override bool IsFormatApplicable(Type dataType)
		{
			var elementType = GetElementType(dataType);

			var applicable = false;

			if (elementType != null)
			{
				// Existence of elementType proves that dataType is matrix or jagged array
				var sfi = new ScalarFormatInfo(this);
				applicable = sfi.IsFormatApplicable(elementType);
			}

			return applicable;
		}