本文整理汇总了C#中IFormatProvider.GetFormat方法的典型用法代码示例。如果您正苦于以下问题:C# IFormatProvider.GetFormat方法的具体用法?C# IFormatProvider.GetFormat怎么用?C# IFormatProvider.GetFormat使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类IFormatProvider
的用法示例。
在下文中一共展示了IFormatProvider.GetFormat方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: ToString
public string ToString(string format, IFormatProvider formatProvider)
{
if (formatProvider == null)
{
return this.ToString();
}
ICustomFormatter fmt = formatProvider.GetFormat(this.GetType()) as ICustomFormatter;
if (fmt != null)
{
return fmt.Format(format, this, formatProvider);
}
switch (format)
{
case "V":
return this.Value;
case "O":
return this.Operator.ToString();
case "G":
default:
return this.ToString();
}
}
示例2: GetTimeTextInfo
private TimeTextInfo GetTimeTextInfo(IFormatProvider provider)
{
if (provider != null)
{
// See if the provider can give us what we want:
var timeTextInfo = (TimeTextInfo)provider.GetFormat(typeof (TimeTextInfo));
if (timeTextInfo != null)
{
return timeTextInfo;
}
// See if there is a rule for this culture:
var cultureInfo = provider as CultureInfo;
if (cultureInfo != null)
{
timeTextInfo = CommonLanguagesTimeTextInfo.GetTimeTextInfo(cultureInfo.TwoLetterISOLanguageName);
// If cultureInfo was supplied,
// we will always return, even if null:
return timeTextInfo;
}
}
// Return the default if the provider couldn't provide:
return CommonLanguagesTimeTextInfo.GetTimeTextInfo(defaultTwoLetterISOLanguageName);
}
示例3: ToString
public string ToString(string format, IFormatProvider formatProvider) {
if (formatProvider != null) {
ICustomFormatter fmt = formatProvider.GetFormat(this.GetType()) as ICustomFormatter;
if (fmt != null)
return fmt.Format(format, this, formatProvider);
}
switch (format) {
case "g": {
string s = Group.ToString("x4");
string x = String.Empty;
x += ((Mask & 0xf0000000) != 0) ? s[0] : 'x';
x += ((Mask & 0x0f000000) != 0) ? s[1] : 'x';
x += ((Mask & 0x00f00000) != 0) ? s[2] : 'x';
x += ((Mask & 0x000f0000) != 0) ? s[3] : 'x';
return x;
}
case "e": {
string s = Element.ToString("x4");
string x = String.Empty;
x += ((Mask & 0x0000f000) != 0) ? s[0] : 'x';
x += ((Mask & 0x00000f00) != 0) ? s[1] : 'x';
x += ((Mask & 0x000000f0) != 0) ? s[2] : 'x';
x += ((Mask & 0x0000000f) != 0) ? s[3] : 'x';
return x;
}
case "G":
default: {
return String.Format("({0},{1})", this.ToString("g", null), this.ToString("e", null));
}
}
}
示例4: ToStringExtended
/// <summary>
/// An alternative for the ToString() method of DateTime to work around a problem with implementing custom formatters for DateTime objects
/// The default implementation, even if given a good IFormatProvider implementation, would never call a custom formatter. It would
/// still use the default formatter.
/// </summary>
/// <param name="dateTime">The date time object.</param>
/// <param name="provider">The provider.</param>
/// <param name="format">The format.</param>
/// <returns></returns>
public static string ToStringExtended(this DateTime dateTime, string format, IFormatProvider provider)
{
var formatter = provider.GetFormat(typeof(DateTimeFormatInfo)) as ICustomFormatter;
if (formatter != null)
{
return formatter.Format(format, dateTime, provider);
}
throw new ArgumentException("The format provider argument did not return an instance that implements ICustomFormatter", "provider");
}
示例5: GetInstance
public static DateTimeFormatInfo GetInstance(IFormatProvider fp) {
if (fp != null) {
DateTimeFormatInfo dtf = (DateTimeFormatInfo)fp.GetFormat(typeof(DateTimeFormatInfo));
if (dtf != null) {
return dtf;
}
}
return CurrentInfo;
}
示例6: Parse
public decimal Parse(IFormatProvider provider, decimal defaultValue = default(decimal), bool round = false)
{
var result = _converter.Parse(provider, defaultValue);
if (round)
{
provider = provider ?? CultureInfo.CurrentCulture;
var numberFormat = (NumberFormatInfo)provider.GetFormat(typeof(NumberFormatInfo)) ?? CultureInfo.CurrentCulture.NumberFormat;
return Math.Round(result, numberFormat.CurrencyDecimalDigits);
}
return result;
}
示例7: Parse
public decimal? Parse(IFormatProvider provider, bool round = false)
{
var result = _converter.Parse(provider);
if (!result.HasValue)
return null;
if (round)
{
provider = provider ?? CultureInfo.CurrentCulture;
var numberFormat = (NumberFormatInfo)provider.GetFormat(typeof(NumberFormatInfo)) ?? CultureInfo.CurrentCulture.NumberFormat;
return Math.Round(result.Value, numberFormat.CurrencyDecimalDigits);
}
return result;
}
示例8: SetDateTimeFormat
/// <summary>
/// Sets a DateTime formatter to be used for outputting the DateTime values
/// </summary>
/// <param name="formatProvider">Format provider</param>
/// <returns>A Collapse instance, used for chaining</returns>
public Collapse SetDateTimeFormat(IFormatProvider formatProvider)
{
if(formatProvider == null)
throw new ArgumentNullException("formatProvider", "Format provider was not supplied.");
DateTimeFormatInfo formatInfo =
formatProvider.GetFormat(typeof(DateTimeFormatInfo)) as DateTimeFormatInfo;
if (formatInfo == null)
throw new DateTimeFormatInvalidException();
SetDateTimeFormat(
String.Format("{0} {1}", formatInfo.ShortDatePattern, formatInfo.LongTimePattern));
return this;
}
示例9: NumberFormatInfo
NumberFormatInfo(IFormatProvider provider)
{
if(provider == null)
{
return System.Globalization.NumberFormatInfo.CurrentInfo;
}
else
{
NumberFormatInfo nfi =
(NumberFormatInfo) provider.GetFormat(
typeof(System.Globalization.NumberFormatInfo));
if(nfi != null)
{
return nfi;
}
else
{
return System.Globalization.NumberFormatInfo.CurrentInfo;
}
}
}
示例10: ToInt32
public int ToInt32(IFormatProvider provider)
{
bool toMinValue = true;
if (provider != null)
{
TestFormatProvider format = provider.GetFormat(typeof(TestFormatProvider)) as TestFormatProvider;
if ((format != null) && format.ToInt16MaxValue)
{
toMinValue = false;
}
}
if (toMinValue)
{
return Int32.MinValue;
}
else
{
return Int32.MaxValue;
}
}
示例11: ToString
public string ToString(string format, IFormatProvider formatProvider)
{
if (formatProvider != null)
{
ICustomFormatter fmt = formatProvider.GetFormat(this.GetType()) as ICustomFormatter;
if (fmt != null)
{
return fmt.Format(format, this, formatProvider);
}
}
switch (format)
{
case "n":
return Name;
case "p":
return PhoneNumber;
case "a":
return Name + PhoneNumber;
default:
return Name;
}
}
示例12: ToJavaFormatString
public static string ToJavaFormatString(string format, IFormatProvider provider, DateTimeKind kind, bool dateTimeOffset, out bool useInvariant, out bool foundDateTimeKind, out bool useUtc)
{
if (string.IsNullOrEmpty(format))
format = "G";
DateTimeFormatInfo dtfi = null;
if (provider != null)
dtfi = (DateTimeFormatInfo)provider.GetFormat(typeof(DateTimeFormatInfo));
if (dtfi == null)
dtfi = DateTimeFormatInfo.CurrentInfo;
bool needsTranslation = true;
if (format.Length == 1 && dtfi != null)
{
format = GetStandardPattern(format[0], dtfi, dateTimeOffset, out useUtc, out useInvariant, out needsTranslation);
if (format == null)
throw new FormatException(
"format is not one of the format specifier characters defined for DateTimeFormatInfo");
}
else
{
useInvariant = false;
useUtc = false;
}
if (needsTranslation)
format = ConvertFormatStringNetToJava(format, kind, out foundDateTimeKind);
else
foundDateTimeKind = false;
return format;
}
示例13: Format
public string Format(string format, object arg, IFormatProvider formatProvider)
{
if (formatProvider != null)
{
ICustomFormatter fmt = formatProvider.GetFormat(this.GetType()) as ICustomFormatter;
if (fmt != null) { return fmt.Format(format, this, formatProvider); }
}
Question question = null;
if (arg is Question)
{
question = arg as Question;
}
else
{
var questions = arg as Question[];
question = questions.First(e => e.Key == format);
}
if (question.Value != null)
return question.Value.ToString();
return string.Format(format, "{0}", arg);
}
示例14: ToString
public override string ToString(string format, IFormatProvider provider)
{
if (provider != null)
{
ICustomFormatter formatter = provider.GetFormat(GetType()) as ICustomFormatter;
if (formatter != null)
return formatter.Format(format, this, provider);
}
var builder = new System.Text.StringBuilder();
DoToString(builder, format, 0);
return builder.ToString();
}
示例15: GetInstance
public static NumberFormatInfo GetInstance(IFormatProvider formatProvider) {
// Fast case for a regular CultureInfo
NumberFormatInfo info;
CultureInfo cultureProvider = formatProvider as CultureInfo;
if (cultureProvider != null && !cultureProvider.m_isInherited) {
info = cultureProvider.numInfo;
if (info != null) {
return info;
}
else {
return cultureProvider.NumberFormat;
}
}
// Fast case for an NFI;
info = formatProvider as NumberFormatInfo;
if (info != null) {
return info;
}
if (formatProvider != null) {
info = formatProvider.GetFormat(typeof(NumberFormatInfo)) as NumberFormatInfo;
if (info != null) {
return info;
}
}
return CurrentInfo;
}