深度解析 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
不对之处请批评指正。
测试代码
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
不对之处请批评指正。
测试代码
相关文章推荐
- 深度解析 TypeConverter & TypeConverterAttribute (一)
- 【Visual Studio风格开发系列 - PropertyGrid控件】深度解析 TypeConverter & TypeConverterAttribute
- 深度解析 TypeConverter & TypeConverterAttribute (一)
- 深度解析 TypeConverter & TypeConverterAttribute (二)
- [转]深度解析 TypeConverter & TypeConverterAttribute
- 深度解析 TypeConverter & TypeConverterAttribute (一)
- 深度解析 TypeConverter & TypeConverterAttribute (一)
- 深度解析 TypeConverter & TypeConverterAttribute (二)
- 深度解析 TypeConverter & TypeConverterAttribute (二)
- LLBL Gen 3.x 源代码追踪与解析 Type Converter 类型转换器
- .NET组件编程(5) TypeConverterAttribute,类型转换
- 组件编程(5) TypeConverterAttribute,类型转换
- Oracle直接路径加载--append的深度解析
- 深度解析Asp.Net2.0中的Callback机制
- 深度解析 BlogEngine.Net
- 让你5分钟内学懂,iOS开发底层原理技术~RAC深度解析
- Android下event事件深度解析
- 【深度学习】深入解析dropout
- 深度解析VC中的消息(转)
- 微信小程序全方位深度解析【免费学习】