|
using System;
using System.ComponentModel;
using System.ComponentModel.Design.Serialization;
using System.Windows.Forms;
using System.Reflection;
namespace XmlBindingManager
{
public class TagSettingTypeConverter : ExpandableObjectConverter
{
public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
{
if(sourceType == typeof(string)) return true;
return base.CanConvertFrom (context, sourceType);
}
public override object ConvertFrom(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value)
{
if(value is string)
{
try
{
string propertyList = (string) value;
string[] properties = propertyList.Split(';');
TagSetting setting = new TagSetting();
setting.XmlTag = properties[0].Trim();
setting.KeyAttribute = properties[1].Trim();
return setting;
}
catch {}
throw new ArgumentException("arguments not valid");
}
return base.ConvertFrom (context, culture, value);
}
public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
{
if(destinationType == typeof(InstanceDescriptor)) return true;
return base.CanConvertTo (context, destinationType);
}
public override object ConvertTo(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, Type destinationType)
{
if(value is TagSetting)
{
TagSetting setting = value as TagSetting;
if(destinationType == typeof(string))
{
return string.Format("{0}; {1}", setting.XmlTag, setting.KeyAttribute);
}
if(destinationType == typeof(InstanceDescriptor))
{
object[] properties = new object[2] {setting.XmlTag, setting.KeyAttribute};
Type[] types = new Type[2] {typeof(string), typeof(string)};
ConstructorInfo ci = typeof(TagSetting).GetConstructor(types);
return new InstanceDescriptor(ci, properties);
}
}
return base.ConvertTo (context, culture, value, destinationType);
}
public override bool GetCreateInstanceSupported(ITypeDescriptorContext context)
{
return true;
}
public override object CreateInstance(ITypeDescriptorContext context, System.Collections.IDictionary propertyValues)
{
TagSetting setting = new TagSetting();
setting.XmlTag = (string) propertyValues["XmlTag"];
setting.KeyAttribute = (string) propertyValues["KeyAttribute"];
return setting;
}
}
}
|
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.
This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.
A list of licenses authors might use can be found here