首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> .NET 網頁程式設計 >> ASP.NET 基礎 >> 關於ASP.NET

ASP.NET ViewState 自定義狀態管理(二):TypeConverter 詳解與實戰

     編輯:關於ASP.NET

上一篇介紹了自定義屬性類別中重寫 ToString 與建構函數來實現狀態管理,但為何不直接使用 TypeConverter?本文將詳細闡述 TypeConverter 的作用、核心方法與實際應用,並提供完整代碼範例。

一、TypeConverter 是什麼?

根據 MSDN 定義:提供一種將值的類型轉換為其他類型、訪問標準值及子屬性的統一方法。在屬性視窗或 ASPX/ASCX 標記中為控件屬性賦值時,TypeConverter 負責將字串輸入轉換為目標類型實例。

二、核心方法解析

繼承 ExpandableObjectConverter 並重寫以下四個關鍵方法:

  • CanConvertFrom:指定是否支援從特定類型(如 string)轉換。
  • CanConvertTo:指定是否可轉換為特定類型(如 string)。
  • ConvertFrom:實際執行從字串到物件的轉換邏輯(如按 ';' 分割屬性值)。
  • ConvertTo:將物件轉換為字串表示(如 "Property1;Property2")。

三、完整代碼範例

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;
      }
    }

    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) {
      writer.Write(TypeDescriptor.GetConverter(Property.GetType())
        .ConvertTo(null, CultureInfo.InvariantCulture, Property, typeof(string)));
    }
  }

  [TypeConverter(typeof(CustomTypeConverter))]
  public class CustomProperty4 {
    private string m_Property1, m_Property2;
    public string Property1 { get { return m_Property1; } set { m_Property1 = value; } }
    public string Property2 { get { return m_Property2; } set { m_Property2 = value; } }
  }

  public class CustomTypeConverter : ExpandableObjectConverter {
    public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType) {
      return sourceType == typeof(string) || base.CanConvertFrom(context, sourceType);
    }
    public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType) {
      return destinationType == typeof(string) || base.CanConvertTo(context, destinationType);
    }
    public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value) {
      if (value is string) {
        string[] parts = ((string)value).Split(';');
        if (parts.Length == 2) {
          var obj = new CustomProperty4();
          obj.Property1 = parts[0];
          obj.Property2 = parts[1];
          return obj;
        }
      }
      return base.ConvertFrom(context, culture, value);
    }
    public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType) {
      if (value is CustomProperty4 && destinationType == typeof(string)) {
        var obj = (CustomProperty4)value;
        return obj.Property1 + ";" + obj.Property2;
      }
      return base.ConvertTo(context, culture, value, destinationType);
    }
  }
}

四、最佳實踐建議

  1. 優先使用 TypeConverter 處理複雜類型的序列化/反序列化,避免手工字串拼接。
  2. 配合 [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)] 讓設計器正確持久化子屬性。
  3. 在 Render 階段透過 TypeDescriptor.GetConverter 統一輸出,保持一致性。

五、總結

TypeConverter 不僅簡化了 ViewState 中自定義類型的存取,還能無縫整合至設計時環境,是 ASP.NET 控件開發中不可或缺的基礎設施。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved