AppendFriendlyTypeName() private method

Appends user friendly name for the given data type and appends it to string builder.
private AppendFriendlyTypeName ( Type type, object instance, StringBuilder sb, int &maxLength ) : bool
type System.Type Data type for which user friendly name is requested.
instance object /// Instance for which friendly type name is appended. /// Use this argument to gather additional information which might not be available from the type information. /// This argument may be null if instance is not available. ///
sb StringBuilder String builder to which data type name should be appended.
maxLength int /// Maximum number of characters allowed to the formatter. Formatting will fail (and return false) /// if this number of characters is breached. Multi-lined formatters will ignore this parameter. /// Negative value indicates that formatter has unlimited space available. /// On output contains remaining number of characters available. ///
return bool
        /// <summary>
        ///     Formats user friendly representation of the name of the given type.
        /// </summary>
        /// <param name="type">Type for which user friendly representation of the name is required.</param>
        /// <param name="instance">
        ///     Instance for which friendly type name is appended.
        ///     This argument is used to gather additional information which might not be available from the type information.
        ///     This argument may be null if instance is not available.
        /// </param>
        /// <param name="sb">
        ///     String builder to which user friendly representation of the name of <paramref name="type" /> is appended.
        ///     If <paramref name="type" /> is null then nothing is appended to this string builder.
        /// </param>
        /// <param name="maxLength">
        ///     Maximum number of characters allowed to this method to append to string builder. Negative value indicates
        ///     unlimited number of characters allowed. Method fails and returns false if it could not perform the task within given number of
        ///     characters.
        ///     On output contains remaining number of characters allowed.
        /// </param>
        /// <returns>
        ///     true if method has successfully appended friendly name of the data type within given number of characters allowed; otherwise
        ///     false.
        /// </returns>
        internal override bool AppendFriendlyTypeName(Type type, object instance, StringBuilder sb, ref int maxLength)
        {
            var success        = true;
            var originalLength = sb.Length;

            if (type.IsArray && type.GetArrayRank() == 1)
            {
                // This

                var elementType = type.GetElementType();

                var sfi = new ScalarFormatInfo(this);
                success = success && sfi.AppendFriendlyTypeName(elementType, null, sb, ref maxLength);                 // This will append compact name for scalar types

                var ar        = (Array)instance;
                var dimension = string.Format("[{0}]", ar.GetLength(0));
                success = FormatInfoUtils.TryAppendString(this, sb, dimension, success, ref maxLength);
            }

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

            return(success);
        }
