国产一级a片免费看高清,亚洲熟女中文字幕在线视频,黄三级高清在线播放,免费黄色视频在线看

打開APP
userphoto
未登錄

開通VIP,暢享免費(fèi)電子書等14項(xiàng)超值服

開通VIP
深度解析 TypeConverter & TypeConverterAttribute (二)

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

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

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

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

那是因?yàn)槿绻粋€(gè)類附件了Attribute,那么這個(gè)類就可以通過反射方法得到這個(gè)類屬性,還可以通過TypeDescriptor.GetConverter靜態(tài)方法獲得這個(gè)類相關(guān)轉(zhuǎn)換類的實(shí)例。這樣就輕松的關(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時(shí)
                strLongitude = (string)converter.ConvertTo(longitude, typeof(string));
                //Longitude 類附件了TypeconverterAttribute時(shí)
                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時(shí)
                longitude1 = (Longitude)converter.ConvertFrom(strLongitude);
                //Longitude 類附件了TypeconverterAttribute時(shí)
                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);

        }
    }

上面是在運(yùn)行時(shí),如果Longitude類的方法或字段也附加了相應(yīng)的ConverterAttribute,我們也可以通過反射的方法得到附加ConverterAttribute的方法或字段。
例如,

 Type type = typeof(Longitude);
            //AttributeTargs包括method實(shí)體
            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è)計(jì)時(shí)的應(yīng)用,例如在復(fù)雜控件中的一個(gè)屬性為一個(gè)類,我們需要在property browser中以string的形式輸入值來初始化或構(gòu)造這個(gè)屬性property,我們需要在這個(gè)屬性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

這樣就可以達(dá)到效果如下([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

不對(duì)之處請(qǐng)批評(píng)指正。

測試代碼

本站僅提供存儲(chǔ)服務(wù),所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊舉報(bào)。
打開APP,閱讀全文并永久保存 查看更多類似文章
猜你喜歡
類似文章
.Net基礎(chǔ)之特性
.net Attribute的讀書筆記
GetCustomAttributes方法獲取Attribute的順序問題
類的擴(kuò)展
利用Attribute簡化Unity框架IOC注入
C# Lambda表達(dá)式詳解,及Lambda表達(dá)式樹的創(chuàng)建
更多類似文章 >>
生活服務(wù)
分享 收藏 導(dǎo)長圖 關(guān)注 下載文章
綁定賬號(hào)成功
后續(xù)可登錄賬號(hào)暢享VIP特權(quán)!
如果VIP功能使用有故障,
可點(diǎn)擊這里聯(lián)系客服!

聯(lián)系客服