Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
UriTypeConverter.cs
Go to the documentation of this file.
5
6namespace System;
7
9{
10 public override bool CanConvertFrom(ITypeDescriptorContext? context, Type sourceType)
11 {
12 if (!(sourceType == typeof(string)) && !(sourceType == typeof(Uri)))
13 {
14 return base.CanConvertFrom(context, sourceType);
15 }
16 return true;
17 }
18
19 public override bool CanConvertTo(ITypeDescriptorContext? context, Type? destinationType)
20 {
21 if (!(destinationType == typeof(Uri)) && !(destinationType == typeof(InstanceDescriptor)))
22 {
23 return base.CanConvertTo(context, destinationType);
24 }
25 return true;
26 }
27
28 public override object? ConvertFrom(ITypeDescriptorContext? context, CultureInfo? culture, object? value)
29 {
30 if (value is string text)
31 {
32 if (string.IsNullOrEmpty(text))
33 {
34 return null;
35 }
36 return new Uri(text, UriKind.RelativeOrAbsolute);
37 }
38 if (value is Uri uri)
39 {
40 return new Uri(uri.OriginalString, GetUriKind(uri));
41 }
43 }
44
45 public override object ConvertTo(ITypeDescriptorContext? context, CultureInfo? culture, object? value, Type destinationType)
46 {
47 if (destinationType == null)
48 {
49 throw new ArgumentNullException("destinationType");
50 }
51 if (value is Uri uri)
52 {
53 if (destinationType == typeof(InstanceDescriptor))
54 {
55 ConstructorInfo constructor = typeof(Uri).GetConstructor(BindingFlags.Instance | BindingFlags.Public, null, new Type[2]
56 {
57 typeof(string),
58 typeof(UriKind)
59 }, null);
60 return new InstanceDescriptor(constructor, new object[2]
61 {
62 uri.OriginalString,
63 GetUriKind(uri)
64 });
65 }
66 if (destinationType == typeof(string))
67 {
68 return uri.OriginalString;
69 }
70 if (destinationType == typeof(Uri))
71 {
72 return new Uri(uri.OriginalString, GetUriKind(uri));
73 }
74 }
75 throw GetConvertToException(value, destinationType);
76 }
77
78 public override bool IsValid(ITypeDescriptorContext? context, object? value)
79 {
80 if (value is string uriString)
81 {
82 Uri result;
83 return Uri.TryCreate(uriString, UriKind.RelativeOrAbsolute, out result);
84 }
85 return value is Uri;
86 }
87
88 private static UriKind GetUriKind(Uri uri)
89 {
90 if (!uri.IsAbsoluteUri)
91 {
92 return UriKind.Relative;
93 }
94 return UriKind.Absolute;
95 }
96}
Exception GetConvertToException(object? value, Type destinationType)
Exception GetConvertFromException(object? value)
override bool CanConvertTo(ITypeDescriptorContext? context, Type? destinationType)
override bool CanConvertFrom(ITypeDescriptorContext? context, Type sourceType)
override bool IsValid(ITypeDescriptorContext? context, object? value)
static UriKind GetUriKind(Uri uri)
override object ConvertTo(ITypeDescriptorContext? context, CultureInfo? culture, object? value, Type destinationType)
override? object ConvertFrom(ITypeDescriptorContext? context, CultureInfo? culture, object? value)
static bool TryCreate([NotNullWhen(true)] string? uriString, UriKind uriKind, [NotNullWhen(true)] out Uri? result)
Definition Uri.cs:3793
bool IsAbsoluteUri
Definition Uri.cs:572
UriKind
Definition UriKind.cs:4