Presentation is loading. Please wait.

Presentation is loading. Please wait.

Fun with Attributes for the Novice and Expert Scott Reed Brain Hz Software (760) 845-3320.

Similar presentations


Presentation on theme: "Fun with Attributes for the Novice and Expert Scott Reed Brain Hz Software (760) 845-3320."— Presentation transcript:

1 Fun with Attributes for 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 “3 rd Party” scenarios –Up to you to define what 3 rd 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 or Gets stored as Metadata with whatever you are applying it on Attribute parameters passed by position or name

5 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; } [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; } InteropXml SerializationAssembly Loader Remoting Custom library

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 mycode.exe The big picture someutil.dll metadata [Foo] public class Person{ [Bar] public string Name; [Baz] public int Age; } [Foo] public class Person{ [Bar] public string Name; [Baz] public int Age; } DoSomething(...) Reflection defines: Foo, Bar, Baz, DoSomething(object)

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's DataAnnotations 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


Download ppt "Fun with Attributes for the Novice and Expert Scott Reed Brain Hz Software (760) 845-3320."

Similar presentations


Ads by Google