-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
_UriTypeConverter.cs
114 lines (86 loc) · 3.61 KB
/
_UriTypeConverter.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
100
101
102
103
104
105
106
107
108
109
110
111
112
/*++
Copyright (c) 2003 Microsoft Corporation
Module Name:
_UriTypeConverter.cs
Abstract:
A default TypeConverter implementation for the System.Uri type
Revision History:
--*/
namespace System {
using System.Globalization;
using System.ComponentModel;
using System.ComponentModel.Design.Serialization;
using System.Reflection;
//
// A limited conversion is implemented such as to and from string
// A conversion to InstanceDescriptor is also provided for design time support.
//
public class UriTypeConverter: TypeConverter
{
private UriKind m_UriKind;
public UriTypeConverter() : this(UriKind.RelativeOrAbsolute) { }
internal UriTypeConverter(UriKind uriKind)
{
m_UriKind = uriKind;
}
public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
{
if (sourceType == null)
throw new ArgumentNullException("sourceType");
if (sourceType == typeof(string))
return true;
if (typeof(Uri).IsAssignableFrom(sourceType))
return true;
return base.CanConvertFrom(context, sourceType);
}
//
public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
{
if (destinationType == typeof(InstanceDescriptor))
return true;
if (destinationType == typeof(string))
return true;
if (destinationType == typeof(Uri))
return true;
return base.CanConvertTo(context, destinationType);
}
//
public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
{
string uriString = value as string;
if (uriString != null)
return new Uri(uriString, m_UriKind);
Uri uri = value as Uri;
if (uri != null)
return new Uri(uri.OriginalString,
m_UriKind == UriKind.RelativeOrAbsolute ? uri.IsAbsoluteUri ? UriKind.Absolute : UriKind.Relative : m_UriKind);
return base.ConvertFrom(context, culture, value);
}
//
public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
{
Uri uri = value as Uri;
if (uri != null && destinationType == typeof(InstanceDescriptor))
{
ConstructorInfo ci = typeof(Uri).GetConstructor(BindingFlags.Public | BindingFlags.Instance, null, new Type[]{typeof(string), typeof(UriKind)}, null);
return new InstanceDescriptor(ci, new object[] { uri.OriginalString,
m_UriKind == UriKind.RelativeOrAbsolute ? uri.IsAbsoluteUri ? UriKind.Absolute : UriKind.Relative : m_UriKind });
}
if (uri != null && destinationType == typeof(string))
return uri.OriginalString;
if (uri != null && destinationType == typeof(Uri))
return new Uri(uri.OriginalString,
m_UriKind == UriKind.RelativeOrAbsolute ? uri.IsAbsoluteUri ? UriKind.Absolute : UriKind.Relative : m_UriKind);
return base.ConvertTo(context, culture, value, destinationType);
}
//
public override bool IsValid(ITypeDescriptorContext context, object value)
{
string str = value as string;
Uri temp;
if (str != null)
return Uri.TryCreate(str, m_UriKind, out temp);
return value is Uri;
}
}
}