SlideShare a Scribd company logo
1 of 43
 
Understanding Reflection  Tamir Khason Technology Consultor ¹ mPrest Systems http://blogs.microsoft.co.il/blogs/tamir   Session Code: DVP307 ¹ con·sul·tor  (kən sult ′ ər) - one of a group of priests appointed to advise and assist a bishop
CLR – compilation and execution Execution Compilation Source code Language compiler JIT compiler Before installation or the first time each method is called MSIL Metadata Code Native code
Reflection overview ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
What is “Reflection”? ,[object Object],[object Object],[object Object],[object Object]
Where is “Reflection”? ,[object Object],[object Object],[object Object]
How to “Reflection”? ,[object Object],[object Object],[object Object],[object Object]
How to… perform type discovery ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
What are you? ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
What you have? ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
MemberInfo ,[object Object],[object Object],[object Object],MemberInfo MethodBase ParameterInfo FieldInfo EventInfo PropertyInfo MethodInfo ConstructorInfo
MethodInfo vs. ConstructorInfo ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
FieldInfo vs. PropertyInfo ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
We can clone it… Type kidType = oldKid.GetType(); Kid newKid = new Kid();   foreach (var m in kidType.GetProperties()) { m.SetValue(newKid ,    m.GetValue(oldKid, null), null); } Unknown object GetType().GetMembers() For Each Member FieldInfo.Copy old value to new Object
Clone optimization Kid CloneKid(Kid) { newKid.Height = oldKid.Height   newKid.Weight = oldKid.Weight } Unknown object GetType().GetMembers() For Each Member Create Code that copies members Save as Method for later Invocation
Clone optimized Unknown object Has custom method? Execute Yes Copy Members No
What is… attributes ,[object Object],[object Object],[object Object],[object Object]
Code generation ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],Kid CloneKid(Kid) { newKid.Height = oldKid.Height   newKid.Weight = oldKid.Weight }
 
CodeDOM ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],public static void Main() { System.Console.WriteLine(“Hello World!”); }
CodeDOM ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
CodeDOM – even more ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
[object Object],[object Object],[object Object]
CodeDOM – Bottom line ,[object Object],[object Object],[object Object]
 
IL introduction ,[object Object],[object Object],[object Object],[object Object],5 + 10 - 3    IL_0001:  ldc.i4  5 IL_0002:  ldc.i4  10  IL_0003:  add IL_0004:  ldc.i4 3 IL_0005:  sub Evaluation Stack 0000 005 0000 0010 0000 0015 0000 0003 0000 0012
IL introduction ,[object Object],[object Object],[object Object],string str = “Hello, World!” IL_0001: ldstr  “Hello, World!” IL_0002:  stloc str IL_0003:  ldloc str  Foo.f = “Hello, World!” IL_0001:  ldstr  “Hello, World!” IL_0002:  stfld string  Foo::f IL_0003:  ldfld string  Foo::f
IL introduction ,[object Object],if (b) { ... } else { ... }  IL_0001:  ldloc b IL_0002:  ldc.i4.1 IL_0003:  ceq IL_0004:  brfalse  IL_00020 // if true statements IL_0010:  ... IL_0011: br  IL_00025 // else statements IL_0020:  ... // rest of method IL_0025: ...
Reflection.Emit ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Code Emitting public  class MyClass { public static void Main()  { string str  = “Hello World!”; Console.WriteLine (str); } } AssemblyBuilder ModuleBuilder TypeBuilder MethodBuilder LocalBuilder ILGenerator OPCodes
Lightweight Code Generation ,[object Object],Kid CloneKid(Kid) { newKid.Height = oldKid.Height   newKid.Weight = oldKid.Weight } DynamicMethod ILGenerator OPCodes
[object Object]
 
