-
Notifications
You must be signed in to change notification settings - Fork 9
/
BoolArrayConverter.cs
38 lines (34 loc) · 1.37 KB
/
BoolArrayConverter.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
using System;
using System.ComponentModel;
using System.Globalization;
using System.Linq;
namespace CodeBlockEndTag
{
internal class BoolArrayConverter : TypeConverter
{
public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
{
return sourceType == typeof(string) || base.CanConvertFrom(context, sourceType);
}
public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
{
return destinationType == typeof(bool[]) || base.CanConvertTo(context, destinationType);
}
public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
{
// Bit string to bool array
return value is not string v ?
base.ConvertFrom(context, culture, value) :
v.ToCharArray().Select(chr => chr == '1').ToArray();
}
public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
{
if (destinationType != typeof(string) || value is not bool[] v)
{
return base.ConvertTo(context, culture, value, destinationType);
}
// bool array to bit string
return string.Join("", v.Select(b => b ? '1' : '0'));
}
}
}