1 / 17

Fun with Attributes for the Novice and Expert

Fun with Attributes for the Novice and Expert. Scott Reed Brain Hz Software scott@brainhzsoftware.com (760) 845-3320. It’s All About the Metadata. Compilation produces an assembly An assembly contains two things IL and Metadata It’s the metadata that makes possible:

maura
Download Presentation

Fun with Attributes for the Novice and Expert

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Fun with Attributesfor the Novice and Expert Scott Reed Brain Hz Software scott@brainhzsoftware.com (760) 845-3320

  2. It’s All About the Metadata • Compilation produces an assembly • An assembly contains two things • IL and Metadata • It’s the metadata that makes possible: • Serialization, Remoting, VS design of WinForms/WebForms/WPF, WCF / web services, interop, documentation, code generation, automated testing, etc.

  3. Attributes are classes • Any class which derives from Attribute • Typically they are named with an Attribute postfix • They are devoid of behavior • Just store information in the form of properties • Only useful in “3rd Party” scenarios • Up to you to define what 3rd party means • Typically not applied by the author of the class

  4. Applying an attribute • Can be applied on assemblies, classes, fields, parameters, etc. • unless disallowed by the attribute’s creator • In C# [MyAttribute] or [My] • In VB.NET <MyAttribute> or <My> • Gets stored as Metadata with whatever you are applying it on • Attribute parameters passed by position or name

  5. Assembly Loader Remoting Interop Xml Serialization Custom library Applying attributes [assembly: AssemblyVersion("1.0.0.0")] [Serializable] public class Person { [DllImport("my.dll")] static void Func([In]int x, [Out]ref int y); [XmlElement(“Nom")] public string Name; [Author("Brock", Comment="Age in years")] public int Age; }

  6. Consuming attributes • Write functions that take loosely typed arguments • Parameters often typed to just System.Object • Use IsDefined and GetCustomAttributes to look for type decoration • Change behavior based on how clients have decorated their classes

  7. The big picture [Foo] publicclass Person{ [Bar] publicstring Name; [Baz] publicint Age; } mycode.exe DoSomething(...) someutil.dll defines: Foo, Bar, Baz, DoSomething(object) metadata Reflection

  8. .NET defined attributes • There are lots of attributes • Misc • Serialization • WCF • Design • Data Annotations • MEF • etc.

  9. Misc (mscorlib) • [CLSCompliant] • [Flags] • [Obsolete] • [ThreadStatic] • [STAThread] • [SuppressMessage] • [Conditional]

  10. Serialization (.NET) • From System and System.Runtime.Serialization • [Serializable] • [NonSerialized] (on fields) • [OptionalField(VersionAdded=1)] • [OnSerializing, OnSerialized, OnDeserializing, OnDeserialized]

  11. Serialization (Xml) • From System.Xml.Serialization • [XmlAnyAttribute, XmlAnyElement, XmlArray, XmlArrayItem, XmlAttribute, XmlChoiceIdentifier, XmlElement, XmlEnum, XmlIgnore, XmlInclude, XmlNamespaceDeclarations, XmlRoot, XmlSchemaProvider, XmlSerializerAssembly, XmlSerializerVersion, XmlText, XmlType]

  12. WCF • System.ServiceModel [CallbackBehavior, FaultContract, MessageBodyMember, MessageContract, MessageHeader, OperationBehavior, OperationContract, ServiceBehavior, ServiceContract, ServiceKnownType, XmlSerializerFormat, TransactionFlow] • Sytem.Runtime.Serialization [CollectionDataContract, DataContract, DataMember, EnumMember, KnownType]

  13. Design • From System.ComponentModel • [Description, Category, DisplayName] • [Bindable, Browsable, ReadOnly] • [DefaultEvent, DefaultValue] • [Designer, Editor] • [TypeConverter, TypeDescriptionProvider]

  14. Data Annotations • .NET 3.5 SP1's DataAnnotations • RIA Services'sDataAnnotations • Silverlight's DataAnnotations • [Validation, DataType, MetadataType, DisplayFormat, UIHint, Range, RegularExpression, Required, StringLength] • [Display, CustomValidation, Editable, Key, FilterUIHint]

  15. Managed Extensibility Framework • Learns from Unity and PRISM • [Export, InheritedExport, ImportMany, ImportingConstructor, PartCreationPolicy, PartMetadata, PartNotDiscoverable, ExportMetadata, Import, MetadataAttribute]

  16. Other major areas of attributes • CAS • System.Configuration • System.EnterpriseServices • System.Runtime.InteropServices

  17. Thank You Scott Reed Brain Hz Software scott@brainhzsoftware.com (760) 845-3320 www.brainhzsoftware.com

More Related