// This is our TypeConverter class. Notice that we are deriving
// from EnumConverter
public class TaskCodeConverter : EnumConverter
{
// Here we are defining the values that the API expect.
// Note: public read-only constants are still a great way
// of exposing a strict set of valid values. I just don’t
// want to expose them to users of my webservices or
// my businesslogic layer.
#region constants
public const string CSRVN="CSRVN";
public const string CSRVY="CSRVY";
public const string INSTN="INSTN";
public const string INSTY="INSTY";
#endregion constants
#region ctors
// The default constructor (define the enum this class supports).
public TaskCodeConverter ():base(typeof(eTaskCode)){}
#endregion ctors
#region public methods
// This OVERRIDDEN method handles converting “From” a specific
// type. We only care about string values, so we are only handling
// string types. Any other type is passed up to the base type
// EnumConverter.
// This method is all about converting TO our Enumeration
// eTaskCode. Nothing else should ever be returned.
public override object ConvertFrom(ITypeDescriptorContext
context,system.Globalization.CultureInfo culture,
object value)
{
// remember we only care about strings…………
if(value as string != null)
{
// it’s a good idea to clean up the string…………
string localTask=((string)value).Trim().ToUpper();
// If they pass us nothing, then throw an exception.
if(localTask.Length == 0)
{
throw new ArgumentNullException("taskCode");
}
else
{
// compare what we got with out constants defined above.
// only handle defined values.
switch(localTask)
{
case CSRVN:
return eTaskCode.ChangeOfServiceSendTechnician;
case CSRVY:
return eTaskCode.ChangeOfServiceNoTechnician;
case INSTN:
return eTaskCode.InstallServiceSendTechnician;
case INSTY:
return eTaskCode.InstallServiceNoTechnician;
default:
throw new ArgumentException(string.Format("Invalid
taskCode value: {0}",localTask));
}
}
}
else
{
// all other types get passed up to the base class EnumConverter.
return base.ConvertFrom(context,culture,value);
}
}
// This OVERRIDDEN method handles converting “To” a specific
// type. We only care about string values, so we are only handling
// string types. Any other type is passed up to the base type
// EnumConverter.
// This method is all about converting FROM our Enumeration eTaskCode
// to a value of a specified type. Since we want to return a value
// that the API understands then that is what we return.
public override object ConvertTo(ITypeDescriptorContext context,
System.Globalization.CultureInfo culture, object value,
Type destinationType)
{
// check if we are returning a string.
if(destinationType==typeof(string))
{
switch((eTaskCode)value)
{
case eTaskCode.ChangeOfServiceSendTechnician:
return CSRVN;
case eTaskCode.ChangeOfServiceNoTechnician:
return CSRVY;
case eTaskCode.InstallServiceSendTechnician:
return INSTN;
default:
return INSTY;
}
}
else
{
return base.ConvertTo(context,culture,value,destinationType);
}
}
#endregion public methods
} |