Skip to content

Commit

Permalink
Cleanup other TypeConverters
Browse files Browse the repository at this point in the history
  • Loading branch information
hughbe committed May 3, 2023
1 parent 1b5451c commit 5c9fd9c
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 223 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,50 +4,26 @@

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);
}
=> sourceType == typeof(string);

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;
return base.ConvertFrom(context, culture, value);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@

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

namespace System.Xaml.Replacements
{
Expand All @@ -15,51 +15,42 @@ namespace System.Xaml.Replacements
internal class TypeTypeConverter : TypeConverter
{
public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
{
return sourceType == typeof(string);
}
=> sourceType == typeof(string);

public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
{
string typeName = value as string;

if (null != context && null != typeName)
if (context != null && value is string typeName)
{
var typeResolver = GetService<XAML3.IXamlTypeResolver>(context);

if (null != typeResolver)
IXamlTypeResolver typeResolver = GetService<IXamlTypeResolver>(context);
if (typeResolver != null)
{
Type type = typeResolver.Resolve(typeName);
return type;
return typeResolver.Resolve(typeName);
}
}

return base.ConvertFrom(context, culture, value);
}

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

public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
{
Type type = value as Type;

if (context != null && type != null && destinationType == typeof(string))
if (context != null && value is Type type && destinationType == typeof(string))
{
string result = ConvertTypeToString(context, type);
if (result != null)
{
return result;
}
}

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

private static string ConvertTypeToString(ITypeDescriptorContext context, Type type)
{
var schemaContextProvider = GetService<IXamlSchemaContextProvider>(context);
IXamlSchemaContextProvider schemaContextProvider = GetService<IXamlSchemaContextProvider>(context);
if (schemaContextProvider == null)
{
return null;
Expand All @@ -68,17 +59,17 @@ private static string ConvertTypeToString(ITypeDescriptorContext context, Type t
{
return null;
}

XamlType xamlType = schemaContextProvider.SchemaContext.GetXamlType(type);
if (xamlType == null)
{
return null;
}

return XamlTypeTypeConverter.ConvertXamlTypeToString(context, xamlType);
}

private static TService GetService<TService>(ITypeDescriptorContext context) where TService : class
{
return context.GetService(typeof(TService)) as TService;
}
=> context.GetService(typeof(TService)) as TService;
}
}
Loading

0 comments on commit 5c9fd9c

Please sign in to comment.