HackMe ,[object Object],[object Object],[object Object],[object Object],[object Object]
What's new in .NET 3.5? ,[object Object],[object Object],[object Object],[object Object]
From delegate to Lambda Delegate void SomeDelegate(EventsArgs e); Object.SomeEvent += new SomeDelegate(SomeMethod); Delegate Object.SomeEvent += delegate(EventArgs e)    { //work here } Anonymous delegate  Object.SomeEvent += (e) => { //work here }; Lambda
Expression Tree ,[object Object],[object Object],[object Object],[object Object],[object Object]
[object Object],[object Object]
Related Content ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Resources ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Please complete an evaluation
 
© 2008 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries. The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation.  Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation.  MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.

More Related Content

What's hot

Ado.net session11
Ado.net session11Ado.net session11
Ado.net session11
Niit Care
 
Real-World Scala Design Patterns
Real-World Scala Design PatternsReal-World Scala Design Patterns
Real-World Scala Design Patterns
NLJUG
 
JavaScript Parser Infrastructure for Code Quality Analysis
JavaScript Parser Infrastructure for Code Quality AnalysisJavaScript Parser Infrastructure for Code Quality Analysis
JavaScript Parser Infrastructure for Code Quality Analysis
Ariya Hidayat
 
Effective Scala: Programming Patterns
Effective Scala: Programming PatternsEffective Scala: Programming Patterns
Effective Scala: Programming Patterns
Vasil Remeniuk
 
Chapter 7 C++ As OOP
Chapter 7 C++ As OOPChapter 7 C++ As OOP
Chapter 7 C++ As OOP
Amrit Kaur
 

What's hot (20)

Java Script Patterns
Java Script PatternsJava Script Patterns
Java Script Patterns
 
Ado.net session11
Ado.net session11Ado.net session11
Ado.net session11
 
Real-World Scala Design Patterns
Real-World Scala Design PatternsReal-World Scala Design Patterns
Real-World Scala Design Patterns
 
03class
03class03class
03class
 
Java core - Detailed Overview
Java  core - Detailed OverviewJava  core - Detailed Overview
Java core - Detailed Overview
 
Scala’s implicits
Scala’s implicitsScala’s implicits
Scala’s implicits
 
JavaScript Parser Infrastructure for Code Quality Analysis
JavaScript Parser Infrastructure for Code Quality AnalysisJavaScript Parser Infrastructure for Code Quality Analysis
JavaScript Parser Infrastructure for Code Quality Analysis
 
Effective Scala: Programming Patterns
Effective Scala: Programming PatternsEffective Scala: Programming Patterns
Effective Scala: Programming Patterns
 
Iphone lecture imp
Iphone lecture  impIphone lecture  imp
Iphone lecture imp
 
Hello Java-First Level
Hello Java-First LevelHello Java-First Level
Hello Java-First Level
 
Hello java
Hello java  Hello java
Hello java
 
Chapter 7 C++ As OOP
Chapter 7 C++ As OOPChapter 7 C++ As OOP
Chapter 7 C++ As OOP
 
Presentation to java
Presentation  to  javaPresentation  to  java
Presentation to java
 
NDK Primer (Wearable DevCon 2014)
NDK Primer (Wearable DevCon 2014)NDK Primer (Wearable DevCon 2014)
NDK Primer (Wearable DevCon 2014)
 
Java
Java Java
Java
 
Core java Basics
Core java BasicsCore java Basics
Core java Basics
 
openFrameworks freakDay S03E02 Diederick Huijbers - C++/Physics/Cloth Animati...
openFrameworks freakDay S03E02 Diederick Huijbers - C++/Physics/Cloth Animati...openFrameworks freakDay S03E02 Diederick Huijbers - C++/Physics/Cloth Animati...
openFrameworks freakDay S03E02 Diederick Huijbers - C++/Physics/Cloth Animati...
 
Introduction to iOS and Objective-C
Introduction to iOS and Objective-CIntroduction to iOS and Objective-C
Introduction to iOS and Objective-C
 
About Python
About PythonAbout Python
About Python
 
C# for beginners
C# for beginnersC# for beginners
C# for beginners
 

Similar to Understanding Reflection

03 oo with-c-sharp
03 oo with-c-sharp03 oo with-c-sharp
03 oo with-c-sharp
Naved khan
 
Introduction to c sharp 4.0 and dynamic
Introduction to c sharp 4.0 and dynamicIntroduction to c sharp 4.0 and dynamic
Introduction to c sharp 4.0 and dynamic
Gieno Miao
 
NNUG Certification Presentation
NNUG Certification PresentationNNUG Certification Presentation
NNUG Certification Presentation
Niall Merrigan
 
Concepts of Asp.Net
Concepts of Asp.NetConcepts of Asp.Net
Concepts of Asp.Net
vidyamittal
 

Similar to Understanding Reflection (20)

Reflection
ReflectionReflection
Reflection
 
Reflecting On The Code Dom
Reflecting On The Code DomReflecting On The Code Dom
Reflecting On The Code Dom
 
Jeff English: Demystifying Module Development - How to Extend Titanium
Jeff English: Demystifying Module Development - How to Extend TitaniumJeff English: Demystifying Module Development - How to Extend Titanium
Jeff English: Demystifying Module Development - How to Extend Titanium
 
.NET Reflection
.NET Reflection.NET Reflection
.NET Reflection
 
Introduction to directshow II
Introduction to directshow IIIntroduction to directshow II
Introduction to directshow II
 
Windows 8 für .net Entwickler
Windows 8 für .net EntwicklerWindows 8 für .net Entwickler
Windows 8 für .net Entwickler
 
Net framework session03
Net framework session03Net framework session03
Net framework session03
 
03 oo with-c-sharp
03 oo with-c-sharp03 oo with-c-sharp
03 oo with-c-sharp
 
Introduction to c sharp 4.0 and dynamic
Introduction to c sharp 4.0 and dynamicIntroduction to c sharp 4.0 and dynamic
Introduction to c sharp 4.0 and dynamic
 
1.Philosophy of .NET
1.Philosophy of .NET1.Philosophy of .NET
1.Philosophy of .NET
 
Reflection Slides by Zubair Dar
Reflection Slides by Zubair DarReflection Slides by Zubair Dar
Reflection Slides by Zubair Dar
 
Basics of c# by sabir
Basics of c# by sabirBasics of c# by sabir
Basics of c# by sabir
 
Net framework session01
Net framework session01Net framework session01
Net framework session01
 
Introduction to .NET
Introduction to .NETIntroduction to .NET
Introduction to .NET
 
NNUG Certification Presentation
NNUG Certification PresentationNNUG Certification Presentation
NNUG Certification Presentation
 
C Sharp Jn
C Sharp JnC Sharp Jn
C Sharp Jn
 
C Sharp Jn
C Sharp JnC Sharp Jn
C Sharp Jn
 
Tdd,Ioc
Tdd,IocTdd,Ioc
Tdd,Ioc
 
Concepts of Asp.Net
Concepts of Asp.NetConcepts of Asp.Net
Concepts of Asp.Net
 
Mobile Weekend Budapest presentation
Mobile Weekend Budapest presentationMobile Weekend Budapest presentation
Mobile Weekend Budapest presentation
 

More from Tamir Khason (6)

Smart Client Development
Smart Client DevelopmentSmart Client Development
Smart Client Development
 
WPF for developers - optimizing your WPF application
WPF for developers - optimizing your WPF applicationWPF for developers - optimizing your WPF application
WPF for developers - optimizing your WPF application
 
Creating A Game Using Microsoft’s Next Generation Technologies
Creating A Game Using Microsoft’s Next Generation TechnologiesCreating A Game Using Microsoft’s Next Generation Technologies
Creating A Game Using Microsoft’s Next Generation Technologies
 
Modern C&C Systems, Using New Technologies
Modern C&C Systems, Using New TechnologiesModern C&C Systems, Using New Technologies
Modern C&C Systems, Using New Technologies
 
Wpf Under The Hood Engines
Wpf Under The Hood EnginesWpf Under The Hood Engines
Wpf Under The Hood Engines
 
Introduction To Wpf Engines
Introduction To Wpf   EnginesIntroduction To Wpf   Engines
Introduction To Wpf Engines
 

Recently uploaded

Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Victor Rentea
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Safe Software
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
WSO2
 

Recently uploaded (20)

EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWEREMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
 
Exploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with MilvusExploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with Milvus
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
Ransomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdfRansomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdf
 
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
 
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdfRising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
 
ICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesICT role in 21st century education and its challenges
ICT role in 21st century education and its challenges
 
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 

Understanding Reflection

  • 1.  
  • 2. Understanding Reflection Tamir Khason Technology Consultor ¹ mPrest Systems http://blogs.microsoft.co.il/blogs/tamir Session Code: DVP307 ¹ con·sul·tor (kən sult ′ ər) - one of a group of priests appointed to advise and assist a bishop
  • 3. CLR – compilation and execution Execution Compilation Source code Language compiler JIT compiler Before installation or the first time each method is called MSIL Metadata Code Native code
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14. We can clone it… Type kidType = oldKid.GetType(); Kid newKid = new Kid(); foreach (var m in kidType.GetProperties()) { m.SetValue(newKid , m.GetValue(oldKid, null), null); } Unknown object GetType().GetMembers() For Each Member FieldInfo.Copy old value to new Object
  • 15. Clone optimization Kid CloneKid(Kid) { newKid.Height = oldKid.Height newKid.Weight = oldKid.Weight } Unknown object GetType().GetMembers() For Each Member Create Code that copies members Save as Method for later Invocation
  • 16. Clone optimized Unknown object Has custom method? Execute Yes Copy Members No
  • 17.
  • 18.
  • 19.  
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.  
  • 26.
  • 27.
  • 28.
  • 29.
  • 30. Code Emitting public class MyClass { public static void Main() { string str = “Hello World!”; Console.WriteLine (str); } } AssemblyBuilder ModuleBuilder TypeBuilder MethodBuilder LocalBuilder ILGenerator OPCodes
  • 31.
  • 32.
  • 33.  
  • 34.
  • 35.
  • 36. From delegate to Lambda Delegate void SomeDelegate(EventsArgs e); Object.SomeEvent += new SomeDelegate(SomeMethod); Delegate Object.SomeEvent += delegate(EventArgs e) { //work here } Anonymous delegate Object.SomeEvent += (e) => { //work here }; Lambda
  • 37.
  • 38.
  • 39.
  • 40.
  • 41. Please complete an evaluation
  • 42.  
  • 43. © 2008 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries. The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.

Editor's Notes

  1. 06/07/09 17:53 © 2007 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries. The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.