Upload
zubairdar6
View
64
Download
1
Embed Size (px)
Citation preview
.Net ReflectionNabell (BCSF12M060)
Sheraz Manzoor (BCSF13M009)
Arham Butt (BCSF13M025)
Farhan (BCSF13M058)
Zubair Dar (BCSF13M065)
Overview
• Reflection core concepts• Exploring metadata• Detail information• Attributes• Building Types at runtime
What is reflection?
Is the ability to of instpecting an assemblies metadata at run time. It is used to find all types in an assembly and/or dynamically invoke methods in an assembly
Use Of Reflection
1. When you drag and drop a button on a win forms or an asp.net application. The properties windows use the reflection to show all the properties of the button class. So reflection is extensively use by IDE or UI designers
2. Late binding can be achieved by using Reflection
Information can be:• Data of the class
• Names of the methods • Constructors of that object
Why reflection
• explore assembly metadata• creating objects dynamically• invoking methods dynamically• write “generic” code that works with
different types
• implement sophisticated programming techniques
Reflection Core Concepts
Metadata Single location for type information and code Code is literally contained within type information Every .NET object can be queried for its type Type metadata can be explored with Reflection
Dynamic Type System Highly dynamic and language independent Types may be extended and built at run time Allows on-the-fly creation of assemblies .NET Compilers use .NET to emit .NET code
• Accessing meta-data: System.Object.GetType()– All .NET classes (implicitly) inherit System.Object– Available on every .NET class; simple types too
• Explicit language support for type meta-data– C#, JScript.NET: typeof(…)– VB.NET: If TypeOf … Is … Then …
• Determining Type Identity– Types have unique identity across any assembly– Types can be compared for identity
• if ( a.GetType() == b.GetType() ) { … };
ReflectionReflectionSystem.TypeSystem.Type
• Provides access to metadata for any .NET type• Returned by System.Object.GetType()• Allows drilling down into all facets of a type– Category: Simple, Enum, Struct or Class– Methods and Constructors, Parameters and Return– Fields and Properties, Arguments and Attributes– Events, Delegates, and Namespaces
7
ExampleExample
public class customer { public int id { get; set; } public string name { get; set; }
public customer() { id = -1; name = string.Empty; } public customer(int id, string name) {this.id = id; this.name = name; } public void printid() { Console.WriteLine("id is {0}",this.id);} public void printname() {Console.WriteLine("Name is {0}",this.name);} }
class Program { static void Main(string[] args) { } }
Type t = Type.GetType("csharp.customer");//static method on type clss customer c = new customer();
Console.WriteLine("Full Name {0}", t.FullName); Console.WriteLine(" Name {0}", t.Name); Console.WriteLine("Namespace {0}", t.Namespace); Console.WriteLine();
In main class
Type t = Type.GetType("csharp.customer");
Console.WriteLine("Full Name {0}", t.FullName); Console.WriteLine(" Name {0}", t.Name); Console.WriteLine("Namespace {0}", t.Namespace); Console.WriteLine();
In main class( Get Propeties)
Console.WriteLine("Methods in customers"); MethodInfo[] methods = t.GetMethods(); foreach (MethodInfo method in methods) { Console.WriteLine(method.ReturnType + " " + method.Name); } Console.WriteLine();
In main class(Get Methods)
In main class(Get Constructor)
Console.WriteLine("constructor in customers"); ConstructorInfo[] constructors = t.GetConstructors(); foreach (ConstructorInfo constructor in constructors) { Console.WriteLine(constructor.ToString());//Console.WriteLine(constructor.Name);//it dsnt give any info }
ReflectionAttributes
• Custom attributes are the killer-app for Reflection!
• Attributes enable declarative behavior• Attributes allow data augmentation
13
Reflection The Bigger Picture
• Types know their module; modules know their types• Modules know their assembly and vice versa• Code can browse and search its entire context
14
Assembly
Module Module ModuleClass Struct
Constructor
Method
Method
Field
Class
Interface
Class
Delegate
Class
Interface
Interface
Summary
• Reflection = System.Type + GetType()
• Explore Type Information at Runtime
• Enables Attribute-Driven Programming
• Use Emit Classes to Produce .NET Assemblies
• Bottom Line: Fully Self-Contained Structural Model