Upload
brice-mcdowell
View
220
Download
0
Embed Size (px)
Citation preview
Fun with Attributesfor the Novice and Expert
Scott ReedBrain Hz Software
[email protected](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
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
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
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)
.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's DataAnnotations• 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 ReedBrain Hz [email protected](760) 845-3320www.brainhzsoftware.com