Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cleanup TypeConverter replacements #203

Merged
merged 5 commits into from
Oct 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,25 @@
// 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)
{
ArgumentNullException.ThrowIfNull(sourceType);

return sourceType == typeof(string) || sourceType == typeof(Uri);
}

/// <inheritdoc />
public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
{
return
Expand All @@ -35,39 +29,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 @@ -76,11 +57,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 @@ -95,8 +74,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 @@ -109,11 +87,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,27 @@ 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 );
}
=> _dateTimeValueSerializer.ConvertFromString(value as string, null);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since DateTimeValueSerializerContext doesn't have any functionality, we can just pass null and remove the class here and in the other DateTimeConverter2


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();
private readonly DateTimeValueSerializer _dateTimeValueSerializer = new DateTimeValueSerializer();
}
}
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[]
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

constructor will always exist, remove null check

{
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.