本文整理汇总了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);
}
}
}