attributter og reflection

24
.Net Attributter og reflektion NOEA / PQC 2006 Attributter og Reflection Kilde: Joe Hummel, kursus i .Net, jan. 2003

Upload: damali

Post on 02-Feb-2016

18 views

Category:

Documents


0 download

DESCRIPTION

Attributter og Reflection. Kilde: Joe Hummel, kursus i .Net, jan. 2003. Content. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Attributter og Reflection

.Net Attributter og reflektion NOEA / PQC 2006

Attributter og Reflection

Kilde: Joe Hummel, kursus i .Net, jan. 2003

Page 2: Attributter og Reflection

2.Net Attributter og reflektion NOEA / PQC 2007

Content

“For some years, Microsoft's development tools have embraced a declarative programming style — programmer's declare their intent ("I need a database transaction"), and the underlying software provides the implementation ("BeginTrans … Commit"). .NET has formalized this style of programming in the form of attributes (declaring intent) and reflection (recognizing intent)…”

• Attributes • Reflection

Page 3: Attributter og Reflection

3.Net Attributter og reflektion NOEA / PQC 2007

Part 1

• Attributes…

Not to be confused with the attributes of a class!

Page 4: Attributter og Reflection

4.Net Attributter og reflektion NOEA / PQC 2007

Attributes

• Attributes is a general mechanism for outlining code– To support declarative programming– Be able to develop .NET in unforeseen directions

• We can define our own attributes• .NET contains many predefined attributs for:

– Transaction handling– Serialization– COM interaction– Version control and ‘digital signing’ of the assembly

Page 5: Attributter og Reflection

5.Net Attributter og reflektion NOEA / PQC 2007

Where are attributes used?

• Many technologies in .NET uses attributes:– CLR– ASP.NET– C# compiler– Visual Studio .NET

Page 6: Attributter og Reflection

6.Net Attributter og reflektion NOEA / PQC 2007

Attributes example

• Attributes are used for example in a webservice• Example:

– By specifying a method as [WebMethod], it turns into a web service in .NET

public class Service1 : System.Web.Services.WebService{ [WebMethod] public int Add(int x, int y) { return x + y; } . . .}

attribute

Page 7: Attributter og Reflection

7.Net Attributter og reflektion NOEA / PQC 2007

Objekt serialisation

• Add the System.Serializable() attribute and . NET offers support for serialisation– Here an object is serialized to an XML-file…

