且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

开发中灵活使用属性类

更新时间:2022-09-19 16:53:53

什么是属性类?

        从抽象类System.Attribute派生出来的类(无论是直接的还是间接的)都称为属性类(MSDN中的定义)。



        属性类的名称一般都带有Attribute后缀,但使用的时候可以包含也可以忽略,如果你自己起的名字没有这个后缀,则需要全称来区分了。如果不怕输入麻烦,建议还是不管是否有后缀,都输入全称为好,这样可以减少歧义。

        在.NET框架中差不多定义了200个Attribute相关类,可见Attribute的应用十分广泛,它的全图在MSDN中如下:

开发中灵活使用属性类System.Object
开发中灵活使用属性类   System.Attribute
开发中灵活使用属性类      System.AttributeUsageAttribute
开发中灵活使用属性类      System.CLSCompliantAttribute
开发中灵活使用属性类      System.ComponentModel.AmbientValueAttribute
开发中灵活使用属性类      System.ComponentModel.BindableAttribute
开发中灵活使用属性类      System.ComponentModel.BrowsableAttribute
开发中灵活使用属性类      System.ComponentModel.CategoryAttribute
开发中灵活使用属性类      System.ComponentModel.DefaultEventAttribute
开发中灵活使用属性类      System.ComponentModel.DefaultPropertyAttribute
开发中灵活使用属性类      System.ComponentModel.DefaultValueAttribute
开发中灵活使用属性类      System.ComponentModel.DescriptionAttribute
开发中灵活使用属性类      System.ComponentModel.Design.Serialization.DesignerSerializerAttribute
开发中灵活使用属性类      System.ComponentModel.Design.Serialization.RootDesignerSerializerAttribute
开发中灵活使用属性类      System.ComponentModel.DesignerAttribute
开发中灵活使用属性类      System.ComponentModel.DesignerCategoryAttribute
开发中灵活使用属性类      System.ComponentModel.DesignerSerializationVisibilityAttribute
开发中灵活使用属性类      System.ComponentModel.DesignOnlyAttribute
开发中灵活使用属性类      System.ComponentModel.DesignTimeVisibleAttribute
开发中灵活使用属性类      System.ComponentModel.EditorAttribute
开发中灵活使用属性类      System.ComponentModel.EditorBrowsableAttribute
开发中灵活使用属性类      System.ComponentModel.ImmutableObjectAttribute
开发中灵活使用属性类      System.ComponentModel.InheritanceAttribute
开发中灵活使用属性类      System.ComponentModel.InstallerTypeAttribute
开发中灵活使用属性类      System.ComponentModel.LicenseProviderAttribute
开发中灵活使用属性类      System.ComponentModel.ListBindableAttribute
开发中灵活使用属性类      System.ComponentModel.LocalizableAttribute
开发中灵活使用属性类      System.ComponentModel.MergablePropertyAttribute
开发中灵活使用属性类      System.ComponentModel.NotifyParentPropertyAttribute
开发中灵活使用属性类      System.ComponentModel.ParenthesizePropertyNameAttribute
开发中灵活使用属性类      System.ComponentModel.PropertyTabAttribute
开发中灵活使用属性类      System.ComponentModel.ProvidePropertyAttribute
开发中灵活使用属性类      System.ComponentModel.ReadOnlyAttribute
开发中灵活使用属性类      System.ComponentModel.RecommendedAsConfigurableAttribute
开发中灵活使用属性类      System.ComponentModel.RefreshPropertiesAttribute
开发中灵活使用属性类      System.ComponentModel.RunInstallerAttribute
开发中灵活使用属性类      System.ComponentModel.ToolboxItemAttribute
开发中灵活使用属性类      System.ComponentModel.ToolboxItemFilterAttribute
开发中灵活使用属性类      System.ComponentModel.TypeConverterAttribute
开发中灵活使用属性类      System.ContextStaticAttribute
开发中灵活使用属性类      System.Diagnostics.ConditionalAttribute
开发中灵活使用属性类      System.Diagnostics.DebuggableAttribute
开发中灵活使用属性类      System.Diagnostics.DebuggerHiddenAttribute
开发中灵活使用属性类      System.Diagnostics.DebuggerStepThroughAttribute
开发中灵活使用属性类      System.Drawing.ToolboxBitmapAttribute
开发中灵活使用属性类      System.EnterpriseServices.ApplicationAccessControlAttribute
开发中灵活使用属性类      System.EnterpriseServices.ApplicationActivationAttribute
开发中灵活使用属性类      System.EnterpriseServices.ApplicationIDAttribute
开发中灵活使用属性类      System.EnterpriseServices.ApplicationNameAttribute
开发中灵活使用属性类      System.EnterpriseServices.ApplicationQueuingAttribute
开发中灵活使用属性类      System.EnterpriseServices.AutoCompleteAttribute
开发中灵活使用属性类      System.EnterpriseServices.CompensatingResourceManager.ApplicationCrmEnabledAttribute
开发中灵活使用属性类      System.EnterpriseServices.ComponentAccessControlAttribute
开发中灵活使用属性类      System.EnterpriseServices.COMTIIntrinsicsAttribute
开发中灵活使用属性类      System.EnterpriseServices.ConstructionEnabledAttribute
开发中灵活使用属性类      System.EnterpriseServices.DescriptionAttribute
开发中灵活使用属性类      System.EnterpriseServices.EventClassAttribute
开发中灵活使用属性类      System.EnterpriseServices.EventTrackingEnabledAttribute
开发中灵活使用属性类      System.EnterpriseServices.ExceptionClassAttribute
开发中灵活使用属性类      System.EnterpriseServices.IISIntrinsicsAttribute
开发中灵活使用属性类      System.EnterpriseServices.InterfaceQueuingAttribute
开发中灵活使用属性类      System.EnterpriseServices.JustInTimeActivationAttribute
开发中灵活使用属性类      System.EnterpriseServices.LoadBalancingSupportedAttribute
开发中灵活使用属性类      System.EnterpriseServices.MustRunInClientContextAttribute
开发中灵活使用属性类      System.EnterpriseServices.ObjectPoolingAttribute
开发中灵活使用属性类      System.EnterpriseServices.PrivateComponentAttribute
开发中灵活使用属性类      System.EnterpriseServices.SecureMethodAttribute
开发中灵活使用属性类      System.EnterpriseServices.SecurityRoleAttribute
开发中灵活使用属性类      System.EnterpriseServices.SynchronizationAttribute
开发中灵活使用属性类      System.EnterpriseServices.TransactionAttribute
开发中灵活使用属性类      System.FlagsAttribute
开发中灵活使用属性类      System.LoaderOptimizationAttribute
开发中灵活使用属性类      System.Management.Instrumentation.IgnoreMemberAttribute
开发中灵活使用属性类      System.Management.Instrumentation.InstrumentationClassAttribute
开发中灵活使用属性类      System.Management.Instrumentation.InstrumentedAttribute
开发中灵活使用属性类      System.Management.Instrumentation.ManagedNameAttribute
开发中灵活使用属性类      System.MTAThreadAttribute
开发中灵活使用属性类      System.NonSerializedAttribute
开发中灵活使用属性类      System.ObsoleteAttribute
开发中灵活使用属性类      System.ParamArrayAttribute
开发中灵活使用属性类      System.Reflection.AssemblyAlgorithmIdAttribute
开发中灵活使用属性类      System.Reflection.AssemblyCompanyAttribute
开发中灵活使用属性类      System.Reflection.AssemblyConfigurationAttribute
开发中灵活使用属性类      System.Reflection.AssemblyCopyrightAttribute
开发中灵活使用属性类      System.Reflection.AssemblyCultureAttribute
开发中灵活使用属性类      System.Reflection.AssemblyDefaultAliasAttribute
开发中灵活使用属性类      System.Reflection.AssemblyDelaySignAttribute
开发中灵活使用属性类      System.Reflection.AssemblyDescriptionAttribute
开发中灵活使用属性类      System.Reflection.AssemblyFileVersionAttribute
开发中灵活使用属性类      System.Reflection.AssemblyFlagsAttribute
开发中灵活使用属性类      System.Reflection.AssemblyInformationalVersionAttribute
开发中灵活使用属性类      System.Reflection.AssemblyKeyFileAttribute
开发中灵活使用属性类      System.Reflection.AssemblyKeyNameAttribute
开发中灵活使用属性类      System.Reflection.AssemblyProductAttribute
开发中灵活使用属性类      System.Reflection.AssemblyTitleAttribute
开发中灵活使用属性类      System.Reflection.AssemblyTrademarkAttribute
开发中灵活使用属性类      System.Reflection.AssemblyVersionAttribute
开发中灵活使用属性类      System.Reflection.DefaultMemberAttribute
开发中灵活使用属性类      System.Resources.NeutralResourcesLanguageAttribute
开发中灵活使用属性类      System.Resources.SatelliteContractVersionAttribute
开发中灵活使用属性类      System.Runtime.CompilerServices.AccessedThroughPropertyAttribute
开发中灵活使用属性类      System.Runtime.CompilerServices.CompilationRelaxationsAttribute
开发中灵活使用属性类      System.Runtime.CompilerServices.CompilerGlobalScopeAttribute
开发中灵活使用属性类      System.Runtime.CompilerServices.CustomConstantAttribute
开发中灵活使用属性类      System.Runtime.CompilerServices.DecimalConstantAttribute
开发中灵活使用属性类      System.Runtime.CompilerServices.DiscardableAttribute
开发中灵活使用属性类      System.Runtime.CompilerServices.IndexerNameAttribute
开发中灵活使用属性类      System.Runtime.CompilerServices.MethodImplAttribute
开发中灵活使用属性类      System.Runtime.CompilerServices.RequiredAttributeAttribute
开发中灵活使用属性类      System.Runtime.InteropServices.AutomationProxyAttribute
开发中灵活使用属性类      System.Runtime.InteropServices.BestFitMappingAttribute
开发中灵活使用属性类      System.Runtime.InteropServices.ClassInterfaceAttribute
开发中灵活使用属性类      System.Runtime.InteropServices.CoClassAttribute
开发中灵活使用属性类      System.Runtime.InteropServices.ComAliasNameAttribute
开发中灵活使用属性类      System.Runtime.InteropServices.ComCompatibleVersionAttribute
开发中灵活使用属性类      System.Runtime.InteropServices.ComConversionLossAttribute
开发中灵活使用属性类      System.Runtime.InteropServices.ComEventInterfaceAttribute
开发中灵活使用属性类      System.Runtime.InteropServices.ComImportAttribute
开发中灵活使用属性类      System.Runtime.InteropServices.ComRegisterFunctionAttribute
开发中灵活使用属性类      System.Runtime.InteropServices.ComSourceInterfacesAttribute
开发中灵活使用属性类      System.Runtime.InteropServices.ComUnregisterFunctionAttribute
开发中灵活使用属性类      System.Runtime.InteropServices.ComVisibleAttribute
开发中灵活使用属性类      System.Runtime.InteropServices.DispIdAttribute
开发中灵活使用属性类      System.Runtime.InteropServices.DllImportAttribute
开发中灵活使用属性类      System.Runtime.InteropServices.FieldOffsetAttribute
开发中灵活使用属性类      System.Runtime.InteropServices.GuidAttribute
开发中灵活使用属性类      System.Runtime.InteropServices.IDispatchImplAttribute
开发中灵活使用属性类      System.Runtime.InteropServices.ImportedFromTypeLibAttribute
开发中灵活使用属性类      System.Runtime.InteropServices.InAttribute
开发中灵活使用属性类      System.Runtime.InteropServices.InterfaceTypeAttribute
开发中灵活使用属性类      System.Runtime.InteropServices.LCIDConversionAttribute
开发中灵活使用属性类      System.Runtime.InteropServices.MarshalAsAttribute
开发中灵活使用属性类      System.Runtime.InteropServices.OptionalAttribute
开发中灵活使用属性类      System.Runtime.InteropServices.OutAttribute
开发中灵活使用属性类      System.Runtime.InteropServices.PreserveSigAttribute
开发中灵活使用属性类      System.Runtime.InteropServices.PrimaryInteropAssemblyAttribute
开发中灵活使用属性类      System.Runtime.InteropServices.ProgIdAttribute
开发中灵活使用属性类      System.Runtime.InteropServices.StructLayoutAttribute
开发中灵活使用属性类      System.Runtime.InteropServices.TypeLibFuncAttribute
开发中灵活使用属性类      System.Runtime.InteropServices.TypeLibTypeAttribute
开发中灵活使用属性类      System.Runtime.InteropServices.TypeLibVarAttribute
开发中灵活使用属性类      System.Runtime.InteropServices.TypeLibVersionAttribute
开发中灵活使用属性类      System.Runtime.Remoting.Messaging.OneWayAttribute
开发中灵活使用属性类      System.Runtime.Remoting.Metadata.SoapAttribute
开发中灵活使用属性类      System.Runtime.Remoting.Proxies.ProxyAttribute
开发中灵活使用属性类      System.Security.AllowPartiallyTrustedCallersAttribute
开发中灵活使用属性类      System.Security.Permissions.SecurityAttribute
开发中灵活使用属性类      System.Security.SuppressUnmanagedCodeSecurityAttribute
开发中灵活使用属性类      System.Security.UnverifiableCodeAttribute
开发中灵活使用属性类      System.SerializableAttribute
开发中灵活使用属性类      System.STAThreadAttribute
开发中灵活使用属性类      System.ThreadStaticAttribute
开发中灵活使用属性类      System.Web.Services.Configuration.XmlFormatExtensionAttribute
开发中灵活使用属性类      System.Web.Services.Configuration.XmlFormatExtensionPointAttribute
开发中灵活使用属性类      System.Web.Services.Configuration.XmlFormatExtensionPrefixAttribute
开发中灵活使用属性类      System.Web.Services.Protocols.HttpMethodAttribute
开发中灵活使用属性类      System.Web.Services.Protocols.MatchAttribute
开发中灵活使用属性类      System.Web.Services.Protocols.SoapDocumentMethodAttribute
开发中灵活使用属性类      System.Web.Services.Protocols.SoapDocumentServiceAttribute
开发中灵活使用属性类      System.Web.Services.Protocols.SoapExtensionAttribute
开发中灵活使用属性类      System.Web.Services.Protocols.SoapHeaderAttribute
开发中灵活使用属性类      System.Web.Services.Protocols.SoapRpcMethodAttribute
开发中灵活使用属性类      System.Web.Services.Protocols.SoapRpcServiceAttribute
开发中灵活使用属性类      System.Web.Services.WebMethodAttribute
开发中灵活使用属性类      System.Web.Services.WebServiceAttribute
开发中灵活使用属性类      System.Web.Services.WebServiceBindingAttribute
开发中灵活使用属性类      System.Web.UI.ConstructorNeedsTagAttribute
开发中灵活使用属性类      System.Web.UI.ControlBuilderAttribute
开发中灵活使用属性类      System.Web.UI.DataBindingHandlerAttribute
开发中灵活使用属性类      System.Web.UI.ParseChildrenAttribute
开发中灵活使用属性类      System.Web.UI.PartialCachingAttribute
开发中灵活使用属性类      System.Web.UI.PersistChildrenAttribute
开发中灵活使用属性类      System.Web.UI.PersistenceModeAttribute
开发中灵活使用属性类      System.Web.UI.TagPrefixAttribute
开发中灵活使用属性类      System.Web.UI.TemplateContainerAttribute
开发中灵活使用属性类      System.Web.UI.ToolboxDataAttribute
开发中灵活使用属性类      System.Web.UI.ValidationPropertyAttribute
开发中灵活使用属性类      System.Xml.Serialization.SoapAttributeAttribute
开发中灵活使用属性类      System.Xml.Serialization.SoapElementAttribute
开发中灵活使用属性类      System.Xml.Serialization.SoapEnumAttribute
开发中灵活使用属性类      System.Xml.Serialization.SoapIgnoreAttribute
开发中灵活使用属性类      System.Xml.Serialization.SoapIncludeAttribute
开发中灵活使用属性类      System.Xml.Serialization.SoapTypeAttribute
开发中灵活使用属性类      System.Xml.Serialization.XmlAnyAttributeAttribute
开发中灵活使用属性类      System.Xml.Serialization.XmlAnyElementAttribute
开发中灵活使用属性类      System.Xml.Serialization.XmlArrayAttribute
开发中灵活使用属性类      System.Xml.Serialization.XmlArrayItemAttribute
开发中灵活使用属性类      System.Xml.Serialization.XmlAttributeAttribute
开发中灵活使用属性类      System.Xml.Serialization.XmlChoiceIdentifierAttribute
开发中灵活使用属性类      System.Xml.Serialization.XmlElementAttribute
开发中灵活使用属性类      System.Xml.Serialization.XmlEnumAttribute
开发中灵活使用属性类      System.Xml.Serialization.XmlIgnoreAttribute
开发中灵活使用属性类      System.Xml.Serialization.XmlIncludeAttribute
开发中灵活使用属性类      System.Xml.Serialization.XmlNamespaceDeclarationsAttribute
开发中灵活使用属性类      System.Xml.Serialization.XmlRootAttribute
开发中灵活使用属性类      System.Xml.Serialization.XmlTextAttribute
开发中灵活使用属性类      System.Xml.Serialization.XmlTypeAttribute

