.Net Reflection Taipan Tamsare. Overview Reflection core concepts Exploring metadata Detail information Attributes Building Types at runtime.

Slides:



Advertisements
Similar presentations
Attributes Programming in C# Attributes CSE 494R (proposed course for 459 Programming in C#) Prof. Roger Crawfis.
Advertisements

Microsoft.NET Fundamentals. Introduction Name Company affiliation Title/function Job responsibility Database & Developer experience Your expectations.
OOP in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
C#.NET C# language. C# A modern, general-purpose object-oriented language Part of the.NET family of languages ECMA standard Based on C and C++
Advanced Object-Oriented Programming Features
C# Programming: From Problem Analysis to Program Design1 10 Advanced Object-Oriented Programming Features C# Programming: From Problem Analysis to Program.
Reflection Leveraging the Power of Metadata
5. OOP. 2 Microsoft Objectives “Classes, objects and object-oriented programming (OOP) play a fundamental role in.NET. C# features full support for the.
.NET Attributes and Reflection “What a developer needs to know……” Dan Douglas Blog:
OOP in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
(c) University of Washington03-1 CSC 143 Java Inheritance Reading: Ch. 10.
C# vs. C++ What's Different & What's New. An example C# public sometype myfn { get; set; } C++ public: sometype myfn { sometype get (); void set (sometype.
OOP Languages: Java vs C++
Programming Languages and Paradigms Object-Oriented Programming.
.NET Framework Introduction: Metadata
Introduction to .Net Framework
Advanced Java Programming Lecture 5 Reflection dr hab. Szymon Grabowski dr inż. Wojciech Bieniecki
The Metadata System1. 2 Introduction Metadata is data that describes data. Traditionally, metadata has been found in language- specific files (e.g. C/C++
.NET Framework & C#.
Lecture 8 Inheritance Richard Gesick. 2 OBJECTIVES How inheritance promotes software reusability. The concepts of base classes and derived classes. To.
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
Kalpesh Padia Reflection in.Net. OVERVIEW 9/19/
Reflection in.Net Siun-Wai Seow. Objective Explain.NET Reflection When to use it? How to use it? Topic is in the final exam.
C# Programming Fundamentals of Object-Oriented Programming Fundamentals of Object-Oriented Programming Introducing Microsoft.NET Introducing Microsoft.NET.
Distributed Systems (236351) Tutorial 1 - Getting Started with Visual Studio C#.NET.
.NET Framework Danish Sami UG Lead.NetFoundry
Programming Pillars Introduction to Object- Oriented Programming.
Introduction to C# C# is - elegant, type-safe, object oriented language enabling to build applications that run on the.NET framework - types of applications.
All types in the CLR are self-describing – CLR provides a reader and writer for type definitions System.Reflection & System.Reflection.emit – You can ‘read’
© FPT Software Advanced features in C# 1. © FPT Software Agenda Attributes Delegates & Events Anonymous Types & Dynamic Type Extension Methods Lambda.
Reflection Leveraging the Power of Metadata. Objectives Provide an introduction to.NET Reflection Explain how applications can use Reflection to explore.
C# D1 CSC 298 Elements of C# code (part 2). C# D2 Writing a class (or a struct)  Similarly to Java or C++  Fields: to hold the class data  Methods:
ABHISHEK BISWAS.NET Reflection Dynamically Create, Find and Invoke Types.
Effective C# 50 Specific Way to Improve Your C# Item 42, 43.
C# Programming: From Problem Analysis to Program Design1 10 Advanced Object-Oriented Programming Features C# Programming: From Problem Analysis to Program.
Reflection.NET Support for Reflection. What is Reflection Reflection: the process by which a program can observe and modify its own structure and behavior.
Module 14: Attributes. Overview Overview of Attributes Defining Custom Attributes Retrieving Attribute Values.
Inheritance. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
08 Encapsulation and Abstraction. 2 Contents Defining Abstraction Levels of Abstraction Class as Abstraction Defining a Java Class Instantiating a Class.
Attributes C#.Net Software Development Version 1.0.
Introduction to Object-Oriented Programming Lesson 2.
Object Oriented Software Development 4. C# data types, objects and references.
.NET Mobile Application Development XML Web Services.
Bruno Cabral “Reflection, Code Generation and Instrumentation in the.NET platform” University of Coimbra.
Building Custom Controls with ASP.NET and the Microsoft ®.NET Framework Rames Gantanant Microsoft Regional Director, Thailand
Delivering Excellence in Software Engineering ® EPAM Systems. All rights reserved. Reflection and Attributes.
C# Fundamentals An Introduction. Before we begin How to get started writing C# – Quick tour of the dev. Environment – The current C# version is 5.0 –
METADATA IN.NET Presented By Sukumar Manduva. INTRODUCTION  What is Metadata ? Metadata is a binary information which contains the complete description.
CHARLES UNIVERSITY IN PRAGUE faculty of mathematics and physics Advanced.NET Programming I 7 th Lecture Pavel Ježek
C# for C++ Programmers 1.
Creating and Using Objects, Exceptions, Strings
Classes (Part 1) Lecture 3
Q Reflection Nauzad Kapadia MGB 2003
Module 5: Common Type System
15: Object Object Programming C# © 2003 DevelopMentor, Inc. 12/1/2003.
Review: Two Programming Paradigms
Reflection SoftUni Team Technical Trainers C# OOP Advanced
Advanced .NET Programming I 7th Lecture
Microsoft .NET 3. Language Innovations Pan Wuming 2017.
Array Array is a variable which holds multiple values (elements) of similar data types. All the values are having their own index with an array. Index.
CS360 Windows Programming
.NET and .NET Core 5.2 Type Operations Pan Wuming 2016.
.NET and .NET Core 9. Towards Higher Order Pan Wuming 2017.
.NET and .NET Core 10. Enabling Contracts Pan Wuming 2017.
1.2 Key Concepts and Walkthroughs
CIS 199 Final Review.
5. 3 Coding with Denotations
Jim Fawcett CSE775 – Distributed Objects Spring 2006
Advanced .NET Programming I 8th Lecture
5. OOP OOP © 2003 Microsoft.
Presentation transcript:

.Net Reflection Taipan Tamsare

Overview Reflection core concepts Exploring metadata Detail information Attributes Building Types at runtime

What is reflection? Reflection is the feature in.Net, which enables us to get some information about object in runtime. 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

Exploring Metadata 6 System.Type Attributes Fields Properties Constructors Methods Events Parameters [serializable] public class Person : { public event OnSaveChange onsv; public Date DOB; public string FirstName; public string LastName; public string Name { get { return F..+ " " + L..; } } public void Person(string F.,string L.) { FirstName=First;LastName=Last; } public bool Save() { System.Type t = this.GetType() ; foreach( FieldInfo f in t.GetFields() ) {... } }

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() ) { … };

Reflection System.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 8

Example public class TestDataType { public TestDataType() { counter = 1; } public TestDataType(int c) { counter = c; } private int counter; public int Inc() { return counter++; } public int Dec() { return counter--; } } Type objectType = testObject.GetType(); ConstructorInfo [] info = objectType.GetConstructors(); MethodInfo [] methods = objectType.GetMethods(); // get all the constructors Console.WriteLine("Constructors:"); foreach( ConstructorInfo cf in info ) { Console.WriteLine(cf); } Console.WriteLine(); // get all the methods Console.WriteLine("Methods:"); foreach( MethodInfo mf in methods ) { Console.WriteLine(mf); } More exampleexample

Reflection MemberInfo Base class for all "member" element descriptions – Fields, Properties, Methods, etc. Provides member kind, name, and declaring class 10 MemberInfo MethodBaseParameterInfoFieldInfoEventInfoPropertyInfo MethodInfoConstructorInfo

Reflection Attributes Custom attributes are the killer-app for Reflection! Attributes enable declarative behavior Attributes allow data augmentation 11

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 12 Assembly Module Class Struct Constructor Method Field Class Interface Class Delegate Class Interface

Building Types at Runtime System.Reflection.Emit Full representation of physical structure Allows building modules and assemblies at run time – Transient code only used at run time – Persistent code for reuse Create classes and types, and emit IL Used by.NET compilers to build.NET apps Example 13

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

References Microsoft Assoc. Prof. Pan Wuming