SlideShare a Scribd company logo
Raleigh Code Camp 2103 - .Net Metaprogramming Essentials
About me...
•
•
•
•

Technology lead for a team at LexisNexis
Blog: http://seanmccarthy.me
Twitter: mccartsc
G+: scmccart
About this talk...
Slides and Code at, https://github.
com/scmccart/MetaprogrammingEssentialsDemo

Or,

http://bit.ly/1go2IsO
Metaprogramming, huh?
• Writing code that can write code.
• Metaprogramming uses a Metalanguage
• S-Expressions, Attributes, Conventions

• Wide support, in languages such as ruby,

python, javascript, C++, lisp, template haskell
• Compile time vs Runtime
Metalanguages in .Net
• S-Expressions?
• Not available, unless you are using IronScheme

or clojureclr. Kind of via Expression<T>

• Attributes
• Available everywhere, widely understood.

• Conventions
• Common, but custom.
• Asp.Net’s routing.
• Entity Framework’s code first method.

• T4 kind of counts, kind of.
Metaprogramming methods
• Reflection.Emit
• Powerful, can build anything.
• Very manual, hard to use, error prone

• System.Linq.Expressions
• AKA Expression Trees
• Easyish API, type checks
• Can only do lambdas, not as fine grained

• Roslyn
• Compiler as a service
• Not released yet
Expression Trees
Expression Trees map very closely to an AST
An example, x => x.y * 2
As a tree, would be:
*
.
x

2
y
Expression Trees
Trees maps to the Expression builder’s use.
Expression.Multiply(
Expression.Property(
x,
"y"),
Expression.Constant(2))

*
.
x

2
y
Using Expression Trees
Repeatable flow with the Expression builder,
1. Declare parameters
2. Build out body
3. Pull together in lambda
4. Compile
5. Use!
1. Declare Parameters
var value = Expression.Parameter(typeof(int), "value");
2. Build out body
var value = Expression.Parameter(typeof(int), "value");
var body = Expression.Multiply(value, Expression.Constant(2));
3. Pull together in Lambda
var value = Expression.Parameter(typeof(int), "value");
var body = Expression.Multiply(value, Expression.Constant(2));
var lambda = Expression.Lambda<Func<int, int>>(body, value);
4. Compile
var value = Expression.Parameter(typeof(int), "value");
var body = Expression.Multiply(value, Expression.Constant(2));
var lambda = Expression.Lambda<Func<int, int>>(body, value);
var compiled = lambda.Compile();
5. Use!
var value = Expression.Parameter(typeof(int), "value");
var body = Expression.Multiply(value, Expression.Constant(2));
var lambda = Expression.Lambda<Func<int, int>>(body, value);
var compiled = lambda.Compile();
Console.Write("input: ");
var input = int.Parse(Console.ReadLine());
var output = compiled(input);
Console.WriteLine("output: " + output);
Let’s see that in action.
Speed
Quick Tips
•
•
•
•

DebugView
ToString
Cache compiled trees
Limits
• Async/Await
• Yield Return
Object Routing Example
• Routing for objects to eliminate nested switch

statements.
• Uses Expressions for
•
•
•
•

Getting property values
Invoking methods
Ternary expressions
Constructing Arrays

More Related Content

PDF
Денис Лебедев, Swift
PPTX
ECMAScript 2015: my favourite parts
PPTX
Python for Security Professionals
PPTX
Scala Refactoring for Fun and Profit
PPT
Introduction to Laws
PDF
Reference Semantik mit C# und .NET Core - BASTA 2019
PPTX
Intro to Ruby on Rails
PPTX
Metaprogramming in .NET
Денис Лебедев, Swift
ECMAScript 2015: my favourite parts
Python for Security Professionals
Scala Refactoring for Fun and Profit
Introduction to Laws
Reference Semantik mit C# und .NET Core - BASTA 2019
Intro to Ruby on Rails
Metaprogramming in .NET

Similar to Raleigh Code Camp 2103 - .Net Metaprogramming Essentials (20)

PPTX
TypeScript: Basic Features and Compilation Guide
PPTX
TypeScript . the JavaScript developer best friend!
PDF
The Swift Compiler and Standard Library
PPTX
Java-Intro.pptx
PDF
Introduction to Elixir
PDF
First Class Variables as AST Annotations
PDF
First Class Variables as AST Annotations
PDF
C# for beginners
PPTX
PPTX
Introduction to JcjfjfjfkuutyuyrsdterdfbvAVA.pptx
PDF
Lecture 2: ES6 / ES2015 Slide
PDF
Elixir in a nutshell - Ecosystem (session 1)
PDF
l2-es6-160830040119.pdf
PDF
sbt, history of JSON libraries, microservices, and schema evolution (Tokyo ver)
PPTX
Building .NET Core tools using the Roslyn API by Arthur Tabatchnic at .Net fo...
PPTX
Connecting C++ and JavaScript on the Web with Embind
PPT
Python ppt
PPTX
JSLounge - TypeScript 소개
PDF
Exploring Elixir Codebases with Archeometer
TypeScript: Basic Features and Compilation Guide
TypeScript . the JavaScript developer best friend!
The Swift Compiler and Standard Library
Java-Intro.pptx
Introduction to Elixir
First Class Variables as AST Annotations
First Class Variables as AST Annotations
C# for beginners
Introduction to JcjfjfjfkuutyuyrsdterdfbvAVA.pptx
Lecture 2: ES6 / ES2015 Slide
Elixir in a nutshell - Ecosystem (session 1)
l2-es6-160830040119.pdf
sbt, history of JSON libraries, microservices, and schema evolution (Tokyo ver)
Building .NET Core tools using the Roslyn API by Arthur Tabatchnic at .Net fo...
Connecting C++ and JavaScript on the Web with Embind
Python ppt
JSLounge - TypeScript 소개
Exploring Elixir Codebases with Archeometer
Ad