System.STAThreadAttribute
       大家在一般使用中会留意到这个属性吗?用VS.NET新建一个项目,往往在main前面会如下面所示:
开发中灵活使用属性类        /// <summary>
开发中灵活使用属性类        
/// 应用程序的主入口点。
开发中灵活使用属性类        
/// </summary>

开发中灵活使用属性类        [STAThread]
开发中灵活使用属性类        static void Main() 
开发中灵活使用属性类        {
开发中灵活使用属性类            Application.Run(new frmMain());
开发中灵活使用属性类        }

       其实这个就是属性类STAThreadAttribute了,它的作用表示应用程序的COM线程模型是单线程单元(STA)。


属性类的实用性:

1.利用XML相关的属性类,将相关业务类XML序列化,可以达到保存状态和远程传输交换类信息等作用。
共14个和XML相关的属性类:
System.Xml.Serialization.XmlAnyAttributeAttribute
System.Xml.Serialization.XmlAnyElementAttribute 
System.Xml.Serialization.XmlArrayAttribute
System.Xml.Serialization.XmlArrayItemAttribute
System.Xml.Serialization.XmlAttributeAttribute
System.Xml.Serialization.XmlChoiceIdentifierAttribute
System.Xml.Serialization.XmlElementAttribute
System.Xml.Serialization.XmlEnumAttribute
System.Xml.Serialization.XmlIgnoreAttribute
System.Xml.Serialization.XmlIncludeAttribute
System.Xml.Serialization.XmlNamespaceDeclarationsAttribute
System.Xml.Serialization.XmlRootAttribute
System.Xml.Serialization.XmlTextAttribute
System.Xml.Serialization.XmlTypeAttribute

        最近做一个项目,需要将一些界面输入的有一定结构规律的信息保存起来,然后下次需要时候可以调入到对应的界面位置上使用。(其实这个是很多应用都会碰到的)。结构和UI的对应关系可能会经常变动的,而且结构中需要填写的内容不是所有都必须的,直接用关系型数据库来保存似乎不是太好的方案。首先想到就是用XML文件来保存这些信息了,而界面之间的信息显示是有关联的,可以说需要一个缓冲来记录这些信息,想到的就是定义一个类来记录和操作这些信息。这自然遇到一个问题,如何保持类和XML数据的同步呢?

        生成的XML文件需要给其它业务来做处理,格式上有一定的规范,对于类来说,信息都保存在字段或者属性那里的,并没有区别,但XML中可能有些信息是Attribute,有些又是Element的。如何灵活地处理这些关系呢?用与XML相关的属性来定义这些字段和类就可以了。

