当前位置: 首页>>代码示例>>C#>>正文


C# Format.GetText方法代码示例

本文整理汇总了C#中Format.GetText方法的典型用法代码示例。如果您正苦于以下问题:C# Format.GetText方法的具体用法?C# Format.GetText怎么用?C# Format.GetText使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在Format的用法示例。


在下文中一共展示了Format.GetText方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: EvaluateFormat

        /// <summary>
        /// Do the default formatting, same logic as "String.Format".
        /// </summary>
        public void EvaluateFormat(object current, Format format, ref bool handled, IOutput output, FormatDetails formatDetails)
        {
            // This function always handles the method:
            handled = true;

            // If the format has nested placeholders, we process those first 
            // instead of formatting the item:
            if (format != null && format.HasNested)
            {
                formatDetails.Formatter.Format(output, format, current, formatDetails);
                return;
            }

            // If the object is null, we shouldn't write anything
            if (current == null)
            {
                return;
            }


            //  (The following code was adapted from the built-in String.Format code)

            //  We will try using IFormatProvider, IFormattable, and if all else fails, ToString.
            var formatter = formatDetails.Formatter;
			if (formatter == null) {} // Force the compiler not to complain about unused variable
            string result = null;
            ICustomFormatter cFormatter;
            IFormattable formattable;
            // Use the provider to see if a CustomFormatter is available:
            if (formatDetails.Provider != null && (cFormatter = formatDetails.Provider.GetFormat(typeof(ICustomFormatter)) as ICustomFormatter) != null)
            {
                var formatText = format == null ? null : format.GetText();
                result = cFormatter.Format(formatText, current, formatDetails.Provider);
            }
            // IFormattable:
            else if ((formattable = current as IFormattable) != null)
            {
                var formatText = format == null ? null : format.ToString();
                result = formattable.ToString(formatText, formatDetails.Provider);
            }
            // ToString:
            else
            {
                result = current.ToString();
            }


            // Now that we have the result, let's output it (and consider alignment):
            
            
            // See if there's a pre-alignment to consider:
            if (formatDetails.Placeholder.Alignment > 0)
            {
                var spaces = formatDetails.Placeholder.Alignment - result.Length;
                if (spaces > 0)
                {
                    output.Write(new String(' ', spaces), formatDetails);
                }
            }

            // Output the result:
            output.Write(result, formatDetails);


            // See if there's a post-alignment to consider:
            if (formatDetails.Placeholder.Alignment < 0)
            {
                var spaces = -formatDetails.Placeholder.Alignment - result.Length;
                if (spaces > 0)
                {
                    output.Write(new String(' ', spaces), formatDetails);
                }
            }
        }
开发者ID:Avatarchik,项目名称:AnimatorAccess,代码行数:77,代码来源:DefaultFormatter.cs


注:本文中的Format.GetText方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。