本文整理汇总了C#中System.Globalization.CultureInfo.GetFormat方法的典型用法代码示例。如果您正苦于以下问题:C# CultureInfo.GetFormat方法的具体用法?C# CultureInfo.GetFormat怎么用?C# CultureInfo.GetFormat使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Globalization.CultureInfo
的用法示例。
在下文中一共展示了CultureInfo.GetFormat方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: ConvertFrom
public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
{
if (value is string)
{
string s = ((string) value).Trim();
if (s.Length == 0)
{
return DateTime.MinValue;
}
try
{
DateTimeFormatInfo provider = null;
if (culture != null)
{
provider = (DateTimeFormatInfo) culture.GetFormat(typeof(DateTimeFormatInfo));
}
if (provider != null)
{
return DateTime.Parse(s, provider);
}
return DateTime.Parse(s, culture);
}
catch (FormatException exception)
{
throw new FormatException(SR.GetString("ConvertInvalidPrimitive", new object[] { (string) value, "DateTime" }), exception);
}
}
return base.ConvertFrom(context, culture, value);
}
示例2: ConvertFrom
public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
{
var str = value as string;
if (str != null)
{
string text = str.Trim();
try
{
object result;
if (AllowHex && text[0] == '#')
{
result = FromString(text.Substring(1), 16);
return result;
}
if ((AllowHex && text.StartsWith("0x", StringComparison.OrdinalIgnoreCase)) || text.StartsWith("&h", StringComparison.OrdinalIgnoreCase))
{
result = FromString(text.Substring(2), 16);
return result;
}
culture = culture ?? CultureInfo.CurrentCulture;
var formatInfo = (NumberFormatInfo)culture.GetFormat(typeof(NumberFormatInfo));
result = FromString(text, formatInfo);
return result;
}
catch (Exception innerException)
{
throw new Exception(text, innerException);
}
}
return base.ConvertFrom(context, culture, value);
}
示例3: ConvertFrom
public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
{
if (value is string) {
string textValue = ((string) value).Trim();
Int64 returnValue;
try {
if (textValue[0] == '#') {
return Convert.ToInt64(textValue.Substring(1), 0x10);
}
if (textValue.StartsWith("0x") ||
textValue.StartsWith("0X") ||
textValue.StartsWith("&h") ||
textValue.StartsWith("&H")) {
return Convert.ToInt64(textValue.Substring(2), 0x10);
}
if (culture == null) {
culture = CultureInfo.CurrentCulture;
}
NumberFormatInfo formatInfo = (NumberFormatInfo) culture.GetFormat(typeof(NumberFormatInfo));
returnValue = Int64.Parse(textValue, NumberStyles.Integer, formatInfo);
} catch (Exception exception) {
throw new Exception("Failed to ConvertFrom: " + textValue, exception);
}
if (IsValid(context, returnValue) == false) {
throw new Exception("Value is not in the valid range of numbers.");
}
return returnValue;
}
return base.ConvertFrom(context, culture, value);
}
示例4: ConvertFrom
/// <devdoc>
/// <para>Converts the given value object to a <see cref='System.DateTime'/>
/// object.</para>
/// </devdoc>
public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value) {
if (value is string) {
string text = ((string)value).Trim();
if (text.Length == 0) {
return DateTimeOffset.MinValue;
}
try {
// See if we have a culture info to parse with. If so, then use it.
//
DateTimeFormatInfo formatInfo = null;
if (culture != null ) {
formatInfo = (DateTimeFormatInfo)culture.GetFormat(typeof(DateTimeFormatInfo));
}
if (formatInfo != null) {
return DateTimeOffset.Parse(text, formatInfo);
}
else {
return DateTimeOffset.Parse(text, culture);
}
}
catch (FormatException e) {
throw new FormatException(SR.GetString(SR.ConvertInvalidPrimitive, (string)value, "DateTimeOffset"), e);
}
}
return base.ConvertFrom(context, culture, value);
}
示例5: ConvertFrom
public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
{
if (value is string)
{
string str = ((string) value).Trim();
try
{
if (this.AllowHex && (str[0] == '#'))
{
return this.FromString(str.Substring(1), 0x10);
}
if ((this.AllowHex && str.StartsWith("0x")) || ((str.StartsWith("0X") || str.StartsWith("&h")) || str.StartsWith("&H")))
{
return this.FromString(str.Substring(2), 0x10);
}
if (culture == null)
{
culture = CultureInfo.CurrentCulture;
}
NumberFormatInfo format = (NumberFormatInfo) culture.GetFormat(typeof(NumberFormatInfo));
return this.FromString(str, format);
}
catch (Exception exception)
{
throw this.FromStringError(str, exception);
}
}
return base.ConvertFrom(context, culture, value);
}
示例6: ConvertFrom
public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
{
if (culture == null)
culture = CultureInfo.CurrentCulture;
string text = value as string;
if (text != null) {
try {
if (SupportHex) {
if (text.Length >= 1 && text[0] == '#') {
return ConvertFromString (text.Substring (1), 16);
}
if (text.StartsWith ("0x") || text.StartsWith ("0X")) {
return ConvertFromString (text, 16);
}
}
NumberFormatInfo numberFormatInfo = (NumberFormatInfo) culture.GetFormat(typeof(NumberFormatInfo));
return ConvertFromString (text, numberFormatInfo);
} catch (Exception e) {
// LAMESPEC MS wraps the actual exception in an Exception
throw new Exception (value.ToString() + " is not a valid "
+ "value for " + InnerType.Name + ".", e);
}
}
return base.ConvertFrom (context, culture, value);
}
示例7: ConvertFrom
public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
{
var s = value as string;
if (s != null)
{
s = s.Trim();
if (s.Length == 0)
{
return DateTime.MinValue;
}
try
{
DateTimeFormatInfo provider = null;
if (culture != null)
{
provider = (DateTimeFormatInfo) culture.GetFormat(typeof(DateTimeFormatInfo));
}
if (provider != null)
{
return DateTime.Parse(s, provider);
}
return DateTime.Parse(s, culture);
}
catch (FormatException exception)
{
throw new FormatException("Invalid DateTime!");
}
}
return base.ConvertFrom(context, culture, value);
}
示例8: BirthDateFormatInfo
/// <summary>
/// Initializes a new instance of the <see cref="BirthDateFormatInfo" /> class.
/// </summary>
/// <param name="representMissingComponentsWithX">
/// If set to <c>true</c> represent missing components with X; otherwise, use 0.</param>
/// <param name="culture">The culture.</param>
public BirthDateFormatInfo(CultureInfo culture = null, bool representMissingComponentsWithX = true)
{
if (culture == null)
culture = CultureInfo.CurrentCulture;
RepresentMissingComponentsWithX = representMissingComponentsWithX;
Culture = culture;
DateTimeFormatInfo = (DateTimeFormatInfo)culture.GetFormat(typeof(DateTimeFormatInfo));
}
示例9: ConvertTo
public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
{
if ((destinationType == typeof(string)) && (value is DateTime))
{
string shortDatePattern;
DateTime time = (DateTime) value;
if (time == DateTime.MinValue)
{
return string.Empty;
}
if (culture == null)
{
culture = CultureInfo.CurrentCulture;
}
DateTimeFormatInfo format = null;
format = (DateTimeFormatInfo) culture.GetFormat(typeof(DateTimeFormatInfo));
if (culture == CultureInfo.InvariantCulture)
{
if (time.TimeOfDay.TotalSeconds == 0.0)
{
return time.ToString("yyyy-MM-dd", culture);
}
return time.ToString(culture);
}
if (time.TimeOfDay.TotalSeconds == 0.0)
{
shortDatePattern = format.ShortDatePattern;
}
else
{
shortDatePattern = format.ShortDatePattern + " " + format.ShortTimePattern;
}
return time.ToString(shortDatePattern, CultureInfo.CurrentCulture);
}
if ((destinationType == typeof(InstanceDescriptor)) && (value is DateTime))
{
DateTime time2 = (DateTime) value;
if (time2.Ticks == 0L)
{
ConstructorInfo member = typeof(DateTime).GetConstructor(new Type[] { typeof(long) });
if (member != null)
{
return new InstanceDescriptor(member, new object[] { time2.Ticks });
}
}
ConstructorInfo constructor = typeof(DateTime).GetConstructor(new Type[] { typeof(int), typeof(int), typeof(int), typeof(int), typeof(int), typeof(int), typeof(int) });
if (constructor != null)
{
return new InstanceDescriptor(constructor, new object[] { time2.Year, time2.Month, time2.Day, time2.Hour, time2.Minute, time2.Second, time2.Millisecond });
}
}
return base.ConvertTo(context, culture, value, destinationType);
}
示例10: ConvertFromString
/// <summary>
/// Converts the string to an object.
/// </summary>
/// <param name="culture">The culture used when converting.</param>
/// <param name="text">The string to convert to an object.</param>
/// <returns>The object created from the string.</returns>
public override object ConvertFromString( CultureInfo culture, string text )
{
var formatProvider = (IFormatProvider)culture.GetFormat( typeof( DateTimeFormatInfo ) ) ?? culture;
DateTime dt;
if( DateTime.TryParse( text, formatProvider, DateTimeStyles.None, out dt ) )
{
return dt;
}
return base.ConvertFromString( culture, text );
}
示例11: ConvertFrom
/// <summary>
/// Converts the specified object to a <see cref="T:System.DateTime" />
/// with the specified culture with the specified format context.
/// </summary>
/// <param name="context">
/// The format context that is used to convert the specified type.
/// </param>
/// <param name="culture">The culture to use for the result.</param>
/// <param name="value">The value to convert.</param>
/// <returns>
/// A <see cref="T:System.DateTime" /> object that represents
/// <paramref name="value" />.
/// </returns>
/// <exception cref="System.FormatException">
/// The conversion cannot be performed.
/// </exception>
/// <exception cref="System.ArgumentNullException">
/// The culture is null.
/// </exception>
/// <exception cref="System.ArgumentNullException">
/// The value is null.
/// </exception>
public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
{
if (culture == null)
{
throw new ArgumentNullException("culture");
}
if (value == null)
{
throw new ArgumentNullException("value");
}
DateTimeFormatInfo info = (DateTimeFormatInfo) culture.GetFormat(typeof(DateTimeFormatInfo));
return DateTime.ParseExact(value.ToString(), info.ShortDatePattern, culture);
}
示例12: ConvertTo
public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType) {
if (destinationType == null) {
throw new ArgumentNullException("destinationType");
}
if (((destinationType == typeof(string)) && (value != null)) && this.TargetType.IsInstanceOfType(value)) {
if (culture == null) {
culture = CultureInfo.CurrentCulture;
}
NumberFormatInfo format = (NumberFormatInfo)culture.GetFormat(typeof(NumberFormatInfo));
return this.ToString(value, format);
}
if (destinationType.IsPrimitive) {
return Convert.ChangeType(value, destinationType, culture);
}
return base.ConvertTo(context, culture, value, destinationType);
}
示例13: ConvertFrom
public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
{
if (culture == null)
culture = CultureInfo.CurrentCulture;
string text = value as string;
if (text != null) {
try {
if (SupportHex) {
if (text.Length >= 1 && text[0] == '#') {
return ConvertFromString (text.Substring (1), 16);
}
if (text.StartsWith ("0x") || text.StartsWith ("0X")) {
return ConvertFromString (text, 16);
}
}
NumberFormatInfo numberFormatInfo = (NumberFormatInfo) culture.GetFormat(typeof(NumberFormatInfo));
if(text.EndsWith("mm")) {
text = text.Replace("mm",string.Empty);
double val = (double) ConvertFromString (text, numberFormatInfo);
return val.mm();
}else if(text.EndsWith("cm")){
text = text.Replace("cm",string.Empty);
double val = (double) ConvertFromString (text, numberFormatInfo);
return val.cm();
}else if(text.EndsWith("in")){
text = text.Replace("in",string.Empty);
double val = (double) ConvertFromString (text, numberFormatInfo);
return val.inch();
}else if(text.EndsWith("pt")){
text = text.Replace("pt",string.Empty);
double val = (double) ConvertFromString (text, numberFormatInfo);
return val.pt();
}
return ConvertFromString (text, numberFormatInfo);
} catch (Exception e) {
// LAMESPEC MS wraps the actual exception in an Exception
throw new Exception (value.ToString() + Catalog.GetString(" is not a valid value for ") + InnerType.Name + ".", e);
}
}
return base.ConvertFrom (context, culture, value);
}
示例14: ConvertFrom
public override object ConvertFrom (ITypeDescriptorContext context, CultureInfo culture, object value)
{
if (value is string) {
string DateString = (string) value;
try {
if (DateString != null && DateString.Trim ().Length == 0) {
return DateTime.MinValue;
} else if (culture == null) {
return DateTime.Parse (DateString);
} else {
DateTimeFormatInfo info = (DateTimeFormatInfo) culture.GetFormat (typeof (DateTimeFormatInfo));
return DateTime.Parse (DateString, info);
}
} catch {
throw new FormatException (DateString + " is not a valid DateTime value.");
}
}
return base.ConvertFrom (context, culture, value);
}
示例15: ConvertFrom
/// <summary>
/// converts datetime from format yyyy/MM/dd HH:mm:ss
/// </summary>
/// <param name="context"></param>
/// <param name="culture"></param>
/// <param name="value"></param>
/// <returns></returns>
public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
{
if (value is string)
{
var info = (DateTimeFormatInfo)culture.GetFormat(typeof(DateTimeFormatInfo));
var DateString = (string)value;
try
{
return DateTime.Parse(DateString, info);
}
catch
{
throw new FormatException(
string.Format("{0} is not a valid DateTime value. The format should be {1}", DateString, RegionalSettingsManager.DateTimeFormat));
}
}
return base.ConvertFrom(context, culture, value);
}