[ System.Serializable() ]public class Person{ public string name; public int age; . . . using System.Runtime.Serialization.Formatters.Soap;

using System.IO;

Person p;p = new Person(...);

FileStream fs = new FileStream(@"C:\person.xml", FileMode.Create, FileAccess.Write);SoapFormatter formatter = new SoapFormatter();formatter.Serialize(fs, p); // serialize to XML!fs.Close();

Page 8: Attributter og Reflection

8.Net Attributter og reflektion NOEA / PQC 2007

Another example

• Conditional compiling is a good tool for debugging– in DEBUG mode, assert / collect / emit debug info– in RELEASE mode, disable the debug things

• In the old fashion C way by using #if directives:

public class MyClass{ #if DEBUG private void DebugInfo() { << output debug info to screen, file, or ... >> } #endif

public void MyMethod() { #if DEBUG this.DebugInfo(); #endif . . .

Page 9: Attributter og Reflection

9.Net Attributter og reflektion NOEA / PQC 2007

Conditional compiling, the .NET way

• The .NET way used the Conditional attribute:– methods is included if the symbol is defined– Calls are made if the symbol is defined

public class MyClass{ [ System.Diagnostics.Conditional("DEBUG") ] private void DebugInfo() { << output debug info to screen, file, or ... >> }

public void MyMethod() { this.DebugInfo(); // call made only if DEBUG defined . . .

Page 10: Attributter og Reflection

10.Net Attributter og reflektion NOEA / PQC 2007

Custom attributes

• .NET can be extended with your own attributes

• Example:– a database with a bug reporter: bug #, dato, symptom– when the error is corrected, assign an attribute in the source

with developer id and bug #– write an app in C# for generating a rapport about:

• unresolved bugs• error corrections pr. person• error corrections in chronological order• etc.

Page 11: Attributter og Reflection

11.Net Attributter og reflektion NOEA / PQC 2007

BugFix attributten

• Inside the program attributes are just classes (like everything else :-)

[ AttributeUsage(AttributeTargets.All, AllowMultiple=true, Inherited=false) ] public class BugFixAttribute : System.Attribute{ public int BugNumber; public string Author; public DateTime TheDate;

// constructor public BugFixAttribute(int BugNumber, string Author, string DateStr) { this.BugNumber = BugNumber; this.Author = Author; this.TheDate = System.Convert.ToDateTime(DateStr); }}

Page 12: Attributter og Reflection

12.Net Attributter og reflektion NOEA / PQC 2007

Brug af BugFix

• Programmører kommenterer kode efterhånden som fejlrettelser implementeres…

[ BugFix(4423, "Jim Bag", "1 April 2002"), BugFix(5518, "Jane Doe", "28 June 2002") ]public class MyClass{ public MyClass() { . . . } [ BugFix(3004, "Kim Lee", "1 January 2002") ] public void SomeMethod() { . . .

Page 13: Attributter og Reflection

13.Net Attributter og reflektion NOEA / PQC 2007

Where are attributes stored?

• Attributes are stores inside the assembly as metadata

Component.dll

Assembly Manifest

Type Information

MyClass metadata[ Serializable() ]

assembly-specific metadata[ AssemblyVersion("1.0.0.0") ]

Method1 metadata[ BugFix(...) ]

Page 14: Attributter og Reflection

14.Net Attributter og reflektion NOEA / PQC 2007

How find attributes?

• Through reflection…

Page 15: Attributter og Reflection

15.Net Attributter og reflektion NOEA / PQC 2007

Part 2

• Reflection…

Page 16: Attributter og Reflection

16.Net Attributter og reflektion NOEA / PQC 2007

Reflection

• Reflection: to read metadata– With the reflection classes in FCL makes you able to reflect on

classes

– reflection library is in the System.Reflection namespace

– compiled into the mscorlib.dll assembly

System.Object

FieldInfo PropertyInfo EventInfoSystem.Type MethodBase

MethodInfo ConstructorInfo

ParameterInfoMemberInfoAssemblyNameAssembly

Page 17: Attributter og Reflection

17.Net Attributter og reflektion NOEA / PQC 2007

Who uses reflection?

• Tool builders• Microsoft .NET:

– CLR– ASP.NET– C# compiler– Visual Studio .NET

Page 18: Attributter og Reflection

18.Net Attributter og reflektion NOEA / PQC 2007

Example

• Fetch an assembly and look for BugFix attributes• 3 steps:

1. Get the assembly is necessary.

2. Get information about each type (class) in the assembly

3. Look for the BugFix attribute…

Page 19: Attributter og Reflection

19.Net Attributter og reflektion NOEA / PQC 2007

Step 1: Get the assembly

• There are different ways to retrieve the assembly for reflection:

using System.Reflection;

Assembly asm;string asmName = ...; // e.g. "Component"string fileName = ...; // e.g. @"C:\Bin\Component.dll"

asm = Assembly.GetExecutingAssembly(); // (1) current asm

asm = Assembly.Load(asmName); // (2) load by name

asm = Assembly.LoadFrom(pathName); // (3) load by file name

<cont'd on next page…>

Page 20: Attributter og Reflection

20.Net Attributter og reflektion NOEA / PQC 2007

Step 2: Get the types

• When the assembly is loaded it is easy to get the types:

Type[] types;types = asm.GetTypes(); // get types from assembly

Type attribute;attribute = Type.GetType("BugFixAttribute, BugFixAssembly");

foreach (Type t in types){ . . .

<cont'd on next page…>

Page 21: Attributter og Reflection

21.Net Attributter og reflektion NOEA / PQC 2007

Step 3: Look for BugFix attributes

• Look at each type, as well as members of each type…

object[] attributes;MemberInfo[] members;

foreach (Type t in types) {

attributes = t.GetCustomAttributes(attribute, false); process(attributes);

members = t.GetMembers(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static); foreach (MemberInfo m in members) { attributes = m.GetCustomAttributes(attribute, false); process(attributes); }}

private void process(object[] attributes){ foreach (BugFixAttribute bugfix in attributes) MessageBox.Show(bugfix.Author);}

Page 22: Attributter og Reflection

22.Net Attributter og reflektion NOEA / PQC 2007

How strong is reflection?

• Try these tools:

– FxCOP: checks assembly against design rules

– Anakrino: reverse engineering assembly —> C#

– Other possibilities:– dynamic call of methods (e.g. plugins)– dynamic generation of code (System.Reflection.Emit)

Page 23: Attributter og Reflection

23.Net Attributter og reflektion NOEA / PQC 2007

Summary

• Attributes and reflection are strong concepts– declarative programming style– can be extended in subsystems

• widely used in .NET…

Page 24: Attributter og Reflection

24.Net Attributter og reflektion NOEA / PQC 2007

References

• Books:– S. Lippman, "C# Primer"– J. Richter, "Applied Microsoft .NET Framework Programming"

• Web sites:– FxCOP: http://www.gotdotnet.com/team/fxcop/ – Anakrino: http://www.saurik.com/net/exemplar/