TypeConverterAttribute Class
    TypeConverterAttribute 其实就是一个继承Attribute的类,使用[TypeConverter(typeof(MyClassConverter))]标签施加到程序实体上。根据TypeConverterAttritue的定义知道这个属性Attribute可以施加到所有实体上。
深度解析 TypeConverter & TypeConverterAttribute (二)    [AttributeUsageAttribute(AttributeTargets.All)] 
深度解析 TypeConverter & TypeConverterAttribute (二)    
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 & TypeConverterAttribute (二) [TypeConverter(typeof(LongitudeTypeConverter))]
深度解析 TypeConverter & TypeConverterAttribute (二)    
public class Longitude
{}

那是因为如果一个类附件了Attribute,那么这个类就可以通过反射方法得到这个类属性,还可以通过TypeDescriptor.GetConverter静态方法获得这个类相关转换类的实例。这样就轻松的关联起来了。比如Test

深度解析 TypeConverter & TypeConverterAttribute (二)class Test
    }

上面是在运行时,如果Longitude类的方法或字段也附加了相应的ConverterAttribute,我们也可以通过反射的方法得到附加ConverterAttribute的方法或字段。
例如,

深度解析 TypeConverter & TypeConverterAttribute (二) Type type = typeof(Longitude);
深度解析 TypeConverter & TypeConverterAttribute (二)            
//AttributeTargs包括method实体
深度解析 TypeConverter & TypeConverterAttribute (二)
            CustomTypeConverterAttribute customTypeConverterAttribute;
深度解析 TypeConverter & TypeConverterAttribute (二)
深度解析 TypeConverter & TypeConverterAttribute (二)            
foreach (Attribute att in type.GetCustomAttributes(typeof(OtherTypeConverter), true))
            }


如果你想test上面的代码,需要自己完成CustomTypeConverterAttribute。

在设计时的应用,例如在复杂控件中的一个属性为一个类,我们需要在property browser中以string的形式输入值来初始化或构造这个属性property,我们需要在这个属性property上附件属性DesignerSerializationVisibility标签。
例如

深度解析 TypeConverter & TypeConverterAttribute (二) [Bindable(true)]
深度解析 TypeConverter & TypeConverterAttribute (二)        [Category(
"Appearance")]
深度解析 TypeConverter & TypeConverterAttribute (二)        [DefaultValue(
typeof(GPSLocation), "")]
深度解析 TypeConverter & TypeConverterAttribute (二)        [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
深度解析 TypeConverter & TypeConverterAttribute (二)        [PersistenceMode(PersistenceMode.InnerProperty)]
深度解析 TypeConverter & TypeConverterAttribute (二)        
public GPSLocation Location
        }

上面还有PersistenceMode属性标签,其表示代码在asp.net页面显示(也可以说是持久)的方式,有几种详见http://www.cnblogs.com/thinhunan/archive/2006/12/10/588341.html

这样就可以达到效果如下([PersistenceMode(PersistenceMode.Attribute)]
深度解析 TypeConverter & TypeConverterAttribute (二)<ui:LocationControl ID="LocationControl1" runat="server" 
深度解析 TypeConverter & TypeConverterAttribute (二)        Location
-GPSLatitude="12N1'2"" Location-GPSLongitude="24W3'4"">
深度解析 TypeConverter & TypeConverterAttribute (二)
</ui:LocationControl>
[PersistenceMode(PersistenceMode.InnerProperty)]
深度解析 TypeConverter & TypeConverterAttribute (二)<ui:LocationControl ID="LocationControl1" runat="server">
深度解析 TypeConverter & TypeConverterAttribute (二)   
<Location GPSLatitude="12N1'3"" GPSLongitude="24W3'4"" />
深度解析 TypeConverter & TypeConverterAttribute (二)
</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

不对之处请批评指正。

测试代码

相关文章: