Files
2026-06-04 11:42:34 +02:00

85 lines
4.4 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.Guid" /> objects to and from various other representations.</summary>
// Token: 0x02000083 RID: 131
public class GuidConverter : TypeConverter
{
/// <summary>Gets a value indicating whether this converter can convert an object in the given source type to a GUID object 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="sourceType">A <see cref="T:System.Type" /> that represents the type you wish to convert from. </param>
// Token: 0x060004AB RID: 1195 RVA: 0x0000D7F4 File Offset: 0x0000B9F4
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>
// Token: 0x060004AC RID: 1196 RVA: 0x0000D810 File Offset: 0x0000BA10
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 GUID object.</summary>
/// <returns>An <see cref="T:System.Object" /> that represents the converted <paramref name="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>
// Token: 0x060004AD RID: 1197 RVA: 0x0000D84C File Offset: 0x0000BA4C
public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
{
if (value.GetType() == typeof(string))
{
string text = (string)value;
try
{
return new Guid(text);
}
catch
{
throw new FormatException(text + "is not a valid GUID.");
}
}
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>
/// <exception cref="T:System.ArgumentNullException">
/// <paramref name="destinationType" /> is null.</exception>
/// <exception cref="T:System.NotSupportedException">The conversion cannot be performed. </exception>
// Token: 0x060004AE RID: 1198 RVA: 0x0000D8CC File Offset: 0x0000BACC
public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
{
if (value is Guid)
{
Guid guid = (Guid)value;
if (destinationType == typeof(string) && value != null)
{
return guid.ToString("D");
}
if (destinationType == typeof(global::System.ComponentModel.Design.Serialization.InstanceDescriptor))
{
ConstructorInfo constructor = typeof(Guid).GetConstructor(new Type[] { typeof(string) });
return new global::System.ComponentModel.Design.Serialization.InstanceDescriptor(constructor, new object[] { guid.ToString("D") });
}
}
return base.ConvertTo(context, culture, value, destinationType);
}
}
}