Skip to content

Commit

Permalink
Cleanup TypeConverter replacements
Browse files Browse the repository at this point in the history
  • Loading branch information
hughbe committed May 2, 2023
1 parent 7228d1c commit ecce08d
Show file tree
Hide file tree
Showing 8 changed files with 68 additions and 214 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,18 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

// Contents: Limited converter for string <--> System.Uri
using System.ComponentModel;
using System.ComponentModel.Design.Serialization;
using System.Globalization;
using System.Reflection;

namespace System.Xaml.Replacements
{
using System;
using System.ComponentModel;
using System.ComponentModel.Design.Serialization;
using System.Globalization;
using System.Reflection;

/// <summary>
/// Limited converter for string <--> System.Uri
/// </summary>
internal class TypeUriConverter : TypeConverter
{
public TypeUriConverter()
{
}

/// <inheritdoc />
public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
{
if (sourceType == null)
Expand All @@ -29,7 +24,6 @@ public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceT
return sourceType == typeof(string) || sourceType == typeof(Uri);
}

/// <inheritdoc />
public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
{
return
Expand All @@ -38,39 +32,26 @@ public override bool CanConvertTo(ITypeDescriptorContext context, Type destinati
destinationType == typeof(Uri);
}

/// <inheritdoc />
public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
{
var uri = value as Uri;
if (uri != null)
if (value is Uri uri)
{
var uriKind = UriKind.RelativeOrAbsolute;
UriKind uriKind = UriKind.RelativeOrAbsolute;
if (uri.IsWellFormedOriginalString())
{
uriKind = uri.IsAbsoluteUri ? UriKind.Absolute : UriKind.Relative;
}

if (destinationType == typeof(InstanceDescriptor))
{
var ci =
typeof(Uri)
.GetConstructor(
BindingFlags.Public | BindingFlags.Instance,
null,
new Type[] { typeof(string), typeof(UriKind) },
null);
return
new InstanceDescriptor(
ci,
new object[] { uri.OriginalString, uriKind});
ConstructorInfo constructor = typeof(Uri).GetConstructor(new Type[] { typeof(string), typeof(UriKind) });
return new InstanceDescriptor(constructor, new object[] { uri.OriginalString, uriKind });
}

if (destinationType == typeof(string))
else if (destinationType == typeof(string))
{
return uri.OriginalString;
}

if (destinationType == typeof(Uri))
else if (destinationType == typeof(Uri))
{
return new Uri(uri.OriginalString, uriKind);
}
Expand All @@ -79,11 +60,9 @@ public override object ConvertTo(ITypeDescriptorContext context, CultureInfo cul
return base.ConvertTo(context, culture, value, destinationType);
}

/// <inheritdoc />
public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
{
var uriString = value as string;
if (uriString != null)
if (value is string uriString)
{
if (Uri.IsWellFormedUriString(uriString, UriKind.Absolute))
{
Expand All @@ -98,8 +77,7 @@ public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo c
return new Uri(uriString, UriKind.RelativeOrAbsolute);
}

var uri = value as Uri;
if (uri != null)
if (value is Uri uri)
{
if (uri.IsWellFormedOriginalString())
{
Expand All @@ -112,11 +90,9 @@ public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo c
return base.ConvertFrom(context, culture, value);
}

/// <inheritdoc />
public override bool IsValid(ITypeDescriptorContext context, object value)
{
var uriString = value as string;
if (uriString != null)
if (value is string uriString)
{
return Uri.TryCreate(uriString, UriKind.RelativeOrAbsolute, out _);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,10 @@

namespace System.Xaml.Replacements
{

//+--------------------------------------------------------------------------------------
//
// DateTimeConverter2
//
// This internal class simply wraps the DateTimeValueSerializer, to make it compatible with
// internal code that expects a type converter.
//
//+--------------------------------------------------------------------------------------

/// <summary>
/// This internal class simply wraps the DateTimeValueSerializer, to make it compatible with
/// internal code that expects a type converter.
/// </summary>
internal class DateTimeConverter2 : TypeConverter
{
public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
Expand All @@ -32,30 +26,29 @@ public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceT

public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
{
if (destinationType == typeof(string))
if (destinationType == typeof(string))
{
return true;
}

return base.CanConvertTo(context, destinationType);
}

public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
{
return _dateTimeValueSerializer.ConvertFromString( value as string, _valueSerializerContext );
return _dateTimeValueSerializer.ConvertFromString(value as string, null);
}

public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
{
if (destinationType == typeof(string) && value is DateTime)
{
return _dateTimeValueSerializer.ConvertToString(value, _valueSerializerContext);
return _dateTimeValueSerializer.ConvertToString(value, null);
}

return base.ConvertTo(context, culture, value, destinationType);
}

private DateTimeValueSerializer _dateTimeValueSerializer = new DateTimeValueSerializer();
private IValueSerializerContext _valueSerializerContext = new DateTimeValueSerializerContext();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,17 @@
using System.Reflection;
using System.Security;

namespace System.Windows.Markup
namespace System.Xaml.Replacements
{
class DateTimeOffsetConverter2 : TypeConverter
internal class DateTimeOffsetConverter2 : TypeConverter
{
public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
{
if (destinationType == typeof(string) || destinationType == typeof(InstanceDescriptor))
{
return true;
}

return base.CanConvertTo(context, destinationType);
}

Expand All @@ -27,42 +28,32 @@ public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceT
{
return true;
}

return base.CanConvertFrom(context, sourceType);
}

public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
{
if ((destinationType == typeof(string)) && (value is DateTimeOffset))
if (value is DateTimeOffset dtOffset)
{
if (culture == null)
if (destinationType == typeof(string))
{
culture = CultureInfo.CurrentCulture;
return dtOffset.ToString("O", culture ?? CultureInfo.CurrentCulture);
}

return ((DateTimeOffset)value).ToString("O", culture);
}
if ((destinationType == typeof(InstanceDescriptor)) && (value is DateTimeOffset))
{
// Use the year, month, day, hour, minute, second, millisecond, offset constructor
// Should there be a branch to use the calendar constructor?
DateTimeOffset dtOffset = (DateTimeOffset)value;

Type intType = typeof(int);
ConstructorInfo constructor = typeof(DateTimeOffset).GetConstructor(
new Type[] {
intType,
intType,
intType,
intType,
intType,
intType,
intType,
typeof(TimeSpan)
}
);

if (constructor != null)
else if (destinationType == typeof(InstanceDescriptor))
{
// Use the year, month, day, hour, minute, second, millisecond, offset constructor
ConstructorInfo constructor = typeof(DateTimeOffset).GetConstructor(new Type[]
{
typeof(int),
typeof(int),
typeof(int),
typeof(int),
typeof(int),
typeof(int),
typeof(int),
typeof(TimeSpan)
});
return new InstanceDescriptor(
constructor,
new object[] {
Expand All @@ -77,23 +68,16 @@ public override object ConvertTo(ITypeDescriptorContext context, CultureInfo cul
},
true);
}

return null;
}

return base.ConvertTo(context, culture, value, destinationType);
}

public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
{
if (value is string)
if (value is string s)
{
string s = ((string)value).Trim();

if (culture == null)
{
culture = CultureInfo.CurrentCulture;
}
return DateTimeOffset.Parse(s, culture, DateTimeStyles.None);
return DateTimeOffset.Parse(s.Trim(), culture ?? CultureInfo.CurrentCulture, DateTimeStyles.None);
}

return base.ConvertFrom(context, culture, value);
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,14 @@

using System.ComponentModel;
using System.Globalization;
using XAML3 = System.Windows.Markup;

namespace System.Xaml.Replacements
{
// Not sure if this type converter is used at all.
// we need to either make this a useful type converter or remove the code.

/// <summary>
/// TypeConverter for System.Type[]
/// </summary>
internal class TypeListConverter : TypeConverter
{
private static readonly TypeTypeConverter typeTypeConverter = new TypeTypeConverter();
public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
{
return sourceType == typeof(string);
Expand All @@ -25,29 +20,12 @@ public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceT
public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
{
string typeList = (string)value;
if (null != context)
if (context != null)
{
// Consider HashMap(int, int), HashMap(int, int)
string[] tl = StringHelpers.SplitTypeList(typeList);
Type[] types = new Type[tl.Length];
for (int i = 0; i < tl.Length; i++)
{
types[i] = (Type)typeTypeConverter.ConvertFrom(context, TypeConverterHelper.InvariantEnglishUS, tl[i]);
}
return types;
throw new NullReferenceException();
}
return base.ConvertFrom(context, culture, value);
}


}

internal static class StringHelpers
{
// split top level types and strip out whitespace
public static string[] SplitTypeList(string typeList)
{
return null;
throw GetConvertFromException(value);
}
}
}
Loading

0 comments on commit ecce08d

Please sign in to comment.