All articles| All Pictures| All Softwares| All Video| Go home page| Write articles| Upload pictures

Reading number is top 10 articles
C#网络应用编程基础练习题与答案(一)_[Asp.Net教程]
PHP入门:什么是PHP-网站开发为什么选择PHP-_[PHP教程]
简述.net编程中容易出现的错误认识_[Asp.Net教程]
.net根据模板生成Word文件_[Asp.Net教程]
增加网站流量方法之ASP.NET弹出窗口技术_.net资料_编程技术
SQL Server中存储过程的安全问题_[SQL Server教程]
ASP.NET教程:AccessDataSource的使用_[Asp.Net教程]
ASP.NET常用JS脚本整理_[Asp.Net教程]
使用.NET自带的功能制作简单的注册码_.net资料_编程技术
.NET平台下WEB应用程序的部署的例子_.net资料_编程技术
Reading number is top 10 pictures
刘亦菲写真集1
西游四格漫画(三)
Desktop Wallpapers1
运动的范冰冰2
Beauty ZhiHuiLin2
Sora aoi mirror memorial classics3
修电脑也有这么漂亮的美女
NeedWallpaper5
Very beautiful interior decoration
The money of more than 100 countries and regions13
Download software ranking
linux安装大全
Sora aoi, the maid, students' uniforms
Professional killers2 for Android
Unix video tutorial18
网络管理员第三版
少妇苏霞全本
VC++6.0培训教程
Macromedia Dreamweaver 8
Sora aoi 120 minutes
仙剑奇侠传98版歌曲
delv published in(发表于) 2014/1/6 9:14:58 Edit(编辑)
组件编程之TypeConverterAttribute_[Asp.Net教程]

组件编程之TypeConverterAttribute_[Asp.Net教程]

组件编程之TypeConverterAttribute_[Asp.Net教程]

今天的这篇文章,我主要是带来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基类。
 {
  /**////
  /// 是否能用string转换到Size类型。
  ///

  /// 上下文。
  /// 转换源的Type。
  ///
  public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
  {
   if (sourceType == typeof(string))
    { return true; }
   else
    { return false; }
  }

  /**////
  /// 从string转到Size类型。
  ///

  /// 提供Component的上下文,如Component.Instance对象等。
  /// 提供区域信息,如语言、时间格式、货币格式等
  ///
  ///
  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]));
  }

  /**////
  /// 是否能用Size转换到string类型。
  ///

  ///
  /// 转换目标的类型。
  ///
  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; }
  }
 }
}





作者:mapserver 来源:博客园





添加到del.icio.us 添加到新浪ViVi 添加到百度搜藏 添加到POCO网摘 添加到天天网摘365Key 添加到和讯网摘 添加到天极网摘 添加到黑米书签 添加到QQ书签 添加到雅虎收藏 添加到奇客发现 diigo it 添加到饭否 添加到飞豆订阅 添加到抓虾收藏 添加到鲜果订阅 digg it 貼到funP 添加到有道阅读 Live Favorites 添加到Newsvine 打印本页 用Email发送本页 在Facebook上分享


Disclaimer Privacy Policy About us Site Map

If you have any requirements, please contact webmaster。(如果有什么要求,请联系站长)
Copyright ©2011-
uuhomepage.com, Inc. All rights reserved.