国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > 綜合 > 正文

組件編程之TypeConverterAttribute

2024-07-21 02:29:23
字體:
來源:轉載
供稿:網友
  今天的這篇文章,我主要是帶來propertyattribute里的typeconverterattribute的講解,首先在這里講講typeconverterattribute的作用是什么:當component的某個property被設置時,如size="60,70",解析器會通過類型轉化器,把這個字符串自動轉換為屬性聲明的類型。.net的框架中已經聲明了很多的類型轉化器,下面的代碼中有列舉到。有點類似于operator。

  同時在asp.net服務器控件的編寫中typeconverterattribute也將會非常有用,服務器控件的property只能以string形式保存在aspx頁面里,而在服務器控件的designtime和runtime時必須要把string轉換為相應的類型。

  源代碼如下:

using system;
using system.collections.generic;
using system.text;
using system.componentmodel;
using system.globalization;
namespace classlibrary1
{
 public class class1 : component
 {
  private size _size;

  public class1()
  {
   _size = new size();
  }

  [designerserializationvisibility(designerserializationvisibility.content)]
  [typeconverter(typeof(sizeconverter))] // —— 注1,也可以把這句typeconverterattribute寫在注2處。

  public size size
  {
   get { return _size; }
   set { _size = value; }
  }
 }

 public class sizeconverter : typeconverter // 我們自定義的converter必須繼承于typeconverter基類。
 {
  /**//// <summary>
  /// 是否能用string轉換到size類型。
  /// </summary>
  /// <param name="context">上下文。</param>
  /// <param name="sourcetype">轉換源的type。</param>
  /// <returns></returns>
  public override bool canconvertfrom(itypedescriptorcontext context, type sourcetype)
  {
   if (sourcetype == typeof(string))
    { return true; }
   else
    { return false; }
  }

  /**//// <summary>
  /// 從string轉到size類型。
  /// </summary>
  /// <param name="context">提供component的上下文,如component.instance對象等。</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 == null || value.tostring().length == 0) return new size();
   char spliter = culture.textinfo.listseparator[0]; // 得到字符串的分隔符
   string[] ss = ((string)value).split(spliter);

   int32converter intconverter = new int32converter(); // 得到類型轉換器,.net中為我們定義了一些常見的類型轉換器。
   return new size((int32)intconverter.convertfromstring(context, culture, ss[0]),
(int32)intconverter.convertfromstring(context, culture, ss[1]));
  }

  /**//// <summary>
  /// 是否能用size轉換到string類型。
  /// </summary>
  /// <param name="context"></param>
  /// <param name="destinationtype">轉換目標的類型。</param>
  /// <returns></returns>
  public override bool canconvertto(itypedescriptorcontext context, type destinationtype)
  {
   if (destinationtype == typeof(size)) // 如果是size格式,則允許轉成string。
    { return true; }
   else
    { return false; }
  }

  // 在property窗口中顯示為string類型。
  public override object convertto(itypedescriptorcontext context, system.globalization.cultureinfo culture, object value, type destinationtype)
  {
   if (value == null) return string.empty;
   if (destinationtype == typeof(string))
   {
    size size = (size)value;
    typeconverter intconverter = typedescriptor.getconverter(typeof(int32)); // 能到類型轉換器的另一種方式。
    char spliter = culture.textinfo.listseparator[0]; // 得到字符串的分隔符

    return string.join(spliter.tostring(), new string[]{
    intconverter.converttostring(context, culture, size.length),
    intconverter.converttostring(context, culture, size.width)});
   }
   return string.empty;
  }

  // typeconverter還有幾個虛方法,請大家自己研究。
 }

 // [typeconverter(typeof(sizeconverter))] —— 注2
 public class size
 {
  private int32 _length;
  private int32 _width;

  public size(int32 length, int32 width)
  {
   _length = length;
   _width = width;
  }

  public size() : this(0, 0)
  {}

  public int32 length
  {
   get { return _length; }
   set { _length = value; }
  }

  public int32 width
  {
   get { return _width; }
   set { _width = value; }
  }
 }
}



發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 安顺市| 彩票| 沙湾县| 洱源县| 康马县| 甘肃省| 海晏县| 松阳县| 玉田县| 和林格尔县| 湖州市| 长乐市| 子洲县| 遂川县| 岑巩县| 鄂伦春自治旗| 兰考县| 泸溪县| 桐乡市| 普兰店市| 禹城市| 东安县| 黄山市| 靖西县| 新余市| 张家川| 盖州市| 铁岭县| 太谷县| 双江| 南宫市| 巴中市| 祁连县| 大理市| 资中县| 长垣县| 澄江县| 阿拉善左旗| 马关县| 共和县| 迁西县|