ABHISHEK BISWAS.NET Reflection Dynamically Create, Find and Invoke Types.

Slides:



Advertisements
Similar presentations
Generating Data Access Assemblies with IronRuby Rob Rowe Blog: rob-rowe.blogspot.com.
Advertisements

What iS RMI? Remote Method Invocation. It is an approach where a method on a remote machine invokes another method on another machine to perform some computation.
.NET IL Obfuscation Presented by: Sarath Chandra Dorbala.
The Type System1. 2.NET Type System The type system is the part of the CLR that defines all the types that programmers can use, and allows developers.
Persistent State Service 1 CORBA Component  Component model  Container programming model  Component implementation framework  Component packaging and.
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++
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.
Windows.Net Programming Series Preview. Course Schedule CourseDate Microsoft.Net Fundamentals 01/13/2014 Microsoft Windows/Web Fundamentals 01/20/2014.
Lecture Roger Sutton CO530 Automation Tools 5: Class Libraries and Assemblies 1.
Advanced Java Programming Lecture 5 Reflection dr hab. Szymon Grabowski dr inż. Wojciech Bieniecki
ISYS 512 Business Application Design and Development with.Net David Chao.
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#.
Kalpesh Padia Reflection in.Net. OVERVIEW 9/19/
Presenter: PhuongNQK. Goals Provide you insights into core concepts of.NET framework  Assembly  Application domain  MSIL.
Reflection in.Net Siun-Wai Seow. Objective Explain.NET Reflection When to use it? How to use it? Topic is in the final exam.
ISYS 573 Special Topic – VB.Net David Chao. The History of VB Early 1960s:BASIC-Beginner’s All-Purpose Symbolic Instruction Code –Teaching –Simple syntax,
.NET Framework Danish Sami UG Lead.NetFoundry
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.
Course contents Basic concepts –What is software architecture ? Fundamental architectural styles –Pipes and filters –Layers –Blackboard –Event-driven Architectural.
Reflection Leveraging the Power of Metadata. Objectives Provide an introduction to.NET Reflection Explain how applications can use Reflection to explore.
Effective C# 50 Specific Way to Improve Your C# Item 42, 43.
SCALABLE EVOLUTION OF HIGHLY AVAILABLE SYSTEMS BY ABHISHEK ASOKAN 8/6/2004.
Reflection.NET Support for Reflection. What is Reflection Reflection: the process by which a program can observe and modify its own structure and behavior.
CS 501: Software Engineering Fall 1999 Lecture 12 System Architecture III Distributed Objects.
Module 14: Attributes. Overview Overview of Attributes Defining Custom Attributes Retrieving Attribute Values.
Attributes C#.Net Software Development Version 1.0.
Object Oriented Software Development 4. C# data types, objects and references.
CSCE 314 Programming Languages Reflection Dr. Hyunyoung Lee 1.
Reflection Bibliografie: Sun: The Java Tutorials – The Reflection API IBM developerWorks:
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 32 JavaBeans and Bean.
.NET Mobile Application Development XML Web Services.
Bruno Cabral “Reflection, Code Generation and Instrumentation in the.NET platform” University of Coimbra.
ISYS 512 Business Application Design and Development with.Net David Chao.
MIDDLE WARE TECHNOLOGIES B.TECH III YR II SEMESTER UNIT 4 PPT SLIDES TEXT BOOKS: 1.Client/Server programming with Java and CORBA Robert Orfali and Dan.
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
Topic 4: Distributed Objects Dr. Ayman Srour Faculty of Applied Engineering and Urban Planning University of Palestine.
INTRODUCTION BEGINNING C#. C# AND THE.NET RUNTIME AND LIBRARIES The C# compiler compiles and convert C# programs. NET Common Language Runtime (CLR) executes.
Course contents Basic concepts Fundamental architectural styles
NESTED CLASSES REFLECTION PROXIES.
/* LIFE RUNS ON CODE*/ Konstantinos Pantos Microsoft MVP ASP.NET
Programming with C# and .NET.
Q Reflection Nauzad Kapadia MGB 2003
15: Object Object Programming C# © 2003 DevelopMentor, Inc. 12/1/2003.
Advanced .NET Programming I 8th Lecture
Reflection SoftUni Team Technical Trainers C# OOP Advanced
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.
Chapter 3 The .NET Framework Class Library (FCL)
.NET and .NET Core 10. Enabling Contracts Pan Wuming 2017.
What is Reflection? Some Definitions….
What is Reflection? Some Definitions….
Jim Fawcett CSE775 – Distributed Objects Spring 2006
C# COM Interoperability
Advanced .NET Programming I 9th Lecture
Advanced .NET Programming I 8th Lecture
Presentation transcript:

