-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathURL.cs
44 lines (37 loc) · 1.11 KB
/
URL.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
namespace HastyAPI {
/// <summary>
/// A more practical version of Uri.
/// </summary>
[TypeConverter(typeof(URLConverter))]
public class URL {
string _value;
public URL(string value) {
_value = value;
}
// convert from URL to string
public static implicit operator string(URL value) {
return value._value;
}
// convert from string to URL
public static implicit operator URL(String value) {
if(!Uri.IsWellFormedUriString(value, UriKind.Absolute)) {
throw new Exception("That doesn't look like a URL");
}
return new URL(value);
}
public static string operator +(URL basePath, string relPath) {
return basePath._value.TrimEnd('/') + '/' + relPath.TrimStart('/');
}
public class URLConverter : TypeConverter {
public override object ConvertFrom(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) {
if(value is string) return new URL((string)value);
return base.ConvertFrom(context, culture, value);
}
}
}
}