|
using System;
using System.ComponentModel;
using System.Globalization;
namespace PythonExpressions
{
/// <summary>
/// The return Type that should be used for the
/// IronPython script that is run to evaluate the
/// code script
/// </summary>
public enum ReturnResultType { String, Double }
/// <summary>
/// Allows the XAML to use the ReturnResultType enum.
/// Basically you need a TypeConverter to allow the XamlParser
/// to know what to do with Enum string value
/// </summary>
public class ReturnResultTypeConverter : TypeConverter
{
#region ConvertTo
/// <summary>
/// True if value can convert destinationType is String
/// </summary>
public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
{
if (destinationType.Equals(typeof(string)))
{
return true;
}
else
{
return base.CanConvertTo(context, destinationType);
}
}
/// <summary>
/// Convert to ReturnResultType enum value to a String
/// </summary>
public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture,
object value, Type destinationType)
{
if (destinationType.Equals(typeof(String)))
{
return value.ToString();
}
else
{
return base.ConvertTo(context, culture, value, destinationType);
}
}
#endregion
#region ConvertFrom
/// <summary>
/// True if value can convert sourceType is String
/// </summary>
public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
{
if (sourceType.Equals(typeof(string)))
{
return true;
}
else
{
return base.CanConvertFrom(context, sourceType);
}
}
/// <summary>
/// Convert from a String to a ReturnResultType enum value
/// </summary>
public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
{
if (value.GetType().Equals(typeof(String)))
{
try
{
return (ReturnResultType)Enum.Parse(typeof(ReturnResultType), value.ToString(), true);
}
catch
{
throw new InvalidCastException(
String.Format("Could not ConvertFrom value {0} into ReturnResultType enum value",
value.ToString()));
}
}
else
{
return base.ConvertFrom(context, culture, value);
}
}
#endregion
}
}
|
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.
I currently hold the following qualifications (amongst others, I also studied Music Technology and Electronics, for my sins)
- MSc (Passed with distinctions), in Information Technology for E-Commerce
- BSc Hons (1st class) in Computer Science & Artificial Intelligence
Both of these at Sussex University UK.
Award(s)
I am lucky enough to have won a few awards for Zany Crazy code articles over the years
- Microsoft C# MVP 2016
- Codeproject MVP 2016
- Microsoft C# MVP 2015
- Codeproject MVP 2015
- Microsoft C# MVP 2014
- Codeproject MVP 2014
- Microsoft C# MVP 2013
- Codeproject MVP 2013
- Microsoft C# MVP 2012
- Codeproject MVP 2012
- Microsoft C# MVP 2011
- Codeproject MVP 2011
- Microsoft C# MVP 2010
- Codeproject MVP 2010
- Microsoft C# MVP 2009
- Codeproject MVP 2009
- Microsoft C# MVP 2008
- Codeproject MVP 2008
- And numerous codeproject awards which you can see over at my blog