-
Notifications
You must be signed in to change notification settings - Fork 0
/
DirAttribute.cs
85 lines (69 loc) · 2.42 KB
/
DirAttribute.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
using System;
using System.Collections.Generic;
using System.DirectoryServices.Protocols;
using System.Globalization;
using System.Linq;
namespace DirLink
{
public class DirAttribute
{
private readonly DirectoryAttribute _attribute;
internal DirAttribute(DirectoryAttribute attribute)
{
_attribute = attribute;
}
public static explicit operator int(DirAttribute attribute)
{
return int.Parse((string) attribute);
}
public static explicit operator long(DirAttribute attribute)
{
return long.Parse((string) attribute);
}
public static explicit operator bool(DirAttribute attribute)
{
return bool.Parse((string) attribute);
}
public static explicit operator byte[](DirAttribute attribute)
{
byte[] bytes = attribute._attribute
.GetValues(typeof (byte[]))
.Cast<byte[]>()
.FirstOrDefault();
return bytes;
}
public static explicit operator Guid(DirAttribute attribute)
{
var bytes = (byte[]) attribute;
return bytes == null ? Guid.Empty : new Guid(bytes);
}
public static explicit operator DateTimeOffset(DirAttribute attribute)
{
return DateTimeOffset.ParseExact((string) attribute, "yyyyMMddHHmmmss.fK", CultureInfo.InvariantCulture);
}
public static explicit operator TimeSpan(DirAttribute attribute)
{
return TimeSpan.FromTicks((long) attribute);
}
public static explicit operator string(DirAttribute attribute)
{
return GetStrings(attribute).FirstOrDefault();
}
public override string ToString()
{
return string.Join("; ", GetStrings(this));
}
public static explicit operator List<string>(DirAttribute attribute)
{
return GetStrings(attribute).ToList();
}
public static explicit operator string[](DirAttribute attribute)
{
return GetStrings(attribute).ToArray();
}
private static IEnumerable<string> GetStrings(DirAttribute attribute)
{
return attribute._attribute.GetValues(typeof (string)).Cast<string>();
}
}
}