(1) 类信息序列化到XML文件中的代码

开发中灵活使用属性类        public void SerializeObject(string filename)
开发中灵活使用属性类        {
开发中灵活使用属性类            XmlSerializer s = new XmlSerializer(typeof(Tables));
开发中灵活使用属性类            TextWriter writer = new StreamWriter(filename);
开发中灵活使用属性类
开发中灵活使用属性类            Tables group = new Tables();
开发中灵活使用属性类            group.GroupID = 10.089f;
开发中灵活使用属性类            group.IsActive = false;     
开发中灵活使用属性类            group.HexBytes = new byte[1]{Convert.ToByte(100)};
开发中灵活使用属性类
开发中灵活使用属性类            Employee x = new Employee();
开发中灵活使用属性类            Employee y = new Employee();
开发中灵活使用属性类
开发中灵活使用属性类            x.Name = "Jack";
开发中灵活使用属性类            y.Name = "Jill";
开发中灵活使用属性类            
开发中灵活使用属性类            x.des2 = "Yyyy开发中灵活使用属性类";
开发中灵活使用属性类            x.colType = new ColumnType[2];
开发中灵活使用属性类            x.colType[0] = new ColumnType();
开发中灵活使用属性类            x.colType[1] = new ColumnType();
开发中灵活使用属性类            x.colType[0].linkField = "Haaa开发中灵活使用属性类";
开发中灵活使用属性类            x.colType[1].linkType = "Zzzz开发中灵活使用属性类";
开发中灵活使用属性类            y.Age[0] = 20;
开发中灵活使用属性类            y.Age[1] = 40;
开发中灵活使用属性类
开发中灵活使用属性类            y.Desc = "OK";
开发中灵活使用属性类      
开发中灵活使用属性类            group.Employees = new Employee[2]{x,y};
开发中灵活使用属性类
开发中灵活使用属性类            Manager mgr = new Manager();
开发中灵活使用属性类            mgr.Name = "Sara";
开发中灵活使用属性类            mgr.Level = 4;
开发中灵活使用属性类
开发中灵活使用属性类            mgr.Desc = "Yes";
开发中灵活使用属性类            mgr.aHead = true;
开发中灵活使用属性类            
开发中灵活使用属性类            group.Manager = mgr;
开发中灵活使用属性类             
开发中灵活使用属性类            group.ExtraInfo = new ArrayList();
开发中灵活使用属性类            group.ExtraInfo.Add(42);
开发中灵活使用属性类            group.ExtraInfo.Add("Answer");
开发中灵活使用属性类
开发中灵活使用属性类            s.Serialize(writer, group);
开发中灵活使用属性类            writer.Close();
开发中灵活使用属性类        }