Recently uploaded (20)

PPT
“AI and Expert System Decision Support & Business Intelligence Systems”
PPTX
Big Data Technologies - Introduction.pptx
PPTX
Programs and apps: productivity, graphics, security and other tools
PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
PDF
Empathic Computing: Creating Shared Understanding
PDF
The Rise and Fall of 3GPP – Time for a Sabbatical?
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PDF
Dropbox Q2 2025 Financial Results & Investor Presentation
PDF
A comparative analysis of optical character recognition models for extracting...
PDF
Chapter 3 Spatial Domain Image Processing.pdf
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PDF
Network Security Unit 5.pdf for BCA BBA.
PDF
cuic standard and advanced reporting.pdf
PDF
Electronic commerce courselecture one. Pdf
PDF
MIND Revenue Release Quarter 2 2025 Press Release
PDF
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
PDF
Encapsulation_ Review paper, used for researhc scholars
DOCX
The AUB Centre for AI in Media Proposal.docx
PDF
Assigned Numbers - 2025 - Bluetooth® Document
“AI and Expert System Decision Support & Business Intelligence Systems”
Big Data Technologies - Introduction.pptx
Programs and apps: productivity, graphics, security and other tools
Diabetes mellitus diagnosis method based random forest with bat algorithm
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
Empathic Computing: Creating Shared Understanding
The Rise and Fall of 3GPP – Time for a Sabbatical?
Reach Out and Touch Someone: Haptics and Empathic Computing
Dropbox Q2 2025 Financial Results & Investor Presentation
A comparative analysis of optical character recognition models for extracting...
Chapter 3 Spatial Domain Image Processing.pdf
Per capita expenditure prediction using model stacking based on satellite ima...
Network Security Unit 5.pdf for BCA BBA.
cuic standard and advanced reporting.pdf
Electronic commerce courselecture one. Pdf
MIND Revenue Release Quarter 2 2025 Press Release
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
Encapsulation_ Review paper, used for researhc scholars
The AUB Centre for AI in Media Proposal.docx
Assigned Numbers - 2025 - Bluetooth® Document
Ad

Raleigh Code Camp 2103 - .Net Metaprogramming Essentials

  • 2. About me... • • • • Technology lead for a team at LexisNexis Blog: http://seanmccarthy.me Twitter: mccartsc G+: scmccart
  • 3. About this talk... Slides and Code at, https://github. com/scmccart/MetaprogrammingEssentialsDemo Or, http://bit.ly/1go2IsO
  • 4. Metaprogramming, huh? • Writing code that can write code. • Metaprogramming uses a Metalanguage • S-Expressions, Attributes, Conventions • Wide support, in languages such as ruby, python, javascript, C++, lisp, template haskell • Compile time vs Runtime
  • 5. Metalanguages in .Net • S-Expressions? • Not available, unless you are using IronScheme or clojureclr. Kind of via Expression<T> • Attributes • Available everywhere, widely understood. • Conventions • Common, but custom. • Asp.Net’s routing. • Entity Framework’s code first method. • T4 kind of counts, kind of.
  • 6. Metaprogramming methods • Reflection.Emit • Powerful, can build anything. • Very manual, hard to use, error prone • System.Linq.Expressions • AKA Expression Trees • Easyish API, type checks • Can only do lambdas, not as fine grained • Roslyn • Compiler as a service • Not released yet
  • 7. Expression Trees Expression Trees map very closely to an AST An example, x => x.y * 2 As a tree, would be: * . x 2 y
  • 8. Expression Trees Trees maps to the Expression builder’s use. Expression.Multiply( Expression.Property( x, "y"), Expression.Constant(2)) * . x 2 y
  • 9. Using Expression Trees Repeatable flow with the Expression builder, 1. Declare parameters 2. Build out body 3. Pull together in lambda 4. Compile 5. Use!
  • 10. 1. Declare Parameters var value = Expression.Parameter(typeof(int), "value");
  • 11. 2. Build out body var value = Expression.Parameter(typeof(int), "value"); var body = Expression.Multiply(value, Expression.Constant(2));
  • 12. 3. Pull together in Lambda var value = Expression.Parameter(typeof(int), "value"); var body = Expression.Multiply(value, Expression.Constant(2)); var lambda = Expression.Lambda<Func<int, int>>(body, value);
  • 13. 4. Compile var value = Expression.Parameter(typeof(int), "value"); var body = Expression.Multiply(value, Expression.Constant(2)); var lambda = Expression.Lambda<Func<int, int>>(body, value); var compiled = lambda.Compile();
  • 14. 5. Use! var value = Expression.Parameter(typeof(int), "value"); var body = Expression.Multiply(value, Expression.Constant(2)); var lambda = Expression.Lambda<Func<int, int>>(body, value); var compiled = lambda.Compile(); Console.Write("input: "); var input = int.Parse(Console.ReadLine()); var output = compiled(input); Console.WriteLine("output: " + output);
  • 15. Let’s see that in action.
  • 16. Speed
  • 17. Quick Tips • • • • DebugView ToString Cache compiled trees Limits • Async/Await • Yield Return
  • 18. Object Routing Example • Routing for objects to eliminate nested switch statements. • Uses Expressions for • • • • Getting property values Invoking methods Ternary expressions Constructing Arrays