ABHISHEK BISWAS.NET Reflection Dynamically Create, Find and Invoke Types

References Video: uq6Ur7Dchttps:// uq6Ur7Dc

.NET Reflection Features  Create and Extend types, modules & assemblies at runtime  Read type metadata at runtime  Invoke type methods at runtime  Language Independent Advantages  Single location for type information and code  Code is contained within type information  Every.NET object can be queried for its type

Metadata Components

Reflection at Work Assembly assm = appDomain.Load(buffer); Type[] types = assm.GetTypes(); foreach (Type type in types) {Console.WriteLine(type.FullName); } MethodInfo myMethod = assm.GetType("HW3.hashKey"). GetMethod("GenHash"); object obj = Activator.CreateInstance (assm.GetType("HW3.hashKey")); myMethod.Invoke(obj, null);

GetType() Function Breakdown Member of System.Object  Parent of all.NET classes  Available on every.NET class & simple type Returns System.Type object 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 Allows drilling down into all facets of a type Category: Simple, Enum, Struct or Class  IsValueType, IsInterface, IsClass  IsNotPublic, IsSealed  IsAbstract Methods and Constructors, Parameters and Return  MemberInfo: GetMembers(), FindMembers()  FieldInfo: GetFields(), PropertyInfo: GetProperties()  GetConstructors(), GetMethods(), GetEvents() Fields and Properties, Arguments and Attributes Events, Delegates and Namespaces

Invoking Methods Dynamic Invocation through Reflection Support for late binding  MethodInfo.Invoke()  FieldInfo.SetValue()  PropertyInfo.SetValue()

Creating a New Type/Module/Assembly Namespace “System.Reflection.Emit” Dim aName As New AssemblyName("DynamicAssemblyExample") Dim ab As AssemblyBuilder = AppDomain.CurrentDomain.DefineDynamicAssembly(aName, AssemblyBuilderAccess.RunAndSave) Dim mb As ModuleBuilder = ab.DefineDynamicModule(aName.Name, aName.Name & ".dll") Dim tb As TypeBuilder = mb.DefineType("MyDynamicType", TypeAttributes.Public)

Adding Field and Constructor Dim fbNumber As FieldBuilder = tb.DefineField("m_number“, GetType(Integer), FieldAttributes.Private) Dim parameterTypes() As Type = { GetType(Integer) } Dim ctor1 As ConstructorBuilder = tb.DefineConstructor( MethodAttributes.Public, CallingConventions.Standard, parameterTypes) Dim ctor1IL As ILGenerator = ctor1.GetILGenerator() ctor1IL.Emit(OpCodes.Ldarg_0) ctor1IL.Emit(OpCodes.Call,GetType(Object).GetConstructor(Type.E mptyTypes)) ctor1IL.Emit(OpCodes.Ldarg_0) ctor1IL.Emit(OpCodes.Ldarg_1) ctor1IL.Emit(OpCodes.Stfld, fbNumber) ctor1IL.Emit(OpCodes.Ret)

Adding Method Dim meth As MethodBuilder = tb.DefineMethod("MyMethod", MethodAttributes.Public, GetType(Integer), New Type(){GetType(Integer)}) Dim methIL As ILGenerator = meth.GetILGenerator() methIL.Emit(OpCodes.Ldarg_0) methIL.Emit(OpCodes.Ldfld, fbNumber) methIL.Emit(OpCodes.Ldarg_1) methIL.Emit(OpCodes.Mul) methIL.Emit(OpCodes.Ret)

Finishing Dim t As Type = tb.CreateType() ab.Save(aName.Name & ".dll") Dim mi As MethodInfo = t.GetMethod("MyMethod") Dim pi As PropertyInfo = t.GetProperty("Number")

Why? 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

References potsdam.de/teaching/componentVl05/slides/Net_V L2_02_Reflection.pdf potsdam.de/teaching/componentVl05/slides/Net_V L2_02_Reflection.pdf us/library/system.reflection.emit.assemblybuilder% 28v=VS.100%29.asp us/library/system.reflection.emit.assemblybuilder% 28v=VS.100%29.asp