attributes c#.net software development version 1.0
TRANSCRIPT
Attributes
C# .Net Software Development
Version 1.0
Overview
Attributes Provide Declarative Metadata
Must inherit from System.Attribute Conventions TestAttribute class ildasm utility
Copyright 2006-2008 © by Dennis A. Fairclough all rights reserved.
Attributes (C# Standard 1.2/3.0)
Contemporary software design increasingly relies on software components in the form of self-contained and self-describing packages of functionality.
Key to such components is that they present a programming model with properties, methods, and events; They have attributes that provide declarative information
about the component; and they incorporate their own documentation.
Attributes are such and provide declarative metadata
Copyright 2006-2008 © by Dennis A. Fairclough all rights reserved.
Declarative Information (Attributes)
C# generalizes this capability such that user-defined types of declarative information can be attached to program entities and retrieved at runtime. C# programs specify this additional declarative information by defining and using attributes.
Copyright 2006-2008 © by Dennis A. Fairclough all rights reserved.
System.Attribute
All attribute classes must derive from the System.Attribute base class provided by the .NET Framework.
If an attribute’s name ends in Attribute, that part of the name can be omitted when the attribute is referenced i.e. TestAttritbute or Test
Copyright 2006-2008 © by Dennis A. Fairclough all rights reserved.
Copyright 2006-2008 © by Dennis A. Fairclough all rights reserved.
What are Attributes? Declarative Information Provider (Decorator Pattern) Attributes encapsulate meta information about
assemblies types (classes, structs, enums, etc.) data and method members
Attributes can indicate Threading Model Security Access COM Access style How a type is to be used Serialization style Custom information
Attribute information is stored in the Assembly Manifest
An User-Defined Attribute Class
public class TestAttritbute : Attribute{
}
Copyright 2006-2008 © by Dennis A. Fairclough all rights reserved.
Example TestAttribute public class TestAttribute : Attribute { private string _Name; public string Name { get { return _Name; } set { _Name = value; } } private string _Mode; public string Mode { get { return _Mode; } set { _Mode = value; } } public TestAttribute(string name) { _Name = name; _Mode = "<none>"; } }//END class TestAttribute
Copyright 2006-2008 © by Dennis A. Fairclough all rights reserved.
Attribute Usage
[Test("Dennis", Mode="ReadOnly")] class Program { static void Main(string[] args) { MemberInfo member = typeof(Program); TestAttribute mattr = (TestAttribute)Attribute.GetCustomAttribute(member,typeof(TestAttribute) ) as TestAttribute; if (mattr == null) Console.WriteLine("No attribute information"); else Console.WriteLine("TestAttribute for: {0} with {1} and {2}",member,mattr.Name,mattr.Mode); Console.Read(); }//END Main() }//END class Program
Attribute (implied)
Attribute Property
Copyright 2006-2008 © by Dennis A. Fairclough all rights reserved.
ildasm (MSIL Dissassembler)
ildasm <filename.exe>
Copyright 2006-2008 © by Dennis A. Fairclough all rights reserved.
Copyright 2006-2008 © by Dennis A. Fairclough all rights reserved.
FlagsAttribute (Flags)
Used to modify the behavior of an enum Use reflector (or another reflection tool) to
view System.Enum Look at ToString (which has to test for the
FlagsAttribute)
(See Test_Attributes Demo)
Copyright 2006-2008 © by Dennis A. Fairclough all rights reserved.
Library Attributes CLSCompliantAttribute CategoryAttribute ToolboxBitmapAttribute FlagsAttribute SecurityAttribute WebMethodAttribute And many more.
Copyright 2006-2008 © by Dennis A. Fairclough all rights reserved.
Attribute Usage Attribute suffix can be omitted
[FlagsAttribute] [Flags]
The attribute tag is actually a constructor call [Flags( )] [PrincipalPermission(SecurityAction.Demand)] Note: You can omit the parens if there are no params
Named constructor parameters: Set Public property values
[PrincipalPermission( SecurityAction.Demand, Name="DEMO/demo", Role="User")]
Property accesses must follow actual parameters
Copyright 2006-2008 © by Dennis A. Fairclough all rights reserved.
Attribute Usage
Attributes can be used on Assemblies Types Data & Method Members
Attribute usage can be restricted to one type of entity See AttributeTargets enum
Copyright 2006-2008 © by Dennis A. Fairclough all rights reserved.
Assembly Level Attributes
In the AssemblyInfo.cs file
Copyright 2006-2008 © by Dennis A. Fairclough all rights reserved.
Querying Attribute Info
Use reflection to access attributes Type.GetCustomAttributes MemberInfo.GetCustomAttributes MethodInfo.GetCustomAttributes MemberInfo.IsDefined etc.
Copyright 2006-2008 © by Dennis A. Fairclough all rights reserved.
Custom Attribute Must ultimately inherit from System.Attribute Limit where the attribute can be used using the
AttributeUsageAttribute ValidOn (positional parameter)
AttributeTargets enum AllowMultiple (named parameter)
bool (default is false) Inherited (named parameter)
bool (default is true)
Copyright 2006-2008 © by Dennis A. Fairclough all rights reserved.
The Conditional Attribute
Allows you to define conditional methods Calls to this method are based upon whether
the conditional symbol is defined at the time of the call Parameter(s) are not evaluated if the call is not
made
Similar to #define DEBUG
Copyright 2006-2008 © by Dennis A. Fairclough all rights reserved.
ObsoleteAttribute
Tag an entity (Class, Struct, Method, etc.) to generate a compile-time warning or error if that entity is used.
Copyright 2006-2008 © by Dennis A. Fairclough all rights reserved.
Demo
See Attributes Example Code
Copyright 2006-2008 © by Dennis A. Fairclough all rights reserved.
What did you learn?
??