Beispiel #2
0
        /// <summary>
        ///     Formats user friendly representation of the name of the given matrix or jagged array type.
        /// </summary>
        /// <param name="type">Type for which user friendly representation of the name is required.</param>
        /// <param name="instance">
        ///     Instance for which friendly type name is appended.
        ///     This argument is used to gather additional information which might not be available from the type information.
        ///     This argument may be null if instance is not available.
        /// </param>
        /// <param name="sb">
        ///     String builder to which user friendly representation of the name of <paramref name="type" /> is appended.
        ///     If <paramref name="type" /> is null then nothing is appended to this string builder.
        /// </param>
        /// <param name="maxLength">
        ///     Maximum number of characters allowed to this method to append to string builder. Negative value indicates
        ///     unlimited number of characters allowed. Method fails and returns false if it could not perform the task within given number of
        ///     characters.
        ///     On output contains remaining number of characters allowed.
        /// </param>
        /// <returns>
        ///     true if method has successfully appended friendly name of the data type within given number of characters allowed; otherwise
        ///     false.
        /// </returns>
        internal override bool AppendFriendlyTypeName(Type type, object instance, StringBuilder sb, ref int maxLength)
        {
            var success        = true;
            var originalLength = sb.Length;

            if (type != null)
            {
                var elementType = GetElementType(type);
                if (elementType != null)
                {
                    var sfi = new ScalarFormatInfo();
                    success = success && sfi.AppendFriendlyTypeName(elementType, null, sb, ref maxLength);
                }
            }

            var rows     = 0;
            var lowCols  = 0;
            var highCols = 0;

            if (instance != null)
            {
                GetDimensions((Array)instance, out rows, out lowCols, out highCols);
            }

            success = FormatInfoUtils.TryAppendChar(this, sb, '[', success, ref maxLength);
            if (instance != null)
            {
                success = FormatInfoUtils.TryAppendString(this, sb, rows.ToString("0"), success, ref maxLength);
            }
            success = FormatInfoUtils.TryAppendChar(this, sb, ']', success, ref maxLength);

            success = FormatInfoUtils.TryAppendChar(this, sb, '[', success, ref maxLength);
            if (instance != null)
            {
                success = FormatInfoUtils.TryAppendString(this, sb, lowCols.ToString("0"), success, ref maxLength);
                if (highCols != lowCols)
                {
                    success = FormatInfoUtils.TryAppendChar(this, sb, '-', success, ref maxLength);
                    success = FormatInfoUtils.TryAppendString(this, sb, highCols.ToString("0"), success, ref maxLength);
                }
            }
            success = FormatInfoUtils.TryAppendChar(this, sb, ']', success, ref maxLength);

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

            return(success);
        }
        /// <summary>
        ///     Formats friendly representation of the name of the given type.
        /// </summary>
        /// <param name="type">Type for which friendly representation of the name is required.</param>
        /// <param name="instance">
        ///     Instance for which friendly type name is appended.
        ///     This argument is used to gather additional information which might not be available
        ///     from the type information. This argument may be null if instance is not available.
        /// </param>
        /// <param name="sb">
        ///     String builder to which friendly name of <paramref name="type" /> is appended.
        ///     If <paramref name="type" /> is null then nothing is appended to this string builder.
        /// </param>
        /// <param name="maxLength">
        ///     Maximum number of characters allowed to this method
        ///     to append to string builder. Negative value indicates unlimited number of characters allowed.
        ///     Method fails and returns false if it could not perform the task within given number
        ///     of characters. On output contains remaining number of characters allowed.
        /// </param>
        /// <returns>
        ///     true if method has successfully appended friendly name of the data type
        ///     within given number of characters allowed; otherwise false.
        /// </returns>
        internal override bool AppendFriendlyTypeName(Type type, object instance, StringBuilder sb, ref int maxLength)
        {
            var success        = true;
            var originalLength = sb.Length;

            if (type != null)
            {
                type = type.GetElementType();                 // For arrays print element type rather than array type
            }

            if (success)
            {
                var sfi = new ScalarFormatInfo(this);
                success = sfi.AppendFriendlyTypeName(type, null, sb, ref maxLength);
                // Use scalar format info because it can write simple data types in a more compact way
            }

            // Now append dimensions of the array
            // If instance is null then dimensions cannot be determined and braces will remain empty, like int[].
            // Otherwise, if instance is present, dimensions will be fully shown, like int[3, 4, 2] in case of three-dimensional array.

            success = FormatInfoUtils.TryAppendChar(this, sb, '[', success, ref maxLength);

            if (instance != null)
            {
                var dimensions = GetDimensions(instance);
                for (var i = 0; success && i < dimensions.Length; i++)
                {
                    if (i > 0)
                    {
                        success = FormatInfoUtils.TryAppendString(this, sb, ", ", success, ref maxLength);
                    }
                    success = FormatInfoUtils.TryAppendString(this, sb, dimensions[i].ToString("0"), success, ref maxLength);
                }
            }

            success = FormatInfoUtils.TryAppendChar(this, sb, ']', success, ref maxLength);

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

            return(success);
        }
		/// <summary>
		///     Formats user friendly representation of the name of the given matrix or jagged array type.
		/// </summary>
		/// <param name="type">Type for which user friendly representation of the name is required.</param>
		/// <param name="instance">
		///     Instance for which friendly type name is appended.
		///     This argument is used to gather additional information which might not be available from the type information.
		///     This argument may be null if instance is not available.
		/// </param>
		/// <param name="sb">
		///     String builder to which user friendly representation of the name of <paramref name="type" /> is appended.
		///     If <paramref name="type" /> is null then nothing is appended to this string builder.
		/// </param>
		/// <param name="maxLength">
		///     Maximum number of characters allowed to this method to append to string builder. Negative value indicates
		///     unlimited number of characters allowed. Method fails and returns false if it could not perform the task within given number of
		///     characters.
		///     On output contains remaining number of characters allowed.
		/// </param>
		/// <returns>
		///     true if method has successfully appended friendly name of the data type within given number of characters allowed; otherwise
		///     false.
		/// </returns>
		internal override bool AppendFriendlyTypeName(Type type, object instance, StringBuilder sb, ref int maxLength)
		{
			var success = true;
			var originalLength = sb.Length;

			if (type != null)
			{
				var elementType = GetElementType(type);
				if (elementType != null)
				{
					var sfi = new ScalarFormatInfo();
					success = success && sfi.AppendFriendlyTypeName(elementType, null, sb, ref maxLength);
				}
			}

			var rows = 0;
			var lowCols = 0;
			var highCols = 0;

			if (instance != null)
			{
				GetDimensions((Array) instance, out rows, out lowCols, out highCols);
			}

			success = FormatInfoUtils.TryAppendChar(this, sb, '[', success, ref maxLength);
			if (instance != null)
			{
				success = FormatInfoUtils.TryAppendString(this, sb, rows.ToString("0"), success, ref maxLength);
			}
			success = FormatInfoUtils.TryAppendChar(this, sb, ']', success, ref maxLength);

			success = FormatInfoUtils.TryAppendChar(this, sb, '[', success, ref maxLength);
			if (instance != null)
			{
				success = FormatInfoUtils.TryAppendString(this, sb, lowCols.ToString("0"), success, ref maxLength);
				if (highCols != lowCols)
				{
					success = FormatInfoUtils.TryAppendChar(this, sb, '-', success, ref maxLength);
					success = FormatInfoUtils.TryAppendString(this, sb, highCols.ToString("0"), success, ref maxLength);
				}
			}
			success = FormatInfoUtils.TryAppendChar(this, sb, ']', success, ref maxLength);

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

			return success;
		}
		/// <summary>
		///     Formats friendly representation of the name of the given type.
		/// </summary>
		/// <param name="type">Type for which friendly representation of the name is required.</param>
		/// <param name="instance">
		///     Instance for which friendly type name is appended.
		///     This argument is used to gather additional information which might not be available
		///     from the type information. This argument may be null if instance is not available.
		/// </param>
		/// <param name="sb">
		///     String builder to which friendly name of <paramref name="type" /> is appended.
		///     If <paramref name="type" /> is null then nothing is appended to this string builder.
		/// </param>
		/// <param name="maxLength">
		///     Maximum number of characters allowed to this method
		///     to append to string builder. Negative value indicates unlimited number of characters allowed.
		///     Method fails and returns false if it could not perform the task within given number
		///     of characters. On output contains remaining number of characters allowed.
		/// </param>
		/// <returns>
		///     true if method has successfully appended friendly name of the data type
		///     within given number of characters allowed; otherwise false.
		/// </returns>
		internal override bool AppendFriendlyTypeName(Type type, object instance, StringBuilder sb, ref int maxLength)
		{
			var success = true;
			var originalLength = sb.Length;

			if (type != null)
			{
				type = type.GetElementType(); // For arrays print element type rather than array type
			}

			if (success)
			{
				var sfi = new ScalarFormatInfo(this);
				success = sfi.AppendFriendlyTypeName(type, null, sb, ref maxLength);
				// Use scalar format info because it can write simple data types in a more compact way
			}

			// Now append dimensions of the array
			// If instance is null then dimensions cannot be determined and braces will remain empty, like int[].
			// Otherwise, if instance is present, dimensions will be fully shown, like int[3, 4, 2] in case of three-dimensional array.

			success = FormatInfoUtils.TryAppendChar(this, sb, '[', success, ref maxLength);

			if (instance != null)
			{
				var dimensions = GetDimensions(instance);
				for (var i = 0; success && i < dimensions.Length; i++)
				{
					if (i > 0)
					{
						success = FormatInfoUtils.TryAppendString(this, sb, ", ", success, ref maxLength);
					}
					success = FormatInfoUtils.TryAppendString(this, sb, dimensions[i].ToString("0"), success, ref maxLength);
				}
			}

			success = FormatInfoUtils.TryAppendChar(this, sb, ']', success, ref maxLength);

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

			return success;
		}