forked from UIShell/OSGi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
VersionConverter.cs
60 lines (52 loc) · 2.17 KB
/
VersionConverter.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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
namespace UIShell.OSGi
{
using System;
using System.ComponentModel;
using System.ComponentModel.Design.Serialization;
using System.Globalization;
internal class VersionConverter : TypeConverter
{
public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
{
if (sourceType != typeof(string))
{
return base.CanConvertFrom(context, sourceType);
}
return true;
}
public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
{
if (destinationType != typeof(InstanceDescriptor))
{
return base.CanConvertTo(context, destinationType);
}
return true;
}
public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
{
string version = value as string;
if (version == null)
{
return base.ConvertFrom(context, culture, value);
}
return new Version(version);
}
public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
{
if (destinationType == null)
{
throw new ArgumentNullException("destinationType");
}
Version version = value as Version;
if ((null != version) && (destinationType == typeof(string)))
{
return version.ToString();
}
return base.ConvertTo(context, culture, value, destinationType);
}
public override bool GetCreateInstanceSupported(ITypeDescriptorContext context) => true;
public override PropertyDescriptorCollection GetProperties(ITypeDescriptorContext context, object value, Attribute[] attributes) =>
TypeDescriptor.GetProperties(typeof(VersionRange), attributes).Sort(new string[] { "min", "max" });
public override bool GetPropertiesSupported(ITypeDescriptorContext context) => true;
}
}