Presentation is loading. Please wait.

Presentation is loading. Please wait.

Attributes C#.Net Software Development Version 1.0.

Similar presentations


Presentation on theme: "Attributes C#.Net Software Development Version 1.0."— Presentation transcript:

1 Attributes C#.Net Software Development Version 1.0

2 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.

3 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.

4 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.

5 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.

6 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

7 An User-Defined Attribute Class public class TestAttritbute : Attribute { } Copyright 2006-2008 © by Dennis A. Fairclough all rights reserved.

8 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 = " "; } }//END class TestAttribute Copyright 2006-2008 © by Dennis A. Fairclough all rights reserved.

9 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.

10 ildasm (MSIL Dissassembler)  ildasm Copyright 2006-2008 © by Dennis A. Fairclough all rights reserved.

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

12 Copyright 2006-2008 © by Dennis A. Fairclough all rights reserved. Library Attributes  CLSCompliantAttribute  CategoryAttribute  ToolboxBitmapAttribute  FlagsAttribute  SecurityAttribute  WebMethodAttribute  And many more.

13 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

14 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

15 Copyright 2006-2008 © by Dennis A. Fairclough all rights reserved. Assembly Level Attributes  In the AssemblyInfo.cs file

16 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.

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

18 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

19 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.

20 Copyright 2006-2008 © by Dennis A. Fairclough all rights reserved. Demo See Attributes Example Code

21 Copyright 2006-2008 © by Dennis A. Fairclough all rights reserved. What did you learn?  ??


Download ppt "Attributes C#.Net Software Development Version 1.0."

Similar presentations


Ads by Google