Reflection Leveraging the Power of Metadata. Objectives Provide an introduction to.NET Reflection Explain how applications can use Reflection to explore.

Slides:



Advertisements
Similar presentations
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 12 Introduction to ASP.NET.
Advertisements

Attributes Programming in C# Attributes CSE 494R (proposed course for 459 Programming in C#) Prof. Roger Crawfis.
CSE 1302 Lecture 8 Inheritance Richard Gesick Figures from Deitel, “Visual C#”, Pearson.
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++
C# Programming: From Problem Analysis to Program Design1 Advanced Object-Oriented Programming Features C# Programming: From Problem Analysis to Program.
ASP.NET Programming with C# and SQL Server First Edition
Reflection Leveraging the Power of Metadata
.NET Attributes and Reflection “What a developer needs to know……” Dan Douglas Blog:
A tour of new features introducing LINQ. Agenda of LINQ Presentation We have features for every step of the way LINQ Fundamentals Anonymous Functions/Lambda.
Java Beans Component Technology Integrated, Visual Development Environments Reusable Platform-Independent Modular.
OOP Languages: Java vs C++
Windows.Net Programming Series Preview. Course Schedule CourseDate Microsoft.Net Fundamentals 01/13/2014 Microsoft Windows/Web Fundamentals 01/20/2014.
Overview of Microsoft.Net and Vb.Net ITSE 2349 Spring 2002 Material from Microsoft.Net an Overview for ACC faculty by Stuart Laughton and Introduction.
A First Program Using C#
Lecture Roger Sutton CO530 Automation Tools 5: Class Libraries and Assemblies 1.
Programming Languages and Paradigms Object-Oriented Programming.
.NET Framework Introduction: Metadata
Introduction to .Net Framework
An Introduction to ASP.NET Ed Dunhill blogs.msdn.com/edunhill SLIDE7.
© 2008 Dr. Paul Walcott – The University of the West Indies: Cave Hill CampusDr. Paul Walcott COMP6325 Advanced Web Technologies Dr. Paul Walcott The University.
The Metadata System1. 2 Introduction Metadata is data that describes data. Traditionally, metadata has been found in language- specific files (e.g. C/C++
CIS NET Applications1 Chapter 2 –.NET Component- Oriented Programming Essentials.
.NET Framework & C#.
Lecture 8 Inheritance Richard Gesick. 2 OBJECTIVES How inheritance promotes software reusability. The concepts of base classes and derived classes. To.
Stimulsoft Reports.Net 20 Problems which Stimulsoft Reports.Net solves
Obsydian OLE Automation Ranjit Sahota Chief Architect Obsydian Development Ranjit Sahota Chief Architect Obsydian Development.
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.
Polymorphism, Inheritance Pt. 1 COMP 401, Fall 2014 Lecture 7 9/9/2014.
JavaBeans Components. To understand JavaBeans…  Proficient experience with the Java language required  Knowledge of classes and interfaces  Object-Oriented.
Managed C++. Objectives Overview to Visual C++.NET Concepts and architecture Developing with Managed Extensions for C++ Use cases Managed C++, Visual.
.NET Framework Danish Sami UG Lead.NetFoundry
Murach’s ASP.NET 4.0/VB, C1© 2006, Mike Murach & Associates, Inc.Slide 1.
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.
ABHISHEK BISWAS.NET Reflection Dynamically Create, Find and Invoke Types.
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.
1.
ASP.NET (Active Server Page) SNU OOPSLA Lab. October 2005.
Attributes C#.Net Software Development Version 1.0.
PerlNET: The Camel Talks.NET Jan Dubois The Perl Conference 6 San Diego, July 26 th 2002.
CS562 Advanced Java and Internet Application Introduction to the Computer Warehouse Web Application. Java Server Pages (JSP) Technology. By Team Alpha.
.NET Mobile Application Development XML Web Services.
Bruno Cabral “Reflection, Code Generation and Instrumentation in the.NET platform” University of Coimbra.
Reflection Programming under the hood SoftUni Team Technical Trainers Software University
July 22, 2001Introduction to.NET1 Introduction to.NET Framework Gholamali Semsarzadeh July 2001.
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 –
.Net Reflection Taipan Tamsare. Overview Reflection core concepts Exploring metadata Detail information Attributes Building Types at runtime.
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
INTRODUCTION BEGINNING C#. C# AND THE.NET RUNTIME AND LIBRARIES The C# compiler compiles and convert C# programs. NET Common Language Runtime (CLR) executes.
Windows Communication Foundation and Web Services
DotNetNuke® Web Application Framework
Q Reflection Nauzad Kapadia MGB 2003
Module 5: Common Type System
Jim Fawcett CSE681 – Software Modeling and Analysis Fall 2005
15: Object Object Programming C# © 2003 DevelopMentor, Inc. 12/1/2003.
Advanced .NET Programming I 7th Lecture
Microsoft .NET 3. Language Innovations Pan Wuming 2017.
CS360 Windows Programming
.NET and .NET Core 5.2 Type Operations Pan Wuming 2016.
Lecture 22 Inheritance Richard Gesick.
.NET and .NET Core 10. Enabling Contracts Pan Wuming 2017.
CIS 199 Final Review.
Quiz Points 1 Rules Raise your hand if you know the question
Jim Fawcett CSE775 – Distributed Objects Spring 2006
Advanced .NET Programming I 8th Lecture
Presentation transcript:

Reflection Leveraging the Power of Metadata

Objectives Provide an introduction to.NET Reflection Explain how applications can use Reflection to explore type information and how they can build types at runtime

Contents Section 1: Overview Section 2: Exploring Metadata Section 3: Detail Information Section 4: Building Types at Runtime Section 5: Putting it together Summary

Section 1: Overview Looking back: Automation and COM Type Info What's wrong with that?.NET Reflection Core Concepts

Looking Back: Automation TypeLibraries contain type information Exploration through ITypeLib/ITypeInfo Attributes describe behavior of elements e.g. Directionality of parameters e.g. Method role (instance method, property) Dynamic Invocation using IDispatch Type system supports most popular simple types Dynamic types using VARIANT Arrays passed using SAFEARRAY Strings expressed as BSTR

What's wrong with that? Automation client model favors VisualBasic Automation server model favors C/C++ ITypeInfo and IDispatch are way too complicated Information model is limited No sufficient support for complex types SAFEARRAYs are a pain from C/C++ BSTRs are difficult to manage from C/C++ Type information separate from code

.NET 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 Types' 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

Section 2: Exploring Metadata Who are you? What are you? Anything special about you? Now tell me what you have! Types and Instances

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

Who are you? 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() ) { … };

