170 likes | 373 Views
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:
E N D
Fun with Attributesfor 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: • Serialization, Remoting, VS design of WinForms/WebForms/WPF, WCF / web services, interop, documentation, code generation, automated testing, etc.
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
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
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; }
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
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
.NET defined attributes • There are lots of attributes • Misc • Serialization • WCF • Design • Data Annotations • MEF • etc.
Misc (mscorlib) • [CLSCompliant] • [Flags] • [Obsolete] • [ThreadStatic] • [STAThread] • [SuppressMessage] • [Conditional]
Serialization (.NET) • From System and System.Runtime.Serialization • [Serializable] • [NonSerialized] (on fields) • [OptionalField(VersionAdded=1)] • [OnSerializing, OnSerialized, OnDeserializing, OnDeserialized]
Serialization (Xml) • From System.Xml.Serialization • [XmlAnyAttribute, XmlAnyElement, XmlArray, XmlArrayItem, XmlAttribute, XmlChoiceIdentifier, XmlElement, XmlEnum, XmlIgnore, XmlInclude, XmlNamespaceDeclarations, XmlRoot, XmlSchemaProvider, XmlSerializerAssembly, XmlSerializerVersion, XmlText, XmlType]
WCF • System.ServiceModel [CallbackBehavior, FaultContract, MessageBodyMember, MessageContract, MessageHeader, OperationBehavior, OperationContract, ServiceBehavior, ServiceContract, ServiceKnownType, XmlSerializerFormat, TransactionFlow] • Sytem.Runtime.Serialization [CollectionDataContract, DataContract, DataMember, EnumMember, KnownType]
Design • From System.ComponentModel • [Description, Category, DisplayName] • [Bindable, Browsable, ReadOnly] • [DefaultEvent, DefaultValue] • [Designer, Editor] • [TypeConverter, TypeDescriptionProvider]
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]
Managed Extensibility Framework • Learns from Unity and PRISM • [Export, InheritedExport, ImportMany, ImportingConstructor, PartCreationPolicy, PartMetadata, PartNotDiscoverable, ExportMetadata, Import, MetadataAttribute]
Other major areas of attributes • CAS • System.Configuration • System.EnterpriseServices • System.Runtime.InteropServices
Thank You Scott Reed Brain Hz Software scott@brainhzsoftware.com (760) 845-3320 www.brainhzsoftware.com