Welcome 微信登录

首页 / 网页编程 / ASP.NET / Asp.net ViewState——自定义状态管理(二)

Asp.net ViewState——自定义状态管理(二)2011-12-30 博客园 MythYsJh在上一篇ViewState——自定义状态管理(一)中我在自定义属性的类里面加入了一个重写的ToString方法和一个从字符串获取一个该类型实例的一个构造函数。大家可能会觉得谈到自定义属性的状态管理却不提及TypeConverter(Attribute),有些神奇。好吧,下面就来说说TypeConverter。

先看下MSDN的解释:提供一种将值的类型转换为其他类型以及访问标准值和子属性的统一方法。

在上一篇我自己写了两个方法而没有使用TypeConverter就是想从一方面说明TypeConverter的作用(从这一点来说就是类型转换)。当然TypeConverter还有很多其他用途,比方说提供一组标准值什么的。因此在通常情况下我们都使用TypeConverter来完成这些工作(即使仅仅是只处理类型转换的问题)。就个人理解,TypeConverter是我们在属性窗体为控件属性输入值以及在aspx或ascx声明控件给控件属性赋值时使用的,它可以控制怎样将我们的输入转换成控件属性对应的类型。您可能会说,其实我们自己也可以调用它来做些操作啊,对,这样也可以。

首先,修改下以前的例子:

ViewStateManagement4

namespace Controls{  using System;  using System.Collections.Generic;  using System.Text;  using System.Web.UI;  using System.ComponentModel;  using System.Globalization;    public class ViewStateManagement4 : Control  {    private CustomProperty4 m_Property;    [DesignerSerializationVisibility( DesignerSerializationVisibility.Content )]    public CustomProperty4 Property    {      get      {        if (m_Property == null)        {          m_Property = new CustomProperty4();        }          return m_Property;      }      //set      //{      //}    }    protected override void LoadViewState(object savedState)    {      if (savedState is Pair)      {        Pair properties = (Pair)savedState;        base.LoadViewState(properties.First);        m_Property = (CustomProperty4)properties.Second;      }      else      {        base.LoadViewState(savedState);      }    }    protected override object SaveViewState()    {      object baseState = base.SaveViewState();      if (Property != null)      {        return new Pair( baseState, Property);      }      return baseState;    }    protected override void TrackViewState()    {      base.TrackViewState();    }      protected override void Render(HtmlTextWriter writer)    {      //CustomTypeConverter c = new CustomTypeConverter();      //c.ConvertTo(null, CultureInfo.InvariantCulture, Property, typeof(string));      //writer.Write(c.ConvertTo(null, CultureInfo.InvariantCulture, Property, typeof(string)));        writer.Write(TypeDescriptor.GetConverter(Property.GetType()).ConvertTo(null, CultureInfo.InvariantCulture, Property, typeof(string)));    }  }  [TypeConverter(typeof( CustomTypeConverter))]  public class CustomProperty4  {    private string m_Property1;    private string m_Property2;      public string Property2    {      get { return m_Property2; }      set { m_Property2 = value; }    }    public string Property1    {      get { return m_Property1; }      set { m_Property1 = value; }    }    }  public class CustomTypeConverter : ExpandableObjectConverter  {    /**//// <summary>    /// 指定是否可以从特定的类型进行转换    /// </summary>    /// <param name="context"></param>    /// <param name="sourceType"></param>    /// <returns>true:可以;false:不可以</returns>    public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)    {      //如果源类型是字符串      if (sourceType == typeof(string))      {        return true;      }        return false;    }    /**//// <summary>    /// 指定是否可以转换为特定类型    /// </summary>    /// <param name="context"></param>    /// <param name="destinationType"></param>    /// <returns></returns>    public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)    {      if (destinationType == typeof(string))      {        return true;      }        return false;    }    /**//// <summary>    /// 从特定值进行转换    /// </summary>    /// <param name="context"></param>    /// <param name="culture"></param>    /// <param name="value"></param>    /// <returns></returns>    public override object ConvertFrom(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value)    {      if (value is string)      {        string str = (string)value;        string[] propertyValues = str.Split(";");        if (propertyValues.Length == 2)        {          CustomProperty4 obj = new CustomProperty4();          obj.Property1 = propertyValues[0];          obj.Property2 = propertyValues[1];          return obj;        }      }      return base.ConvertFrom(context, culture, value);    }    /**//// <summary>    /// 转换成特定值    /// </summary>    /// <param name="context"></param>    /// <param name="culture"></param>    /// <param name="value"></param>    /// <param name="destinationType"></param>    /// <returns></returns>    public override object ConvertTo(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, Type destinationType)    {      if (value is CustomProperty4)      {        CustomProperty4 obj = (CustomProperty4)value;        return obj.Property1 + ";" + obj.Property2;      }      return base.ConvertTo(context, culture, value, destinationType);    }  }}