开发中灵活使用属性类
(2) XML文件信息变成类的信息的代码
开发中灵活使用属性类        public void DeserializeObject(string filename)
开发中灵活使用属性类        {
开发中灵活使用属性类            FileStream fs = new FileStream(filename, FileMode.Open);
开发中灵活使用属性类            XmlSerializer x = new XmlSerializer(typeof(Tables));
开发中灵活使用属性类            Tables g = (Tables) x.Deserialize(fs);
开发中灵活使用属性类            int i=0;
开发中灵活使用属性类              foreach(Employee e in g.Employees)
开发中灵活使用属性类            {
开发中灵活使用属性类                i = i + 1;
开发中灵活使用属性类                Console.WriteLine("No {0} Name: {1}",i.ToString(),e.Name);
开发中灵活使用属性类            }

开发中灵活使用属性类
开发中灵活使用属性类            Console.WriteLine(g.Manager.Name);
开发中灵活使用属性类            Console.WriteLine(g.GroupID);
开发中灵活使用属性类            Console.WriteLine(g.HexBytes[0]);
开发中灵活使用属性类        }
(3)例子中相关类信息生成的XML文件
开发中灵活使用属性类<?xml version="1.0" encoding="utf-8"?>
开发中灵活使用属性类<Tables xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
开发中灵活使用属性类  <Member>
开发中灵活使用属性类    <Employee AHead="false">
开发中灵活使用属性类      <Name>Jack</Name>
开发中灵活使用属性类      <Age>0</Age>
开发中灵活使用属性类      <Age>0</Age>
开发中灵活使用属性类      <des2>Yyyy开发中灵活使用属性类</des2>
开发中灵活使用属性类      <ColumnType LinkField="Haaa开发中灵活使用属性类" />
开发中灵活使用属性类      <ColumnType LinkType="Zzzz开发中灵活使用属性类" />
开发中灵活使用属性类    </Employee>
开发中灵活使用属性类    <Employee Desc="OK" AHead="false">
开发中灵活使用属性类      <Name>Jill</Name>
开发中灵活使用属性类      <Age>20</Age>
开发中灵活使用属性类      <Age>40</Age>
开发中灵活使用属性类    </Employee>
开发中灵活使用属性类  </Member>
开发中灵活使用属性类  <Building>10.088999748229981</Building>
开发中灵活使用属性类  <HexBytes>64</HexBytes>
开发中灵活使用属性类  <IsActive>false</IsActive>
开发中灵活使用属性类  <Manager Desc="Yes" AHead="true">
开发中灵活使用属性类    <Name>Sara</Name>
开发中灵活使用属性类    <Age>0</Age>
开发中灵活使用属性类    <Age>0</Age>
开发中灵活使用属性类    <Level>4</Level>
开发中灵活使用属性类  </Manager>
开发中灵活使用属性类  <ObjectNumber>42</ObjectNumber>
开发中灵活使用属性类  <ObjectString>Answer</ObjectString>
开发中灵活使用属性类</Tables>
(4) XML总体框架如何用XML相关属性类描述
开发中灵活使用属性类    public class Tables
开发中灵活使用属性类    {
开发中灵活使用属性类        [XmlArray(ElementName = "Member")]
开发中灵活使用属性类        public Employee[] Employees;
开发中灵活使用属性类      
开发中灵活使用属性类        [XmlElement(DataType = "double",
开发中灵活使用属性类             ElementName = "Building")]
开发中灵活使用属性类        public double GroupID;
开发中灵活使用属性类
开发中灵活使用属性类        [XmlElement(DataType = "hexBinary")]
开发中灵活使用属性类        public byte [] HexBytes;
开发中灵活使用属性类
开发中灵活使用属性类
开发中灵活使用属性类        [XmlElement(DataType = "boolean")]
开发中灵活使用属性类        public bool IsActive;
开发中灵活使用属性类
开发中灵活使用属性类        [XmlElement(Type = typeof(Manager))]
开发中灵活使用属性类        public Employee Manager;
开发中灵活使用属性类
开发中灵活使用属性类        [XmlElement(typeof(int),
开发中灵活使用属性类             ElementName = "ObjectNumber"),
开发中灵活使用属性类        XmlElement(typeof(string),
开发中灵活使用属性类            ElementName = "ObjectString")]
开发中灵活使用属性类        public ArrayList ExtraInfo;
开发中灵活使用属性类    }
   
