|
using System;
using System.ComponentModel;
using System.ComponentModel.Design.Serialization;
using System.Windows.Forms;
using System.Reflection;
namespace XmlBindingManager
{
public class XmlBindingTypeConverter : 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).Replace("<->", "|");
string[] properties = propertyList.Split('|');
XmlBinding binding = new XmlBinding();
binding.BoundProperty = properties[0].Trim();
if(properties.Length > 1) binding.XmlProperty = properties[1].Trim();
return binding;
}
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 XmlBinding)
{
XmlBinding binding = value as XmlBinding;
if(destinationType == typeof(string))
{
return string.Format("{0} <-> {1}", binding.BoundProperty, binding.XmlProperty);
}
if(destinationType == typeof(InstanceDescriptor))
{
object[] properties = new object[2] {binding.BoundProperty, binding.XmlProperty};
Type[] types = new Type[2] {typeof(string), typeof(string)};
ConstructorInfo ci = typeof(XmlBinding).GetConstructor(types);
return new InstanceDescriptor(ci, properties, false);
}
}
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)
{
XmlBinding binding = new XmlBinding();
binding.BoundProperty = (string) propertyValues["BoundProperty"];
binding.XmlProperty = (string) propertyValues["XmlProperty"];
return binding;
}
}
}
|
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