![]() 前言 public class Longitude
{ private int _Degrees; private int _Minutes; private int _Seconds; private LongitudeDirection _Direction; /// <summary> /// 度数 /// </summary> public int Degrees { } /// <summary> /// 分度 /// </summary> public int Minutes { } /// <summary> /// 秒读 /// </summary> public int Seconds { } /// <summary> /// 方向 /// </summary> public LongitudeDirection Direction { } } 有了个这个类,我们怎样将其转换到string类或其他类呢(这里假设string类)例如“24W3'4”形式,也许你会说重写ToString()方法不就行了,似乎可行,但如果转换成其他类呢,又从其他类转换回来呢,怎么办。还有在复杂控件中Designer设计中又该怎么办。(在复杂控件的应用稍后介绍) class LongitudeTypeConverter : TypeConverter { ![]() }
![]() public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType) { if (sourceType == typeof(string)) return true; else return base.CanConvertFrom(context, sourceType); }
![]() public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType) { if ((destinationType == typeof(string)) | (destinationType == typeof(InstanceDescriptor))) return true; else return base.CanConvertTo(context, destinationType); }
![]() public override object ConvertTo(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, Type destinationType) { // check that the value we got passed on is of type Longitude if (value != null) if (!(value is Longitude)) throw new Exception(WrongType); // convert to a string if (destinationType == typeof(string)) { // no value so we return an empty string if (value == null) return string.Empty; // strongly typed Longitude LongValue = value as Longitude; // get the two type converters to use TypeConverter IntConverter = TypeDescriptor.GetConverter(typeof(int)); TypeConverter EnumConverter = TypeDescriptor.GetConverter(typeof(LongitudeDirection)); // convert to a string and return return IntConverter.ConvertToString(context, culture, LongValue.Degrees) + EnumConverter.ConvertToString(context, culture, LongValue.Direction).Substring(0, 1) + IntConverter.ConvertToString(context, culture, LongValue.Minutes) + MinutesUnit + IntConverter.ConvertToString(context, culture, LongValue.Seconds) + SecondsUnit; } // convert to a instance descriptor if (destinationType == typeof(InstanceDescriptor)) { // no value so we return no instance descriptor if (value == null) return null; // strongly typed Longitude LongValue = value as Longitude; // used to descripe the constructor MemberInfo Member = null; object[] Arguments = null; // get the constructor for the type Member = typeof(Longitude).GetConstructor(new Type[] { typeof(int), typeof(int), typeof(int), typeof(LongitudeDirection) }); // create the arguments to pass along Arguments = new object[] { LongValue.Degrees, LongValue.Minutes, LongValue.Seconds, LongValue.Direction }; // return the instance descriptor if (Member != null) return new InstanceDescriptor(Member, Arguments); else return null; } // call the base converter return base.ConvertTo(context, culture, value, destinationType); }
![]() public override object ConvertFrom(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) { // no value so we return a new Longitude instance if (value == null) return new Longitude(); // convert from a string if (value is string) { // get strongly typed value string StringValue = value as string; // empty string so we return a new Longitude instance if (StringValue.Length <= 0) return new Longitude(); // get the position of the West longitude separator int DirectionPos = StringValue.IndexOf(LongitudeDirection.West.ToString().Substring(0, 1)); LongitudeDirection Direction = LongitudeDirection.West; // if not found get the position of the East longitude separator if (DirectionPos == -1) { DirectionPos = StringValue.IndexOf(LongitudeDirection.East.ToString().Substring(0, 1)); Direction = LongitudeDirection.East; } // get the minutes and seconds characters int MinutesPos = StringValue.IndexOf(MinutesUnit); int SecondsPos = StringValue.IndexOf(SecondsUnit); // no minutes present if (MinutesPos == -1) throw new Exception(MinutesMissing); // no seconds present if (SecondsPos == -1) throw new Exception(SecondsMissing); // no minutes present if (DirectionPos == -1) throw new Exception(DirectionMissing); // no degrees present if (DirectionPos == 0) throw new Exception(DegreesMissing); // get the type converters we need TypeConverter IntConverter = TypeDescriptor.GetConverter(typeof(int)); // get the degrees, minutes and seconds value int Degrees = (int)IntConverter.ConvertFromString(context, culture, StringValue.Substring(0, DirectionPos)); int Minutes = (int)IntConverter.ConvertFromString(context, culture, StringValue.Substring(DirectionPos + 1, MinutesPos - DirectionPos - 1)); int Seconds = (int)IntConverter.ConvertFromString(context, culture, StringValue.Substring(MinutesPos + 1, SecondsPos - MinutesPos - 1)); // create a new Longitude instance with these values and return it return new Longitude(Degrees, Minutes, Seconds, Direction); } // otherwise call the base converter else return base.ConvertFrom(context, culture, value); }
class Test
{ public static void Main(string[] args) { //将Longitude类转换到string类型 Longitude longitude = new Longitude(10,11,12,LongitudeDirection.East); LongitudeTypeConverter converter = new LongitudeTypeConverter(); string strLongitude=""; if (converter.CanConvertTo(typeof(string))) { strLongitude = (string)converter.ConvertTo(longitude, typeof(string)); } System.Console.WriteLine(strLongitude); //将string还原回Longitude类 Longitude longitude1 = new Longitude(); if (converter.CanConvertFrom(typeof(string))) { longitude1 = (Longitude)converter.ConvertFrom(strLongitude); } System.Console.WriteLine(longitude1.Degrees); System.Console.WriteLine(longitude1.Direction); System.Console.WriteLine(longitude1.Minutes); System.Console.WriteLine(longitude1.Seconds); } } 输出结果是 |
|