开发中灵活使用属性类
(5) 类的字段信息表示为属性的方法
开发中灵活使用属性类    public class Employee
开发中灵活使用属性类    {
开发中灵活使用属性类        public string Name;
开发中灵活使用属性类        [XmlElement(typeof(int))]
开发中灵活使用属性类        public int[] Age = new int[2];
开发中灵活使用属性类        [XmlAttribute(AttributeName = "Desc")]
开发中灵活使用属性类        public string Desc;
开发中灵活使用属性类        [XmlAttribute(AttributeName = "AHead")]
开发中灵活使用属性类        public bool aHead;
开发中灵活使用属性类
开发中灵活使用属性类        public string des2;
开发中灵活使用属性类        
开发中灵活使用属性类        [XmlElement(ElementName="ColumnType")]
开发中灵活使用属性类        public ColumnType[] colType;
开发中灵活使用属性类    }

开发中灵活使用属性类


2.自己根据需要定义自己的属性类,例如定义相关业务类和UI的关系。
在使用中,信息不是一个UI就完成的,可能经过了几个UI,或者几个页面去交互,但可能业务的调整,显示的位置或者一些参数可能需要调整的,这些UI的信息能否也定义为类的某些属性?
从.NET的Attribute派生一个客户定制类可以轻松做到这点,定制类只需要从System.Attribute派生即可。

