recipe name filter
This commit is contained in:
@@ -276,6 +276,10 @@ namespace Inspectron.Settings
|
||||
{
|
||||
value = converter.ConvertFromInvariantString(valueString);
|
||||
}
|
||||
else if (TypeConverterRegistry.CanConvert(type))
|
||||
{
|
||||
value = TypeConverterRegistry.GetConverter(type).ConvertFrom(valueString);
|
||||
}
|
||||
else
|
||||
{
|
||||
// deserialize
|
||||
@@ -295,8 +299,8 @@ namespace Inspectron.Settings
|
||||
}
|
||||
private static bool CanConvertToAndFromString(TypeConverter converter)
|
||||
{
|
||||
return converter.CanConvertFrom(typeof(string)) &&
|
||||
converter.CanConvertTo(typeof(string));
|
||||
return (converter.CanConvertFrom(typeof(string)) &&
|
||||
converter.CanConvertTo(typeof(string)));
|
||||
}
|
||||
/// <summary>
|
||||
/// Class for group of user settings</summary>
|
||||
@@ -478,6 +482,10 @@ namespace Inspectron.Settings
|
||||
{
|
||||
valueString = converter.ConvertToInvariantString(value);
|
||||
}
|
||||
else if (TypeConverterRegistry.TryGetConverter(type, out var typeConverter))
|
||||
{
|
||||
valueString = typeConverter.ConvertTo(value, typeof(string)) as string;
|
||||
}
|
||||
else if (type.IsSerializable)
|
||||
{
|
||||
// serialize
|
||||
|
||||
57
framework/Inspectron.Settings/TypeConverterRegistry.cs
Normal file
57
framework/Inspectron.Settings/TypeConverterRegistry.cs
Normal file
@@ -0,0 +1,57 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace Inspectron.Settings
|
||||
{
|
||||
public static class TypeConverterRegistry
|
||||
{
|
||||
private static readonly Dictionary<Type, ITypeConverter> _converters = new Dictionary<Type, ITypeConverter>();
|
||||
|
||||
public static void Register<T>(ITypeConverter converter)
|
||||
{
|
||||
_converters[typeof(T)] = converter;
|
||||
}
|
||||
|
||||
public static ITypeConverter GetConverter<T>()
|
||||
{
|
||||
return _converters[typeof(T)];
|
||||
}
|
||||
|
||||
public static ITypeConverter GetConverter(Type type)
|
||||
{
|
||||
if (_converters.TryGetValue(type, out var converter))
|
||||
{
|
||||
return converter;
|
||||
}
|
||||
throw new KeyNotFoundException($"No converter registered for type {type.FullName}");
|
||||
}
|
||||
|
||||
public static bool TryGetConverter<T>(out ITypeConverter converter)
|
||||
{
|
||||
return _converters.TryGetValue(typeof(T), out converter);
|
||||
}
|
||||
|
||||
public static bool TryGetConverter(Type type, out ITypeConverter converter)
|
||||
{
|
||||
return _converters.TryGetValue(type, out converter);
|
||||
}
|
||||
|
||||
public static bool CanConvert<T>()
|
||||
{
|
||||
return _converters.ContainsKey(typeof(T));
|
||||
}
|
||||
public static bool CanConvert(Type type)
|
||||
{
|
||||
return _converters.ContainsKey(type);
|
||||
}
|
||||
}
|
||||
|
||||
public interface ITypeConverter
|
||||
{
|
||||
// Converts an object from one type to another
|
||||
object ConvertFrom(object value);
|
||||
|
||||
// Converts an object to a specified type
|
||||
object ConvertTo(object value, Type destinationType);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user