17
Fun with Attributes for the Novice and Expert Scott Reed Brain Hz Software [email protected] (760) 845-3320

Fun with Attributes for the Novice and Expert Scott Reed Brain Hz Software [email protected] (760) 845-3320

Embed Size (px)

Citation preview

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

Fun with Attributesfor the Novice and Expert

Scott ReedBrain Hz Software

[email protected](760) 845-3320

Page 2: 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:– Serialization, Remoting, VS design of

WinForms/WebForms/WPF, WCF / web services, interop, documentation, code generation, automated testing, etc.

Page 3: Fun with Attributes for the Novice and Expert Scott Reed Brain Hz Software scott@brainhzsoftware.com (760) 845-3320

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

Page 4: Fun with Attributes for the Novice and Expert Scott Reed Brain Hz Software scott@brainhzsoftware.com (760) 845-3320

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

Page 5: Fun with Attributes for the Novice and Expert Scott Reed Brain Hz Software scott@brainhzsoftware.com (760) 845-3320

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;}

Interop

Xml Serialization

Assembly LoaderRemoting

Custom library

Page 6: Fun with Attributes for the Novice and Expert Scott Reed Brain Hz Software scott@brainhzsoftware.com (760) 845-3320

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

Page 7: Fun with Attributes for the Novice and Expert Scott Reed Brain Hz Software scott@brainhzsoftware.com (760) 845-3320

mycode.exemycode.exe

The big picture

someutil.dllsomeutil.dllmetadata

[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)

Page 8: Fun with Attributes for the Novice and Expert Scott Reed Brain Hz Software scott@brainhzsoftware.com (760) 845-3320

.NET defined attributes

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

Page 9: Fun with Attributes for the Novice and Expert Scott Reed Brain Hz Software scott@brainhzsoftware.com (760) 845-3320

Misc (mscorlib)

• [CLSCompliant]• [Flags]• [Obsolete]• [ThreadStatic]• [STAThread]• [SuppressMessage]• [Conditional]

Page 10: Fun with Attributes for the Novice and Expert Scott Reed Brain Hz Software scott@brainhzsoftware.com (760) 845-3320

Serialization (.NET)

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

OnDeserialized]

Page 11: Fun with Attributes for the Novice and Expert Scott Reed Brain Hz Software scott@brainhzsoftware.com (760) 845-3320

Serialization (Xml)

• From System.Xml.Serialization• [XmlAnyAttribute, XmlAnyElement,

XmlArray, XmlArrayItem, XmlAttribute, XmlChoiceIdentifier, XmlElement, XmlEnum, XmlIgnore, XmlInclude, XmlNamespaceDeclarations, XmlRoot, XmlSchemaProvider, XmlSerializerAssembly, XmlSerializerVersion, XmlText, XmlType]

Page 12: Fun with Attributes for the Novice and Expert Scott Reed Brain Hz Software scott@brainhzsoftware.com (760) 845-3320

WCF• System.ServiceModel

[CallbackBehavior, FaultContract, MessageBodyMember, MessageContract, MessageHeader, OperationBehavior, OperationContract, ServiceBehavior, ServiceContract, ServiceKnownType, XmlSerializerFormat, TransactionFlow]

• Sytem.Runtime.Serialization

[CollectionDataContract, DataContract, DataMember, EnumMember, KnownType]

Page 13: Fun with Attributes for the Novice and Expert Scott Reed Brain Hz Software scott@brainhzsoftware.com (760) 845-3320

Design

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

Page 14: Fun with Attributes for the Novice and Expert Scott Reed Brain Hz Software scott@brainhzsoftware.com (760) 845-3320

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]

Page 15: Fun with Attributes for the Novice and Expert Scott Reed Brain Hz Software scott@brainhzsoftware.com (760) 845-3320

Managed Extensibility Framework

• Learns from Unity and PRISM• [Export, InheritedExport, ImportMany,

ImportingConstructor, PartCreationPolicy, PartMetadata, PartNotDiscoverable, ExportMetadata, Import, MetadataAttribute]

Page 16: Fun with Attributes for the Novice and Expert Scott Reed Brain Hz Software scott@brainhzsoftware.com (760) 845-3320

Other major areas of attributes

• CAS• System.Configuration• System.EnterpriseServices• System.Runtime.InteropServices

Page 17: Fun with Attributes for the Novice and Expert Scott Reed Brain Hz Software scott@brainhzsoftware.com (760) 845-3320

Thank You

Scott ReedBrain Hz [email protected](760) 845-3320www.brainhzsoftware.com