日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

深度解析 TypeConverter TypeConverterAttribute (二)

發(fā)布時間:2024/4/11 编程问答 45 豆豆
生活随笔 收集整理的這篇文章主要介紹了 深度解析 TypeConverter TypeConverterAttribute (二) 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

TypeConverterAttribute Class
????TypeConverterAttribute 其實就是一個繼承Attribute的類,使用[TypeConverter(typeof(MyClassConverter))]標(biāo)簽施加到程序?qū)嶓w 上。根據(jù)TypeConverterAttritue的定義知道這個屬性Attribute可以施加到所有實體上。

????[AttributeUsageAttribute(AttributeTargets.All)]?
????
public?sealed?class?TypeConverterAttribute?:?Attribute

如果你對Attribute不太了解可以先看看dudu的闡釋,或者看看http://www.codeproject.com/KB/cs/attributes.aspx
Attribute是一種新的申明方式,它可以在是設(shè)計時和運行時作用于它附加的Program Entities上。
上篇我們定義了class Longitude 和 class LongitudeTypeConverter,然后我們做了個Test,實現(xiàn)了轉(zhuǎn)換的目的。
但要在設(shè)計時或在運行時起作用,就是說在這兩種情況LongitudeTypeConverter“自動的”幫助Longitude 實例于其他實例轉(zhuǎn)換,需要TypeConverterAttribute的幫忙。
在coding中我們很驚奇的發(fā)現(xiàn),只用在Longitude類上附加TypeConverterAttribute標(biāo)簽,這兩者就能關(guān)聯(lián)起來。為什么呢?

?[TypeConverter(typeof(LongitudeTypeConverter))]
????
public?class?Longitude
????
{}

那是因為如果一個類附件了Attribute,那么這個類就可以通過反射方法得到這個類屬性,還可以通過TypeDescriptor.GetConverter靜態(tài)方法獲得這個類相關(guān)轉(zhuǎn)換類的實例。這樣就輕松的關(guān)聯(lián)起來了。比如Test

class?Test
????
{
????????
public?static?void?Main(string[]?args)
????????
{
????????????
//將Longitude類轉(zhuǎn)換到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類的方法或字段也附加了相應(yīng)的ConverterAttribute,我們也可以通過反射的方法得到附加ConverterAttribute的方法或字段。
例如,

?Type?type?=?typeof(Longitude);
????????????
//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。

在 設(shè)計時的應(yīng)用,例如在復(fù)雜控件中的一個屬性為一個類,我們需要在property browser中以string的形式輸入值來初始化或構(gòu)造這個屬性property,我們需要在這個屬性property上附件屬性 DesignerSerializationVisibility標(biāo)簽。
例如

?[Bindable(true)]
????????[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屬性標(biāo)簽,其表示代碼在asp.net頁面顯示(也可以說是持久)的方式,有幾種詳見http://www.cnblogs.com/thinhunan/archive/2006/12/10/588341.html

這樣就可以達到效果如下([PersistenceMode(PersistenceMode.Attribute)]

<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

不對之處請批評指正。

測試代碼

?

轉(zhuǎn)載自http://blog.csdn.net/luyifeiniu/article/details/5107839

轉(zhuǎn)載于:https://www.cnblogs.com/wsion/archive/2013/04/19/3030218.html

總結(jié)

以上是生活随笔為你收集整理的深度解析 TypeConverter TypeConverterAttribute (二)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。