深度解析 TypeConverter & TypeConverterAttribute (二
点击次数:25 次 发布日期:2008-11-26 10:33:26 作者:源代码网
|
源代码网推荐 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 源代码网推荐 {} 源代码网推荐 那是因为如果一个类附件了Attribute,那么这个类就可以通过反射方法得到这个类属性,还可以通过TypeDescriptor.GetConverter静态方法获得这个类相关转换类的实例。这样就轻松的关联起来了。比如Test 源代码网推荐 源代码网推荐 源代码网推荐 class 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的方法或字段。 源代码网推荐 例如, 源代码网推荐 源代码网推荐 源代码网推荐 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。 源代码网推荐 源代码网推荐 在设计时的应用,例如在复杂控件中的一个属性为一个类,我们需要在property browser中以string的形式输入值来初始化或构造这个属性property,我们需要在这个属性property上附件属性DesignerSerializationVisibility标签。 源代码网推荐 例如 源代码网推荐 源代码网推荐 [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属性标签,其表示代码在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 源代码网推荐 做人要厚道,请注明转自酷网动力(www.ASPCOOL.COM)。 源代码网推荐 源代码网供稿. |
