Auto-generate configuration dialog, fix logger names, prepare for async initialization
This commit is contained in:
54
Torch.Server/Views/Converters/ListConverter.cs
Normal file
54
Torch.Server/Views/Converters/ListConverter.cs
Normal file
@@ -0,0 +1,54 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel;
|
||||
using System.Globalization;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using System.Windows.Data;
|
||||
using System.Windows.Navigation;
|
||||
|
||||
namespace Torch.Server.Views.Converters
|
||||
{
|
||||
public class ListConverter : IValueConverter
|
||||
{
|
||||
public Type Type { get; set; }
|
||||
|
||||
/// <inheritdoc />
|
||||
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
|
||||
{
|
||||
if (!(value is IList list))
|
||||
throw new InvalidOperationException("Value is not the proper type.");
|
||||
|
||||
var sb = new StringBuilder();
|
||||
foreach (var item in list)
|
||||
{
|
||||
sb.AppendLine(item.ToString());
|
||||
}
|
||||
|
||||
return sb.ToString();
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
|
||||
{
|
||||
var list = (IList)Activator.CreateInstance(typeof(List<>).MakeGenericType(Type));
|
||||
var converter = TypeDescriptor.GetConverter(Type);
|
||||
var input = ((string)value).Split(new[] {Environment.NewLine}, StringSplitOptions.RemoveEmptyEntries);
|
||||
foreach (var item in input)
|
||||
{
|
||||
try
|
||||
{
|
||||
list.Add(converter.ConvertFromString(item));
|
||||
}
|
||||
catch
|
||||
{
|
||||
throw new InvalidOperationException("Could not convert input value.");
|
||||
}
|
||||
}
|
||||
|
||||
return list;
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user