深度解析 TypeConverter TypeConverterAttribute (二)
TypeConverterAttribute Class
????TypeConverterAttribute 其實就是一個繼承Attribute的類,使用[TypeConverter(typeof(MyClassConverter))]標簽施加到程序實體 上。根據TypeConverterAttritue的定義知道這個屬性Attribute可以施加到所有實體上。
????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標簽,這兩者就能關聯起來。為什么呢?
????public?class?Longitude
????{}
那是因為如果一個類附件了Attribute,那么這個類就可以通過反射方法得到這個類屬性,還可以通過TypeDescriptor.GetConverter靜態方法獲得這個類相關轉換類的實例。這樣就輕松的關聯起來了。比如Test
????{
????????public?static?void?Main(string[]?args)
????????{
????????????//將Longitude類轉換到string類型
????????????Longitude?longitude?=?new?Longitude(10,11,12,LongitudeDirection.East);
????????????LongitudeTypeConverter?converter?=?new?LongitudeTypeConverter();
????????????string?strLongitude="";
????????????if?(converter.CanConvertTo(typeof(string)))
????????????{
????????????????//Longitude?類沒有附件TypeconverterAttribute時
????????????????strLongitude?=?(string)converter.ConvertTo(longitude,?typeof(string));
????????????????//Longitude?類附件了TypeconverterAttribute時
????????????????strLongitude?=?(string)TypeDescriptor.GetConverter(typeof(Longitude)).ConvertTo(longitude,?typeof(string));
????????????}
????????????System.Console.WriteLine(strLongitude);
????????????//將string還原回Longitude類
????????????Longitude?longitude1?=?new?Longitude();
????????????if?(converter.CanConvertFrom(typeof(string)))
????????????{
????????????????//Longitude?類沒有附件TypeconverterAttribute時
????????????????longitude1?=?(Longitude)converter.ConvertFrom(strLongitude);
????????????????//Longitude?類附件了TypeconverterAttribute時
????????????????longitude1?=?(Longitude)TypeDescriptor.GetConverter(typeof(Longitude)).ConvertFrom(strLongitude);
????????????}
????????????System.Console.WriteLine(longitude1.Degrees);
????????????System.Console.WriteLine(longitude1.Direction);
????????????System.Console.WriteLine(longitude1.Minutes);
????????????System.Console.WriteLine(longitude1.Seconds);
????????}
????}
上面是在運行時,如果Longitude類的方法或字段也附加了相應的ConverterAttribute,我們也可以通過反射的方法得到附加ConverterAttribute的方法或字段。
例如,
????????????//AttributeTargs包括method實體
????????????CustomTypeConverterAttribute?customTypeConverterAttribute;
????????????foreach?(Attribute?att?in?type.GetCustomAttributes(typeof(OtherTypeConverter),?true))
????????????{
????????????????System.Console.WriteLine("OtherTypeConverter?的屬性Property"?+?customTypeConverterAttribute.Property1);
????????????}
????????????foreach?(MethodInfo?method?in?type.GetMethods())
????????????{
????????????????foreach?(Attribute?att?in?method.GetCustomAttributes(true))
????????????????{
????????????????????customTypeConverterAttribute?=?att?as?CustomTypeConverterAttribute;
????????????????????if?(null?!=?customTypeConverterAttribute)
????????????????????{
????????????????????????System.Console.WriteLine("類的方法是:"?+?method.Name?+?"?該方法的附加Attribute是:"?+?customTypeConverterAttribute.ToString());
????????????????????}
????????????????}
????????????}
如果你想test上面的代碼,需要自己完成CustomTypeConverterAttribute。
在 設計時的應用,例如在復雜控件中的一個屬性為一個類,我們需要在property browser中以string的形式輸入值來初始化或構造這個屬性property,我們需要在這個屬性property上附件屬性 DesignerSerializationVisibility標簽。
例如
????????[Category("Appearance")]
????????[DefaultValue(typeof(GPSLocation),?"")]
????????[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
????????[PersistenceMode(PersistenceMode.InnerProperty)]
????????public?GPSLocation?Location
????????{
????????????get
????????????{
????????????????//?if?no?instance?created?yet?do?so
????????????????if?(_Location?==?null)
????????????????????_Location?=?new?GPSLocation();
????????????????return?_Location;
????????????}
????????}
上面還有PersistenceMode屬性標簽,其表示代碼在asp.net頁面顯示(也可以說是持久)的方式,有幾種詳見http://www.cnblogs.com/thinhunan/archive/2006/12/10/588341.html
這樣就可以達到效果如下([PersistenceMode(PersistenceMode.Attribute)])
????????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
不對之處請批評指正。
測試代碼
?
轉載自http://blog.csdn.net/luyifeiniu/article/details/5107839
轉載于:https://www.cnblogs.com/wsion/archive/2013/04/19/3030218.html
總結
以上是生活随笔為你收集整理的深度解析 TypeConverter TypeConverterAttribute (二)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: VMware下主机与虚拟机通信问题
- 下一篇: 拦截器,过滤器,监听器原理