TypeConvert Demo

下班了,先把代码贴上

Code
[TypeConverter(typeof(AddressConverter))]
    
public class Address
    
{
        
public Address() { }

        
public Address(string street, string city,string state, string zip)
        
{
            
this.street = street;
            
this.city = city;
            
this.state = state;
            
this.zip = zip;
        }


        
private String street = null;
        
private String city = null;
        
private String state = null;
        
private String zip = null;

        [NotifyParentProperty(
true)]
        
public String Street
        
{
            
get
            
{
                
return street;
            }

            
set
            
{
                street 
= value;
            }

        }


        [NotifyParentProperty(
true)]
        [TypeConverter(
typeof(CityConverter))]
        
public String City
        
{
            
get
            
{
                
return city;
            }

            
set
            
{
                city 
= value;
            }

        }


        [NotifyParentProperty(
true)]
        
public String State
        
{
            
get
            
{
                
return state;
            }

            
set
            
{
                state 
= value;
            }

        }


        [NotifyParentProperty(
true)]
        
public String Zip
        
{
            
get
            
{
                
return zip;
            }

            
set
            
{
                zip 
= value;
            }

        }


        
public override string ToString()
        
{
            
return this.ToString(CultureInfo.CurrentCulture);
        }


        
protected virtual string ToString(CultureInfo culture)
        
{
            
return TypeDescriptor.GetConverter(typeof(Address)).ConvertToString(null,culture,this);
        }

    }


    
/**//// <summary>
    
/// 自定义类型转换器
    
/// </summary>

    public class AddressConverter : ExpandableObjectConverter
    
{
        
/**//// <summary>
        
/// 
        
/// </summary>
        
/// <param name="context"></param>
        
/// <param name="sourceType">sourceType表示要转换的类型</param>
        
/// <returns>返回值能否将String类型转换为Address类型</returns>

        public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
        
{
            
if(sourceType==typeof(string))
            
{
                
return true;
            }

            
return base.CanConvertFrom(context, sourceType);
        }


        
/**//// <summary>
        
/// 
        
/// </summary>
        
/// <param name="context"></param>
        
/// <param name="destinationType">表示要转换到的类型</param>
        
/// <returns>返回值能否将Address类型转换为String类型</returns>

        public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
        
{
            
if (destinationType == typeof(string))
            
{
                
return true;
            }

            
return base.CanConvertTo(context, destinationType);
        }


        
/**//// <summary>
        
/// 
        
/// </summary>
        
/// <param name="context"></param>
        
/// <param name="culture"></param>
        
/// <param name="value">value为要转换的类型</param>
        
/// <returns>将String类型转换为Address类型</returns>

        public override object ConvertFrom(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value)
        
{
            
if (value == null)
                
return new Address();

            
if (value is string)
            
{
                
string s = (string)value;
                
if (s.Length == 0)
                    
return new Address();

                
string[] parts = s.Split(culture.TextInfo.ListSeparator[0]);
                
if (parts.Length != 4)
                    
throw new ArgumentException();

                TypeConverter stringConverter 
= TypeDescriptor.GetConverter(typeof(string));
                
                
return new Address((string)stringConverter.ConvertFromString(context,culture,parts[0]),
                    (
string)stringConverter.ConvertFromString(context,culture,parts[1]),
                    (
string)stringConverter.ConvertFromString(context,culture,parts[2]),
                    (
string)stringConverter.ConvertFromString(context,culture,parts[3])
                    );
            }


            
return base.ConvertFrom(context, culture, value);
        }


        
/**//// <summary>
        
/// 将Address类型转换为String类型
        
/// </summary>
        
/// <param name="context"></param>
        
/// <param name="culture"></param>
        
/// <param name="value">value为要转换的类型</param>
        
/// <param name="destinationType"></param>
        
/// <returns></returns>

        public override object ConvertTo(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, Type destinationType)
        
{
            
if (value != null)
            
{
                
if (!(value is Address))
                
{
                    
throw new ArgumentException();
                }

            }


            
if(destinationType==typeof(string))
            
{
                
if (value == null)
                    
return string.Empty;

                Address ad 
= (Address)value;
                TypeConverter stringConverter 
= TypeDescriptor.GetConverter(typeof(string));
                
return string.Join(culture.TextInfo.ListSeparator,
                    
new string[] 
                        stringConverter.ConvertToString(context,culture,ad.State),
                        stringConverter.ConvertToString(context,culture,ad.City),
                        stringConverter.ConvertToString(context,culture,ad.Street),
                        stringConverter.ConvertToString(context,culture,ad.Zip),
                    }
);
            }


            
return base.ConvertTo(context, culture, value, destinationType);
        }

    }


    
/**//// <summary>
    
/// 
    
/// </summary>

    public class CityConverter : StringConverter
    
{
        
/**//// <summary>
        
/// 返回此对象是否支持可以从列表中选取的标准值集
        
/// </summary>
        
/// <param name="context"></param>
        
/// <returns></returns>

        public override bool GetStandardValuesSupported(ITypeDescriptorContext context)
        
{
            
return true;
        }


        
/**//// <summary>
        
/// 返回下拉框集合类
        
/// </summary>
        
/// <param name="context"></param>
        
/// <returns></returns>

        public override TypeConverter.StandardValuesCollection GetStandardValues(ITypeDescriptorContext context)
        
{
            
return new StandardValuesCollection(new string[] 
                
{
                    
"naxx",
                    
"mx",
                    
"bwl" 
                }
);
        }


        
/**//// <summary>
        
/// 标准值的集合是否为独占列表
        
/// 默认为flase,为true则表示无法修改列表值
        
/// </summary>
        
/// <param name="context"></param>
        
/// <returns></returns>

        public override bool GetStandardValuesExclusive(ITypeDescriptorContext context)
        
{
            
return false;//false可自定义内容 true只能选择下拉列表内容
        }

    }

转载于:https://www.cnblogs.com/raylovelc/archive/2008/06/12/1218562.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值