本文整理汇总了C#中IFormatProvider.GetType方法的典型用法代码示例。如果您正苦于以下问题:C# IFormatProvider.GetType方法的具体用法?C# IFormatProvider.GetType怎么用?C# IFormatProvider.GetType使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类IFormatProvider
的用法示例。
在下文中一共展示了IFormatProvider.GetType方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: TryParse
/// <summary>
/// Tries to parse the specified string.
/// </summary>
/// <param name="type">The target type.</param>
/// <param name="s">The arguments.</param>
/// <param name="provider">The format provider.</param>
/// <param name="result">The result.</param>
/// <returns>
/// <c>true</c> if parsing successful, <c>false</c> otherwise.
/// </returns>
public static bool TryParse(Type type, string s, IFormatProvider provider, out object result)
{
try
{
var t1 = typeof(string);
var t2 = provider.GetType();
var mi =
type.GetMethods().FirstOrDefault(m =>
{
var p = m.GetParameters();
return m.Name == "Parse" && p.Length == 2 && p[0].ParameterType.IsAssignableFrom(t1) && p[1].ParameterType.IsAssignableFrom(t2);
});
if (mi == null)
{
result = null;
return false;
}
result = mi.Invoke(null, parameters: new object[] { s, provider });
return true;
}
catch
{
result = null;
return false;
}
}
示例2: Good1
public static object Good1(object x, IFormatProvider p)
{
if (x == null)
throw new ArgumentNullException("x");
if (p == null)
throw new ArgumentNullException("p");
return Convert.ChangeType(x, p.GetType(), p);
}
示例3: ToString
//public string ToString(string format, IFormatProvider provider)
//{
//}
#endregion
#region IFormattable Members
//string IFormattable.ToString(string format, IFormatProvider formatProvider)
//{
// throw new Exception("The method or operation is not implemented.");
//}
#endregion
#region IFormattable Members
/// <summary>
/// Convert a runtime context to a specified format string
/// </summary>
/// <param name="format">Format string that determines who a context will be returned</param>
/// <param name="formatProvider">Unique provider for this context</param>
/// <returns>Formatted string</returns>
/// <exception cref="InvalidOperationException">Thrown if formatProvider is not MachineContextFormatter</exception>
public string ToString(string format, IFormatProvider formatProvider)
{
MachineContextFormatter fmt = formatProvider as MachineContextFormatter;
if (fmt == null)
{
throw new InvalidOperationException(TextUtils.StringFormat("Unable to convert {0} to RunTimeContextFormatter", formatProvider.GetType().FullName));
}
return fmt.Format(format, this, formatProvider);
}
示例4: FormatTypeConversionExceptionMesssage
/// <summary>
/// Formats the message for the TypeConversionException
/// </summary>
/// <param name="value">The value that the user tried to convert</param>
/// <param name="defaultValue">The default value used</param>
/// <param name="targetType">The target type</param>
/// <param name="format">The format provider</param>
/// <returns>A string containing the message for the generated TypeConversionException</returns>
public static string FormatTypeConversionExceptionMesssage(object value, object defaultValue, Type targetType, IFormatProvider format)
{
string valueTypeName = (value != null) ? value.GetType().ToString() : "(undefined type)";
string valueString = (value != null) ? (IsDBNull(value) ? "[NULL]" : value.ToString()) : "(null)";
string defaultValueTypeName = (defaultValue != null) ? defaultValue.GetType().ToString() : "(undefined type)";
string defaultValueString = (defaultValue != null) ? (IsDBNull(defaultValue) ? "[NULL]" : defaultValue.ToString()) : "(null)";
string targetTypeName = (targetType != null) ? targetType.ToString() : "(undefined type)";
string formatProviderTypeName = (format != null) ? format.GetType().ToString() : "(undefined type)";
string cultureName = (format != null) ? format.ToString() : string.Empty;
return string.Format(
"Error(s) occured while trying to convert value '{0}' (of type '{1}') to type '{2}' using default value '{3}' (of type '{4}') and format provider '{5}' (of type '{6}')",
valueString,
valueTypeName,
targetTypeName,
defaultValueString,
defaultValueString,
cultureName,
formatProviderTypeName);
}
示例5: Format
public string Format(string format, object arg, IFormatProvider formatProvider)
{
Console.WriteLine("myID is " + myID + ", Format called with formatProvider: " + formatProvider.GetType().AssemblyQualifiedName);
int baseNumber;
string thisFmt = string.Empty;
if (!String.IsNullOrEmpty(format))
thisFmt = format.Length > 1 ? format.Substring(0, 1) : format;
byte[] bytes;
if (arg is sbyte)
{
string byteString = ((sbyte)arg).ToString("X2");
bytes = new byte[1] { Byte.Parse(byteString, NumberStyles.HexNumber) };
}
else if (arg is byte)
{
bytes = new byte[1] { (byte)arg };
}
else if (arg is short)
{
bytes = BitConverter.GetBytes((short)arg);
}
else if (arg is int)
{
bytes = BitConverter.GetBytes((int)arg);
}
else if (arg is long)
{
bytes = BitConverter.GetBytes((long)arg);
}
else if (arg is ushort)
{
bytes = BitConverter.GetBytes((ushort)arg);
}
else if (arg is uint)
{
bytes = BitConverter.GetBytes((uint)arg);
}
else if (arg is ulong)
{
bytes = BitConverter.GetBytes((ulong)arg);
}
else if (arg is BigInteger)
{
bytes = ((BigInteger)arg).ToByteArray();
}
else
{
try
{
return HandleOtherFormats(format, arg);
}
catch (FormatException e)
{
throw new FormatException(String.Format("The format of '{0}' is invalid.", format), e);
}
}
switch (thisFmt.ToUpper())
{
// Binary formatting.
case "B":
baseNumber = 2;
break;
case "O":
baseNumber = 8;
break;
case "H":
baseNumber = 16;
break;
default:
try
{
return HandleOtherFormats(format, arg);
}
catch (FormatException e)
{
throw new FormatException(String.Format("The format of '{0}' is invalid.", format), e);
}
}
string numericString = string.Empty;
//Calculate the minimum required number of characters to display a value to a certain base.
double numChars = Math.Log(256, baseNumber);
int numCharsAsInt = (int)(numChars + 0.5d);
string[] array = new string[4];
Console.WriteLine("Lower bound: " + array.GetLowerBound(0) + ", Upper bound: " + array.GetUpperBound(0));
for (int ctr = bytes.GetUpperBound(0); ctr >= bytes.GetLowerBound(0); ctr--)
{
string byteString = Convert.ToString(bytes[ctr], baseNumber);
byteString = new String('0', numCharsAsInt - byteString.Length) + byteString;
numericString += byteString + " ";
}
return numericString.Trim();
}