ASP.NET控件开发(WebPart开发)之类型转换器

题外话:
WebPart也是一种ASP.NET服务器控件,因为它也是从WebControl继承的。

之前在学习ASP.NET控件开发的过程中,学到类型转换器的时候就感觉进行不下去了。
感觉类型转换比较麻烦,看代码看得我头晕。
---
现在在工作中,开发WebPart时,需要使用到一个List<string>集合来存储属性,然而在html页面中或aspx页面中展现时,却是以
字符串的形式存储。当我在SharePoint Designer 中编辑一个该webPart所在的页面后,对应的集合属性值就无法保存到List<string>,
也就是缺少一种string<->List<string>的转换。导致该webpart无法正常展现。
解决方法就是使用类型转换器进行两者之间的转换。
----
具体对于类型转换器的介绍就不再说明,直接上代码:
  1 [TypeConverter(typeof(SwapPictureListConverter))]
  2    public sealed class SwapPictureList<T>:IEnumerable
  3    {
  4        private List<T> container = new List<T>();
  5        public void Add(T value)
  6        {
  7            container.Add(value);
  8        }

  9        public int Count
 10        {
 11            get
 12            {
 13                return container.Count;
 14            }

 15        }

 16        public void Clear()
 17        {
 18            container.Clear();
 19        }

 20        public IEnumerator GetEnumerator()
 21        {
 22            return container.GetEnumerator();
 23        }

 24    }

 25    public class SwapPictureListConverter : TypeConverter
 26    {
 27        public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
 28        {
 29            if (sourceType == typeof(string))
 30                return true;
 31            return base.CanConvertFrom(context, sourceType);
 32        }

 33        public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
 34        {
 35            if (destinationType == typeof(string))
 36                return true;
 37            return base.CanConvertTo(context, destinationType);
 38        }

 39        public override object ConvertTo(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, Type destinationType)
 40        {
 41            if (value != null)
 42            {
 43                if (!(value is SwapPictureList<string>))
 44                {
 45                    throw new ArgumentException(
 46                        "Invalid SwapPictureList""value");
 47                }

 48            }

 49
 50            if (destinationType == typeof(string))
 51            {
 52                if (value == null)
 53                {
 54                    return String.Empty;
 55                }

 56                TypeConverter stringConverter = TypeDescriptor.GetConverter(typeof(string));
 57                SwapPictureList<string> tempList = (SwapPictureList<string>)value;
 58                if (tempList.Count <= 0)
 59                    return string.Empty;
 60
 61                string[] tempStrs = new string[tempList.Count];
 62                int i = 0;
 63                foreach (string tempStr in tempList)
 64                {
 65                    tempStrs[i] = stringConverter.ConvertToString(context, culture, tempStr);
 66                    i++;
 67                }

 68
 69                return String.Join(culture.TextInfo.ListSeparator, tempStrs);
 70            }

 71
 72
 73            return base.ConvertTo(context, culture, value, destinationType);
 74        }

 75        public override object ConvertFrom(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value)
 76        {
 77            if (value == null)
 78            {
 79                return new SwapPictureList<string>();
 80            }

 81
 82            if (value is string)
 83            {
 84                string s = (string)value;
 85                if (s.Length == 0)
 86                {
 87                    return new SwapPictureList<string>();
 88                }

 89
 90                string[] parts = s.Split(culture.TextInfo.ListSeparator[0]);
 91
 92                if (parts.Length <= 0)
 93                {
 94                    throw new ArgumentException("Invalid SwapPictureList""value");
 95                }

 96                //返回指定类型转换器
 97                TypeConverter stringConverter = TypeDescriptor.GetConverter(typeof(string));
 98                SwapPictureList<string> tempList = new SwapPictureList<string>();
 99                for (int i = 0; i < parts.Length; i++)
100                {
101                    tempList.Add((string)stringConverter.ConvertFromString(context, culture, parts[i]));
102                }

103                return tempList;
104            }

105            return base.ConvertFrom(context, culture, value);
106        }

通过SwapPictureListConverter就可以在SwapPictureList和String之间进行转换了。
这个类型转换是属于值(类型)转换的一种。
而类型转换器还有属性转换等等。
-----
以上代码记录在此,仅作备忘。
原文地址:https://www.cnblogs.com/McJeremy/p/1371248.html