System.Type Access to meta-data 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

What are you? Value, Interface or Class? IsValueType, IsInterface, IsClass Public, Private or Sealed ? IsNotPublic, IsSealed Abstract or Implementation? IsAbstract Covers all possible properties of a managed type Very intuitive API, no "Parameter Hell"

Anything special about you? Special Memory Layout? IsAutoLayout IsExplicitLayout IsLayoutSequential COM Objects? IsCOMObject More… IsUnicodeClass IsSpecialName, etc.

Now tell me what you have! Finding and Exploring Members MemberInfo: GetMembers(), FindMembers() Exploring Fields and Properties FieldInfo: GetFields(), PropertyInfo: GetProperties() Exploring Constructors, Methods and Events GetConstructors(), GetMethods(), GetEvents() Exploring attributes, determining implemented interfaces, enumerating nested types, … Summary: Everything you may ever want to know

Type and Instances Type Safety First! Type checking at runtime C#: if ( o is Customer ) { … } VB: If TypeOf o Is Customer Then … End If Dynamic Invocation through Reflection Support for late binding MethodInfo.Invoke() FieldInfo.SetValue() PropertyInfo.SetValue()

Section 3: Detail Information MemberInfo FieldInfo, PropertyInfo ConstructorInfo, MethodInfo

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

FieldInfo, PropertyInfo FieldInfo Field data type and attributes Static or Instance field, protection level Can set value through reflection Provides low-level, direct access with SetValueDirect() PropertyInfo Property type and attributes Test methods for readability, writeability "get" and "set" MethodInfo Indexer ParameterInfo Can invoke "set" and "get" through Reflection

MethodInfo, ConstructorInfo MethodInfo Return type and attributes List of all parameters as ParameterInfo array Detail implementation information through flag-field Can invoke method through Reflection ConstructorInfo Same features as MethodInfo, just for constructors

Attributes Custom attributes are the killer-app for Reflection! Attributes enable declarative behavior Attributes allow data augmentation [dbcolumn("Address1")] string Street; [dbcolumn("Postcode")] string ZIP; [serializable] class Person {... Map fields to database columns with FieldInfo.GetCustomAttributes() Mark class as serializable Type.GetCustomAttributes()

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

The Unmanaged Spy: Metadata API Unmanaged (COM) Version of Reflection Used by VisualStudio.NET and Compilers Full Access to all Reflection Information for Tools Fully documented in the "Tool Developer's Guide" Buddy: Assembly Metadata API Reads/writes Assembly Manifests

Section 4: Building Types at Runtime Introducing System.Reflection.Emit Why? Some scenarios Dynamic Modules and Assemblies Creating Types and Classes Writing IL

Introducing System.Reflection.Emit Full representation of physical structure Allows building modules and assemblies at runtime Transient code only used at runtime Persistent code for reuse Create classes, types and emit IL Used by.NET compilers to build.NET apps

Why? Some Scenarios… Build classes dynamically from script-like code ASP.NET, Regular Expressions do just that ! Generate code from visual development tools e.g. build interfaces, base classes from UML Create dynamic wrappers for existing code Transfer code-chunks to remote machines Distributed processing scenarios (like

Building Assemblies System.Reflection.Emit.AssemblyBuilder Dynamically create new assemblies Create manifest and resources Add modules Specify security requirements Can be persisted as files or held in memory Act and behave like any other assembly

Building Modules System.Reflection.Emit.ModuleBuilder Modules contain types, classes and code Allows creating fully functional modules with code Can associate source code Emit debug symbols Acts like any module created by any.NET compiler

Writing IL Emit accepts IL (Intermediate Language) code Same code as emitted by C#, VB.NET, Eiffel# IL is optimized and translated into machine code Reflection.Emit puts you "on-par" Same backend as.NET compilers Same access to code-generation Languages are mostly scanners and parsers For more info see System.CodeDOM

Section 5: Putting It Together VisualStudio.NET and Reflection What ASP+ really does with a page

VisualStudio.NET and Reflection Component Class Description DefaultValue Help Localizable ReadOnly ComponentModel Attributes Toolbox Properties Window Designers Help

What ASP.NET does with a Page ASP.NET compiler Reflection Page Assembly IIS ASP.NET runtime 1 2 2a 3 file exists? assembly exists, same timestamp? no? compile run class pg : Page {... }

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

Questions?