您的位置:首页 > 其它

深度解析 TypeConverter & TypeConverterAttribute (二)

2007-12-12 10:59 846 查看
TypeConverterAttribute Class

TypeConverterAttribute 其实就是一个继承Attribute的类,使用[TypeConverter(typeof(MyClassConverter))]标签施加到程序实体上。根据TypeConverterAttritue的定义知道这个属性Attribute可以施加到所有实体上。

[AttributeUsageAttribute(AttributeTargets.All)]

public sealed class TypeConverterAttribute : Attribute
如果你对Attribute不太了解可以先看看dudu的阐释,或者看看http://www.codeproject.com/KB/cs/attributes.aspx

Attribute是一种新的申明方式,它可以在是设计时和运行时作用于它附加的Program Entities上。

上篇我们定义了class Longitude 和 class LongitudeTypeConverter,然后我们做了个Test,实现了转换的目的。

但要在设计时或在运行时起作用,就是说在这两种情况LongitudeTypeConverter“自动的”帮助Longitude 实例于其他实例转换,需要TypeConverterAttribute的帮忙。

在coding中我们很惊奇的发现,只用在Longitude类上附加TypeConverterAttribute标签,这两者就能关联起来。为什么呢?

[TypeConverter(typeof(LongitudeTypeConverter))]

public class Longitude

class Test

Type type = typeof(Longitude);

//AttributeTargs包括method实体

CustomTypeConverterAttribute customTypeConverterAttribute;

foreach (Attribute att in type.GetCustomAttributes(typeof(OtherTypeConverter), true))

foreach (MethodInfo method in type.GetMethods())

[Bindable(true)]

[Category("Appearance")]

[DefaultValue(typeof(GPSLocation), "")]

[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]

[PersistenceMode(PersistenceMode.InnerProperty)]

public GPSLocation Location

<ui:LocationControl ID="LocationControl1" runat="server"

Location-GPSLatitude="12N1'2"" Location-GPSLongitude="24W3'4"">

</ui:LocationControl>
([PersistenceMode(PersistenceMode.InnerProperty)])

<ui:LocationControl ID="LocationControl1" runat="server">

<Location GPSLatitude="12N1'3"" GPSLongitude="24W3'4"" />

</ui:LocationControl>

参考
http://www.codeproject.com/KB/webforms/TypeConverters.aspx http://www.codeproject.com/KB/cs/attributes.aspx
dudu:Attribute系列 http://www.cnblogs.com/ericwen/favorite/115549.html
不对之处请批评指正。

测试代码
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: