SlideShare a Scribd company logo
Reflecting on the CodeDom
What is Reflection? Allows you to view metadata about classes Allows you to dynamically discover new classes at runtime Allows you to dynamically invoke methods or properties discovered at runtime Allows you to create new types at runtime
Why should we care about Reflection? Viewing metadata allows us to automate documentation Dynamically discovering classes make it easier to create plug-ins Dynamically invoking new methods allows us to create adaptive solutions not dependant on recompiling Creating new types at runtime allows us to convert scripting customizations to compiled code
A Gentle Tour of Reflection Key data types System.Type System.Reflection.Assembly System.Reflection.MemberInfo System.Reflection.MethodInfo System.Reflection.ConstructorInfo System.Reflection.ProperyInfo System.Reflection.EventInfo
The Great and Mighty Type Object Every Object provides a GetType method which will return a Type Object Key Properties include: Assembly  will return the Assembly object where this type is located BaseType will return the Type object that this Type is directly derived IsClass, IsEnum, IsInterface will provide details about the type of class
Type Object  (Contd) Key Methods include: GetMethods will return an array of MethodInfo objects one for each method defined in the Type GetProperties will return an array of PropertyInfo objects one for each property defined in the Type GetEvents will return an array of EventInfo objects one for each event defined in the Type GetConstructors will return an array of ConstructorInfo objects one for each event defined in the Type
Assembly Object Provides metadata information about a DLL Key methods GetTypes will return an array of Type objects one for each type included in the assembly Key static methods Load will return the assembly specified LoadFrom will return the assembly specified by filename
MemberInfo Base Class for MethodInfo, ConstructorInfo, ProperyInfo, EventInfo Provides limited baseline common attributes such as: Name MemberType
Sample Code (Object Browser) Select a DLL Detect all of the Classes Detect all of the Properties Detect all of the Methods Detect all of the Parameters for each Method
Commenting Code Type Custom Attributes Apply Custom Attributes to your code Use Reflection to retrieve the custom attributes from the Types in an Assembly
Defining a Custom Attribute AttributeUsageAttribute Define where an attribute can be used Specify whether or not the attribute can be applied more than once Specify whether or not the attribute is inheritable Derives from System.Attribute Define a Constructor to be called when an attribute is added to code Define properties to expose attribute specific details
Applying Attributes to Code C# [FlowerBoxAttribute (&quot;Nick Harrison&quot;, “July 7, 2004&quot;, &quot;A simple class to do some simple calcs&quot;)]   VB.Net <FlowerBoxAttribute(&quot;Nick Harrison&quot;, “July 7, 2004&quot;, &quot;A simple class to do some simple calcs&quot;)>
Retrieving Attributes from Code FlowerBoxAttribute [] FlowerBoxes; FlowerBoxes  = (FlowerBoxAttribute []) Member.GetCustomAttributes (typeof (FlowerBoxAttribute ), false); foreach (CustomAttributes.FlowerBoxAttribute  FlowerBox in FlowerBoxes) { // Access the properties of the FlowerBoxAttribute }
Creating a Plug-in Store the location of the Assembly in a config file Store the name of the Class in a config file Load the Assembly Instantiate the Type Convert the new type to a pre-defined interface. Use the new interface object
CodeDom Demystified

More Related Content

PPTX
Reflection power pointpresentation ppt
PPT
Reflection in C Sharp
PPT
Advanced c#
PPTX
Reflection in C#
PPT
.NET Reflection
PPT
Smoke and Mirrors - Reflection in C#
PPTX
Module 15 attributes
PPTX
Module 13 operators, delegates, and events
Reflection power pointpresentation ppt
Reflection in C Sharp
Advanced c#
Reflection in C#
.NET Reflection
Smoke and Mirrors - Reflection in C#
Module 15 attributes
Module 13 operators, delegates, and events

What's hot (19)

PPT
Linq
PPTX
Iterator - a powerful but underappreciated design pattern
PDF
PDF
12 events and delegates
PPTX
Module 12 aggregation, namespaces, and advanced scope
PDF
ознакомления с модулем Entity api
PPTX
Validate your entities with symfony validator and entity validation api
PPTX
Delegates and events
PPT
Generic
PPT
Module 3: Introduction to LINQ (PowerPoint Slides)
PPTX
C# Constructors
PPTX
Mule java part-3
PPTX
Delegates in C#
PDF
Day02 01 Advance Feature in C# DH TDT
PPT
Wpf from David Kossoglyad
PPTX
Explain Delegates step by step.
PPT
vb.net Constructor and destructor
PPTX
Filter
PPTX
Constructor in java
Linq
Iterator - a powerful but underappreciated design pattern
12 events and delegates
Module 12 aggregation, namespaces, and advanced scope
ознакомления с модулем Entity api
Validate your entities with symfony validator and entity validation api
Delegates and events
Generic
Module 3: Introduction to LINQ (PowerPoint Slides)
C# Constructors
Mule java part-3
Delegates in C#
Day02 01 Advance Feature in C# DH TDT
Wpf from David Kossoglyad
Explain Delegates step by step.
vb.net Constructor and destructor
Filter
Constructor in java
Ad

Viewers also liked (7)

PPT
Refactoring workshop
PPTX
Code Inventory
PPT
Introducing fx cop
PPTX
Metric driven refactoring
PPT
Refactoring Workshop
PPT
Revisiting Refactoring
PPTX
Visual basic coding
Refactoring workshop
Code Inventory
Introducing fx cop
Metric driven refactoring
Refactoring Workshop
Revisiting Refactoring
Visual basic coding
Ad

Similar to Reflecting On The Code Dom (20)

PPT
Understanding Reflection
PDF
CSharp Advanced L05-Attributes+Reflection
PPT
Reflection Slides by Zubair Dar
PPT
Reflection
PDF
Reflection Mcq's by Zubair Dar
PPTX
15reflection in c#
KEY
Attributes, reflection, and dynamic programming
PDF
1204csharp
PPTX
.NET Attributes and Reflection - What a Developer Needs to Know...
DOC
Framework Project Portfolio
PPT
C# Language Overview Part II
PPS
13 iec t1_s1_oo_ps_session_19
PDF
Dynamic Binding in C# 4.0
PPTX
CSharp for Unity Day 3
PPTX
CSharp presentation and software developement
PPS
Introduction to CSharp
PPT
Introduction to csharp
PPT
Introduction to-csharp-1229579367461426-1
PPT
Introduction to csharp
Understanding Reflection
CSharp Advanced L05-Attributes+Reflection
Reflection Slides by Zubair Dar
Reflection
Reflection Mcq's by Zubair Dar
15reflection in c#
Attributes, reflection, and dynamic programming
1204csharp
.NET Attributes and Reflection - What a Developer Needs to Know...
Framework Project Portfolio
C# Language Overview Part II
13 iec t1_s1_oo_ps_session_19
Dynamic Binding in C# 4.0
CSharp for Unity Day 3
CSharp presentation and software developement
Introduction to CSharp
Introduction to csharp
Introduction to-csharp-1229579367461426-1
Introduction to csharp

Recently uploaded (20)

PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
PDF
MIND Revenue Release Quarter 2 2025 Press Release
PDF
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
PDF
A comparative analysis of optical character recognition models for extracting...
PPTX
Programs and apps: productivity, graphics, security and other tools
PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
PDF
Empathic Computing: Creating Shared Understanding
PPTX
sap open course for s4hana steps from ECC to s4
PDF
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
PDF
Approach and Philosophy of On baking technology
PDF
Chapter 3 Spatial Domain Image Processing.pdf
PPTX
Machine Learning_overview_presentation.pptx
PPTX
Cloud computing and distributed systems.
PDF
Encapsulation_ Review paper, used for researhc scholars
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
PPTX
A Presentation on Artificial Intelligence
PDF
gpt5_lecture_notes_comprehensive_20250812015547.pdf
DOCX
The AUB Centre for AI in Media Proposal.docx
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
Agricultural_Statistics_at_a_Glance_2022_0.pdf
MIND Revenue Release Quarter 2 2025 Press Release
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
A comparative analysis of optical character recognition models for extracting...
Programs and apps: productivity, graphics, security and other tools
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
Empathic Computing: Creating Shared Understanding
sap open course for s4hana steps from ECC to s4
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
Approach and Philosophy of On baking technology
Chapter 3 Spatial Domain Image Processing.pdf
Machine Learning_overview_presentation.pptx
Cloud computing and distributed systems.
Encapsulation_ Review paper, used for researhc scholars
Digital-Transformation-Roadmap-for-Companies.pptx
A Presentation on Artificial Intelligence
gpt5_lecture_notes_comprehensive_20250812015547.pdf
The AUB Centre for AI in Media Proposal.docx
Per capita expenditure prediction using model stacking based on satellite ima...

Reflecting On The Code Dom

  • 2. What is Reflection? Allows you to view metadata about classes Allows you to dynamically discover new classes at runtime Allows you to dynamically invoke methods or properties discovered at runtime Allows you to create new types at runtime
  • 3. Why should we care about Reflection? Viewing metadata allows us to automate documentation Dynamically discovering classes make it easier to create plug-ins Dynamically invoking new methods allows us to create adaptive solutions not dependant on recompiling Creating new types at runtime allows us to convert scripting customizations to compiled code
  • 4. A Gentle Tour of Reflection Key data types System.Type System.Reflection.Assembly System.Reflection.MemberInfo System.Reflection.MethodInfo System.Reflection.ConstructorInfo System.Reflection.ProperyInfo System.Reflection.EventInfo
  • 5. The Great and Mighty Type Object Every Object provides a GetType method which will return a Type Object Key Properties include: Assembly will return the Assembly object where this type is located BaseType will return the Type object that this Type is directly derived IsClass, IsEnum, IsInterface will provide details about the type of class
  • 6. Type Object (Contd) Key Methods include: GetMethods will return an array of MethodInfo objects one for each method defined in the Type GetProperties will return an array of PropertyInfo objects one for each property defined in the Type GetEvents will return an array of EventInfo objects one for each event defined in the Type GetConstructors will return an array of ConstructorInfo objects one for each event defined in the Type
  • 7. Assembly Object Provides metadata information about a DLL Key methods GetTypes will return an array of Type objects one for each type included in the assembly Key static methods Load will return the assembly specified LoadFrom will return the assembly specified by filename
  • 8. MemberInfo Base Class for MethodInfo, ConstructorInfo, ProperyInfo, EventInfo Provides limited baseline common attributes such as: Name MemberType
  • 9. Sample Code (Object Browser) Select a DLL Detect all of the Classes Detect all of the Properties Detect all of the Methods Detect all of the Parameters for each Method
  • 10. Commenting Code Type Custom Attributes Apply Custom Attributes to your code Use Reflection to retrieve the custom attributes from the Types in an Assembly
  • 11. Defining a Custom Attribute AttributeUsageAttribute Define where an attribute can be used Specify whether or not the attribute can be applied more than once Specify whether or not the attribute is inheritable Derives from System.Attribute Define a Constructor to be called when an attribute is added to code Define properties to expose attribute specific details
  • 12. Applying Attributes to Code C# [FlowerBoxAttribute (&quot;Nick Harrison&quot;, “July 7, 2004&quot;, &quot;A simple class to do some simple calcs&quot;)] VB.Net <FlowerBoxAttribute(&quot;Nick Harrison&quot;, “July 7, 2004&quot;, &quot;A simple class to do some simple calcs&quot;)>
  • 13. Retrieving Attributes from Code FlowerBoxAttribute [] FlowerBoxes; FlowerBoxes = (FlowerBoxAttribute []) Member.GetCustomAttributes (typeof (FlowerBoxAttribute ), false); foreach (CustomAttributes.FlowerBoxAttribute FlowerBox in FlowerBoxes) { // Access the properties of the FlowerBoxAttribute }
  • 14. Creating a Plug-in Store the location of the Assembly in a config file Store the name of the Class in a config file Load the Assembly Instantiate the Type Convert the new type to a pre-defined interface. Use the new interface object