attributter og reflection
Download
Skip this Video
Download Presentation
Attributter og Reflection

Loading in 2 Seconds...

play fullscreen
1 / 24

Attributter og Reflection - PowerPoint PPT Presentation


  • 54 Views
  • Uploaded on

Attributter og Reflection. Kilde: Joe Hummel, kursus i .Net, jan. 2003. Content.

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about ' Attributter og Reflection' - damali


An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
attributter og reflection

Attributter og Reflection

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

content
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
part 1
Part 1
  • Attributes…

Not to be confused with the attributes of a class!

attributes
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
where are attributes used
Where are attributes used?
  • Many technologies in .NET uses attributes:
    • CLR
    • ASP.NET
    • C# compiler
    • Visual Studio .NET
attributes example
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

objekt serialisation
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();

another example
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

.

.

.

conditional compiling the net way
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

.

.

.

custom attributes
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.
bugfix attributten
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);

}

}

brug af bugfix
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()

{

.

.

.

where are attributes stored
Where are attributes stored?
  • Attributes are stores inside the assembly as metadata

Component.dll

Assembly Manifest

assembly-specific metadata

[ AssemblyVersion("1.0.0.0") ]

Type Information

MyClass metadata

[ Serializable() ]

Method1 metadata

[ BugFix(...) ]

how find attributes
How find attributes?
  • Through reflection…
part 2
Part 2
  • Reflection…
reflection

System.Object

Assembly

AssemblyName

MemberInfo

ParameterInfo

System.Type

MethodBase

FieldInfo

PropertyInfo

EventInfo

MethodInfo

ConstructorInfo

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
who uses reflection
Who uses reflection?
  • Tool builders
  • Microsoft .NET:
    • CLR
    • ASP.NET
    • C# compiler
    • Visual Studio .NET
example
Example
  • Fetch an assembly and look for BugFix attributes
  • 3 steps:
    • Get the assembly is necessary.
    • Get information about each type (class) in the assembly
    • Look for the BugFix attribute…
step 1 get the assembly
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…>

step 2 get the types
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…>

step 3 look for bugfix attributes
Step 3: Look for BugFix attributes
  • Look at each type, as well as members of each type…

private void process(object[] attributes)

{

foreach (BugFixAttribute bugfix in attributes)

MessageBox.Show(bugfix.Author);

}

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

}

}

how strong is reflection
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)
summary
Summary
  • Attributes and reflection are strong concepts
    • declarative programming style
    • can be extended in subsystems
  • widely used in .NET…
references
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/
ad