// (c) Copyright Microsoft Corporation.
// This source is subject to the Microsoft Public License (Ms-PL).
// Please see http://go.microsoft.com/fwlink/?LinkID=131993 for details.
// All other rights reserved.
using System;
using System.ComponentModel;
using System.IO;
namespace Microsoft.Test.CommandLineParsing
{
///
/// Converter that can convert from a string to a FileInfo.
///
public class FileInfoConverter : TypeConverter
{
///
/// Converts from a string to a FileInfo.
///
/// Context.
/// Culture.
/// Value to convert.
/// FileInfo, or null if value was null or non-string.
public override object ConvertFrom(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value)
{
if (value is string && value != null)
{
return new FileInfo((string)value);
}
else
{
return null;
}
}
///
/// Returns whether this converter can convert an object of the given type to a FileInfo.
///
/// An ITypeDescriptorContext that provides a format context.
/// A Type that represents the type you want to convert from.
/// True if this converter can perform the conversion; otherwise, False.
public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
{
return (sourceType == typeof(string));
}
///
/// Converts from a FileInfo to a string.
///
public override object ConvertTo(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, Type destinationType)
{
if (value is FileInfo && destinationType == typeof(string))
{
return ((FileInfo)value).FullName;
}
else
{
return null;
}
}
///
/// Returns whether this converter can convert a FileInfo object to the specified type.
///
public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
{
return (destinationType == typeof(string));
}
}
}