forked from KhaosCoders/VSCodeBlockEndTag
-
Notifications
You must be signed in to change notification settings - Fork 1
/
BoolArrayConverter.cs
39 lines (35 loc) · 1.38 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
39
using System;
using System.ComponentModel;
using System.Globalization;
using System.Linq;
namespace CodeBlockEndTag
{
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 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)
{
var v = value as bool[];
if (destinationType != typeof(string) || v == null)
{
return base.ConvertTo(context, culture, value, destinationType);
}
// bool array to bit string
return string.Join("", v.Select(b => b ? '1' : '0'));
}
}
}