|
using System;
using System.ComponentModel;
using System.Globalization;
using Cubox.Model;
namespace Cubox.Model
{
internal class LocationConverter : ExpandableObjectConverter
{
/// <summary>
///
/// </summary>
/// <param name="context"></param>
/// <param name="sourceType"></param>
/// <returns></returns>
public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
{
if (sourceType == typeof(string))
{
return true;
}
return base.CanConvertFrom (context, sourceType);
}
/// <summary>
///
/// </summary>
/// <param name="context"></param>
/// <param name="culture"></param>
/// <param name="value"></param>
/// <returns></returns>
public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
{
if (value is string)
{
try
{
char c = char.Parse(";");
string[] s = (value as string).Split(c);
Location loc = new Location();
loc.Latitude = double.Parse(s[0]);
loc.Longitude = double.Parse(s[1]);
return loc;
}
catch
{
throw new ArgumentException("Cannot convert from '" + (string)value + "' to type Location");
}
}
return base.ConvertFrom (context, culture, value);
}
/// <summary>
///
/// </summary>
/// <param name="context"></param>
/// <param name="culture"></param>
/// <param name="value"></param>
/// <param name="destinationType"></param>
/// <returns></returns>
public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
{
if (destinationType == typeof(string) && value is Location)
{
Location loc = value as Location;
return (loc.Latitude + "; " + loc.Longitude);
}
return base.ConvertTo (context, culture, value, destinationType);
}
}
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.