88 lines
4.7 KiB
C#
88 lines
4.7 KiB
C#
using System;
|
|
using System.ComponentModel.Design.Serialization;
|
|
using System.Globalization;
|
|
using System.Reflection;
|
|
|
|
namespace System.ComponentModel
|
|
{
|
|
/// <summary>Provides a type converter to convert <see cref="T:System.TimeSpan" /> objects to and from other representations.</summary>
|
|
// Token: 0x020000C3 RID: 195
|
|
public class TimeSpanConverter : TypeConverter
|
|
{
|
|
/// <summary>Gets a value indicating whether this converter can convert an object in the given source type to a <see cref="T:System.TimeSpan" /> using the specified context.</summary>
|
|
/// <returns>true if this converter can perform the conversion; otherwise, false.</returns>
|
|
/// <param name="context">An <see cref="T:System.ComponentModel.ITypeDescriptorContext" /> that provides a format context. </param>
|
|
/// <param name="sourceType">A <see cref="T:System.Type" /> that represents the type you wish to convert from. </param>
|
|
// Token: 0x0600066C RID: 1644 RVA: 0x00010CE0 File Offset: 0x0000EEE0
|
|
public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
|
|
{
|
|
return sourceType == typeof(string) || base.CanConvertFrom(context, sourceType);
|
|
}
|
|
|
|
/// <summary>Gets a value indicating whether this converter can convert an object to the given destination type using the context.</summary>
|
|
/// <returns>true if this converter can perform the conversion; otherwise, false.</returns>
|
|
/// <param name="context">An <see cref="T:System.ComponentModel.ITypeDescriptorContext" /> that provides a format context. </param>
|
|
/// <param name="destinationType">A <see cref="T:System.Type" /> that represents the type you wish to convert to. </param>
|
|
/// <exception cref="T:System.ArgumentNullException">
|
|
/// <paramref name="destinationType" /> is null.</exception>
|
|
/// <exception cref="T:System.FormatException">
|
|
/// <paramref name="value" /> is not a valid value for the target type. </exception>
|
|
// Token: 0x0600066D RID: 1645 RVA: 0x00010CFC File Offset: 0x0000EEFC
|
|
public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
|
|
{
|
|
return destinationType == typeof(string) || destinationType == typeof(global::System.ComponentModel.Design.Serialization.InstanceDescriptor) || base.CanConvertTo(context, destinationType);
|
|
}
|
|
|
|
/// <summary>Converts the given object to a <see cref="T:System.TimeSpan" />.</summary>
|
|
/// <returns>An <see cref="T:System.Object" /> that represents the converted value.</returns>
|
|
/// <param name="context">An <see cref="T:System.ComponentModel.ITypeDescriptorContext" /> that provides a format context. </param>
|
|
/// <param name="culture">An optional <see cref="T:System.Globalization.CultureInfo" />. If not supplied, the current culture is assumed. </param>
|
|
/// <param name="value">The <see cref="T:System.Object" /> to convert. </param>
|
|
/// <exception cref="T:System.NotSupportedException">The conversion cannot be performed. </exception>
|
|
/// <exception cref="T:System.FormatException">
|
|
/// <paramref name="value" /> is not a valid value for the target type. </exception>
|
|
// Token: 0x0600066E RID: 1646 RVA: 0x00010D38 File Offset: 0x0000EF38
|
|
public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
|
|
{
|
|
if (value.GetType() == typeof(string))
|
|
{
|
|
string text = (string)value;
|
|
try
|
|
{
|
|
return TimeSpan.Parse(text);
|
|
}
|
|
catch
|
|
{
|
|
throw new FormatException(text + "is not valid for a TimeSpan.");
|
|
}
|
|
}
|
|
return base.ConvertFrom(context, culture, value);
|
|
}
|
|
|
|
/// <summary>Converts the given object to another type. </summary>
|
|
/// <returns>The converted object.</returns>
|
|
/// <param name="context">A formatter context. </param>
|
|
/// <param name="culture">The culture into which <paramref name="value" /> will be converted.</param>
|
|
/// <param name="value">The object to convert. </param>
|
|
/// <param name="destinationType">The type to convert the object to. </param>
|
|
// Token: 0x0600066F RID: 1647 RVA: 0x00010DB8 File Offset: 0x0000EFB8
|
|
public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
|
|
{
|
|
if (value is TimeSpan)
|
|
{
|
|
TimeSpan timeSpan = (TimeSpan)value;
|
|
if (destinationType == typeof(string) && value != null)
|
|
{
|
|
return timeSpan.ToString();
|
|
}
|
|
if (destinationType == typeof(global::System.ComponentModel.Design.Serialization.InstanceDescriptor))
|
|
{
|
|
ConstructorInfo constructor = typeof(TimeSpan).GetConstructor(new Type[] { typeof(long) });
|
|
return new global::System.ComponentModel.Design.Serialization.InstanceDescriptor(constructor, new object[] { timeSpan.Ticks });
|
|
}
|
|
}
|
|
return base.ConvertTo(context, culture, value, destinationType);
|
|
}
|
|
}
|
|
}
|