-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathFileSystemPathConverter.cs
More file actions
88 lines (79 loc) · 4.42 KB
/
FileSystemPathConverter.cs
File metadata and controls
88 lines (79 loc) · 4.42 KB
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
using System.ComponentModel;
using System.Globalization;
using System.IO;
namespace RayCarrot.RCP.Metro;
/// <summary>
/// Converts a <see cref="FileSystemPath"/> to other values
/// </summary>
public class FileSystemPathConverter : TypeConverter
{
/// <summary>
/// Returns whether this converter can convert an object of the given type to the type of this converter, using the specified context.
/// </summary>
/// <param name="context">An <see cref="ITypeDescriptorContext"/> that provides a format context.</param>
/// <param name="sourceType">A <see cref="Type"/> that represents the type you want to convert from.</param>
/// <returns>True if this converter can perform the conversion; otherwise, false.</returns>
public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
{
if (sourceType == typeof(string) || sourceType == typeof(FileSystemInfo) || sourceType == typeof(FileSystemPath))
return true;
else
return base.CanConvertFrom(context, sourceType);
}
/// <summary>
/// Returns whether this converter can convert the object to the specified type, using the specified context.
/// </summary>
/// <param name="context">An <see cref="ITypeDescriptorContext"/> that provides a format context.</param>
/// <param name="destinationType">A <see cref="Type"/> that represents the type you want to convert to.</param>
/// <returns>True if this converter can perform the conversion; otherwise, false.</returns>
public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
{
if (destinationType == typeof(string) || destinationType == typeof(FileSystemInfo) || destinationType == typeof(FileSystemPath))
return true;
else
return base.CanConvertTo(context, destinationType);
}
/// <summary>
/// Converts the given object to the type of this converter, using the specified context and culture information.
/// </summary>
/// <param name="context">An <see cref="ITypeDescriptorContext"/> that provides a format context.</param>
/// <param name="culture">The <see cref="CultureInfo"/> to use as the current culture.</param>
/// <param name="value">The <see cref="Object"/> to convert.</param>
/// <returns>An <see cref="Object"/> that represents the converted value.</returns>
/// <exception cref="NotSupportedException">The conversion cannot be performed</exception>
public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
{
if (value is string stringValue)
return new FileSystemPath(stringValue);
else if (value is FileSystemInfo fileSystemInfoValue)
return new FileSystemPath(fileSystemInfoValue.FullName);
else if (value is FileSystemPath pathValue)
return pathValue;
else if (value == null)
return FileSystemPath.EmptyPath;
else
return base.ConvertFrom(context, culture, value);
}
/// <summary>
/// Converts the given value object to the specified type, using the specified context and culture information.
/// </summary>
/// <param name="context">An <see cref="ITypeDescriptorContext"/> that provides a format context.</param>
/// <param name="culture">A <see cref="CultureInfo"/>. If null is passed, the current culture is assumed.</param>
/// <param name="value">The <see cref="Object"/> to convert.</param>
/// <param name="destinationType">The <see cref="Type"/> to convert the value parameter to.</param>
/// <returns>An <see cref="Object"/> that represents the converted value.</returns>
/// <exception cref="ArgumentNullException">The destinationType parameter is null.</exception>
/// <exception cref="NotSupportedException">The conversion cannot be performed.</exception>
public override object? ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
{
if (!(value is FileSystemPath path))
return base.ConvertTo(context, culture, value, destinationType);
if (destinationType == typeof(string))
return path.FullPath;
if (destinationType == typeof(FileSystemInfo))
return path.GetFileSystemInfo();
if (destinationType == typeof(FileSystemPath))
return value;
return base.ConvertTo(context, culture, value, destinationType);
}
}