天天看點

開發中靈活使用屬性類

什麼是屬性類?

        從抽象類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>

開發中靈活使用屬性類
開發中靈活使用屬性類

      <des2>Yyyy

開發中靈活使用屬性類

</des2>

開發中靈活使用屬性類

      <ColumnType LinkField="Haaa

開發中靈活使用屬性類

" />

開發中靈活使用屬性類

      <ColumnType LinkType="Zzzz

開發中靈活使用屬性類
開發中靈活使用屬性類

    </Employee>

開發中靈活使用屬性類

    <Employee Desc="OK" AHead="false">

開發中靈活使用屬性類

      <Name>Jill</Name>

開發中靈活使用屬性類

      <Age>20</Age>

開發中靈活使用屬性類

      <Age>40</Age>

開發中靈活使用屬性類
開發中靈活使用屬性類

  </Member>

開發中靈活使用屬性類

  <Building>10.088999748229981</Building>

開發中靈活使用屬性類

  <HexBytes>64</HexBytes>

開發中靈活使用屬性類

  <IsActive>false</IsActive>

開發中靈活使用屬性類

  <Manager Desc="Yes" AHead="true">

開發中靈活使用屬性類

    <Name>Sara</Name>

開發中靈活使用屬性類

    <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,如需轉載請自行聯系原作者