开发中灵活使用属性类    [AttributeUsage(AttributeTargets.Field | AttributeTargets.Class,AllowMultiple=true)]
开发中灵活使用属性类    public class LayoutAttribute : Attribute
开发中灵活使用属性类    {
开发中灵活使用属性类        string m_desc;
开发中灵活使用属性类        public string OwnPage;
开发中灵活使用属性类        public string GetDesc()
开发中灵活使用属性类        {
开发中灵活使用属性类            return m_desc;
开发中灵活使用属性类        }

开发中灵活使用属性类
开发中灵活使用属性类        public LayoutAttribute(string strDesc)
开发中灵活使用属性类        {
开发中灵活使用属性类           m_desc = strDesc;
开发中灵活使用属性类        }

开发中灵活使用属性类
开发中灵活使用属性类    }

开发中灵活使用属性类

       AttributeUsage这个类定义了LayoutAttribute的一些基本信息,例如这里定义了LayoutAttribute适用于字段,类,并且允许重复定义的。

       在应用中,可以利用反射机制,将这些定义的信息获取,从而判断应该在界面上如何处理了。

开发中灵活使用属性类            Type myType = Type.GetType("testXmlAttr.Manager");
开发中灵活使用属性类            Console.WriteLine(myType.FullName);
开发中灵活使用属性类            FieldInfo myFieldInfo = myType.GetField("Level");
开发中灵活使用属性类            
开发中灵活使用属性类            object[] atts = myFieldInfo.GetCustomAttributes(false);            
开发中灵活使用属性类            for (int i=0;i<atts.Length;i++)
开发中灵活使用属性类                Console.WriteLine(((LayoutAttribute)atts[i]).OwnPage);
开发中灵活使用属性类

例子运行结果:(同时在当前目录下生成testXmlAttr.xml)
开发中灵活使用属性类


本文转自风前絮~~博客园博客,原文链接:http://www.cnblogs.com/windsails/archive/2004/09/08/41006.html,如需转载请自行联系原作者