forked from nhibernate/nhibernate-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNullableInt32Converter.cs
99 lines (83 loc) · 2.76 KB
/
NullableInt32Converter.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
using System;
using System.Collections;
using System.ComponentModel;
using System.ComponentModel.Design.Serialization;
using System.Globalization;
using System.Reflection;
namespace NHibernate.DomainModel.NHSpecific
{
public class NullableInt32Converter : TypeConverter
{
public NullableInt32Converter()
{
}
public override bool CanConvertFrom(ITypeDescriptorContext context, System.Type sourceType)
{
if (sourceType == typeof(string))
return true;
else
return base.CanConvertFrom(context, sourceType);
}
public override bool CanConvertTo(ITypeDescriptorContext context, System.Type destinationType)
{
if (destinationType == typeof(InstanceDescriptor))
return true;
else
return base.CanConvertTo(context, destinationType);
}
public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
{
if (value == null)
{
return NullableInt32.Default;
}
if (value is string)
{
string stringValue = ((string) value).Trim();
if (stringValue == string.Empty)
return NullableInt32.Default;
//get underlying types converter
TypeConverter converter = TypeDescriptor.GetConverter(typeof(Int32));
Int32 newValue = (Int32) converter.ConvertFromString(context, culture, stringValue);
return new NullableInt32(newValue);
}
else
{
return base.ConvertFrom(context, culture, value);
}
}
public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value,
System.Type destinationType)
{
if (destinationType == typeof(InstanceDescriptor) && value is NullableInt32)
{
NullableInt32 nullable = (NullableInt32) value;
System.Type[] constructorArgTypes = new System.Type[1] {typeof(Int32)};
ConstructorInfo constructor = typeof(NullableInt32).GetConstructor(constructorArgTypes);
if (constructor != null)
{
object[] constructorArgValues = new object[1] {nullable.Value};
return new InstanceDescriptor(constructor, constructorArgValues);
}
}
return base.ConvertTo(context, culture, value, destinationType);
}
public override object CreateInstance(ITypeDescriptorContext context, IDictionary propertyValues)
{
return new NullableInt32((Int32) propertyValues["Value"]);
}
public override bool GetCreateInstanceSupported(ITypeDescriptorContext context)
{
return true;
}
public override PropertyDescriptorCollection GetProperties(ITypeDescriptorContext context, object value,
Attribute[] attributes)
{
return TypeDescriptor.GetProperties(typeof(NullableInt32), attributes);
}
public override bool GetPropertiesSupported(ITypeDescriptorContext context)
{
return true;
}
}
}