闪客动漫天地
欢迎 游客 , 注册 | 登录 | 会员 | 界面 | 简洁版本 | 在线 | 帮助
闪客动漫天地论坛

发表新主题 回复该主题
本主题被查看1194次, 共1个帖子, 1页, 当前为第1页     选择页数: 1      跳转到第   上一主题   下一主题
标题: 实现能够将字符串翻译成点的简单类型转换器
注册会员
UID: 309683
来自:
精华: 0
积分: 54
帖子: 54
注册: 2007-6-18 15:03:00
状态: 离线
威望: 0.00
金钱: 12.00 元
只看楼主 2007-06-19 10:40
实现能够将字符串翻译成点的简单类型转换器
实现能够将字符串翻译成点的简单类型转换器
定义一个从 TypeConverter 派生的类。

重写 CanConvertFrom 方法,指定转换器可从中转换的类型。此方法是重载方法。

重写实现转换的 ConvertFrom 方法。此方法是重载方法。

重写 CanConvertTo 方法,指定转换器可转换为哪种类型。转换为字符串类型不需要重写此方法。此方法是重载方法。

重写实现转换的 ConvertTo 方法。此方法是重载方法。

重写执行验证的 IsValid 方法。此方法是重载方法。

下面的代码示例实现了一个类型转换器,该转换器可以将 String 类型转换为 Point 类型,将 Point 转换为 String。此示例中不重写 CanConvertTo 和 IsValid 方法。

Visual Basic  复制代码
Option Explicit
Option Strict

Imports System
Imports System.ComponentModel
Imports System.Globalization
Imports System.Drawing

Public Class PointConverter
  Inherits TypeConverter
 
  ' Overrides the CanConvertFrom method of TypeConverter.
  ' The ITypeDescriptorContext interface provides the context for the
  ' conversion. Typically, this interface is used at design time to
  ' provide information about the design-time container.
  Public Overrides Overloads Function CanConvertFrom(context As ITypeDescriptorContext, sourceType As Type) As Boolean
      If sourceType Is GetType(String) Then
        Return True
      End If
      Return MyBase.CanConvertFrom(context, sourceType)
  End Function
 
  ' Overrides the ConvertFrom method of TypeConverter.
  Public Overrides Overloads Function ConvertFrom(context As ITypeDescriptorContext, culture As CultureInfo, value As Object) As Object
      If TypeOf value Is String Then
        Dim v As String() = CStr(value).Split(New Char() {","c})
        Return New Point(Integer.Parse(v(0)), Integer.Parse(v(1)))
      End If
      Return MyBase.ConvertFrom(context, culture, value)
  End Function
 
  ' Overrides the ConvertTo method of TypeConverter.
  Public Overrides Overloads Function ConvertTo(context As ITypeDescriptorContext, culture As CultureInfo, value As Object, destinationType As Type) As Object
      If destinationType Is GetType(String) Then
        Return CType(value, Point).X & "," & CType(value, Point).Y
      End If
      Return MyBase.ConvertTo(context, culture, value, destinationType)
  End Function
End Class

C#  复制代码
using System;
using System.ComponentModel;
using System.Globalization;
using System.Drawing;

public class PointConverter : TypeConverter {
  // Overrides the CanConvertFrom method of TypeConverter.
  // The ITypeDescriptorContext interface provides the context for the
  // conversion. Typically, this interface is used at design time to
  // provide information about the design-time container.
  public override bool CanConvertFrom(ITypeDescriptorContext context,
      Type sourceType) {
     
      if (sourceType == typeof(string)) {
        return true;
      }
      return base.CanConvertFrom(context, sourceType);
  }
  // Overrides the ConvertFrom method of TypeConverter.
  public override object ConvertFrom(ITypeDescriptorContext context,
      CultureInfo culture, object value) {
      if (value is string) {
        string[] v = ((string)value).Split(new char[] {','});
        return new Point(int.Parse(v[0]), int.Parse(v[1]));
      }
      return base.ConvertFrom(context, culture, value);
  }
  // Overrides the ConvertTo method of TypeConverter.
  public override object ConvertTo(ITypeDescriptorContext context,
      CultureInfo culture, object value, Type destinationType) { 
      if (destinationType == typeof(string)) {
        return ((Point)value).X + "," + ((Point)value).Y;
      }
      return base.ConvertTo(context, culture, value, destinationType);
  }
}
#1  
发表新主题 回复该主题
本主题被查看1194次, 共1个帖子, 1页, 当前为第1页     选择页数: 1      跳转到第







现在的时间是 2008-09-09 01:36:21
沪ICP备05003105号

版权所有 闪客俱乐部  
         Powered by Discuz!NT 1.0.6666    Copyright © 2001-2008 Comsenz Inc.
Processed in 0.128 seconds