SlideShare uma empresa Scribd logo
1 de 18
Baixar para ler offline
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

Mais conteúdo relacionado

Semelhante a Raleigh Code Camp 2103 - .Net Metaprogramming Essentials

TypeScript: Basic Features and Compilation Guide
TypeScript: Basic Features and Compilation GuideTypeScript: Basic Features and Compilation Guide
TypeScript: Basic Features and Compilation GuideNascenia IT
 
TypeScript . the JavaScript developer best friend!
TypeScript . the JavaScript developer best friend!TypeScript . the JavaScript developer best friend!
TypeScript . the JavaScript developer best friend!Alessandro Giorgetti
 
The Swift Compiler and Standard Library
The Swift Compiler and Standard LibraryThe Swift Compiler and Standard Library
The Swift Compiler and Standard LibrarySantosh Rajan
 
Java-Intro.pptx
Java-Intro.pptxJava-Intro.pptx
Java-Intro.pptxVijalJain3
 
Introduction to Elixir
Introduction to ElixirIntroduction to Elixir
Introduction to ElixirDiacode
 
First Class Variables as AST Annotations
 First Class Variables as AST Annotations First Class Variables as AST Annotations
First Class Variables as AST AnnotationsESUG
 
First Class Variables as AST Annotations
First Class Variables as AST AnnotationsFirst Class Variables as AST Annotations
First Class Variables as AST AnnotationsMarcus Denker
 
Elixir in a nutshell - Ecosystem (session 1)
Elixir in a nutshell - Ecosystem (session 1)Elixir in a nutshell - Ecosystem (session 1)
Elixir in a nutshell - Ecosystem (session 1)Héla Ben Khalfallah
 
sbt, history of JSON libraries, microservices, and schema evolution (Tokyo ver)
sbt, history of JSON libraries, microservices, and schema evolution (Tokyo ver)sbt, history of JSON libraries, microservices, and schema evolution (Tokyo ver)
sbt, history of JSON libraries, microservices, and schema evolution (Tokyo ver)Eugene Yokota
 
Building .NET Core tools using the Roslyn API by Arthur Tabatchnic at .Net fo...
Building .NET Core tools using the Roslyn API by Arthur Tabatchnic at .Net fo...Building .NET Core tools using the Roslyn API by Arthur Tabatchnic at .Net fo...
Building .NET Core tools using the Roslyn API by Arthur Tabatchnic at .Net fo...DevClub_lv
 
Connecting C++ and JavaScript on the Web with Embind
Connecting C++ and JavaScript on the Web with EmbindConnecting C++ and JavaScript on the Web with Embind
Connecting C++ and JavaScript on the Web with EmbindChad Austin
 
JSLounge - TypeScript 소개
JSLounge - TypeScript 소개JSLounge - TypeScript 소개
JSLounge - TypeScript 소개Reagan Hwang
 
Exploring Elixir Codebases with Archeometer
Exploring Elixir Codebases with ArcheometerExploring Elixir Codebases with Archeometer
Exploring Elixir Codebases with ArcheometerAgustin Ramos
 

Semelhante a Raleigh Code Camp 2103 - .Net Metaprogramming Essentials (20)

TypeScript: Basic Features and Compilation Guide
TypeScript: Basic Features and Compilation GuideTypeScript: Basic Features and Compilation Guide
TypeScript: Basic Features and Compilation Guide
 
TypeScript . the JavaScript developer best friend!
TypeScript . the JavaScript developer best friend!TypeScript . the JavaScript developer best friend!
TypeScript . the JavaScript developer best friend!
 
The Swift Compiler and Standard Library
The Swift Compiler and Standard LibraryThe Swift Compiler and Standard Library
The Swift Compiler and Standard Library
 
Java-Intro.pptx
Java-Intro.pptxJava-Intro.pptx
Java-Intro.pptx
 
Introduction to Elixir
Introduction to ElixirIntroduction to Elixir
Introduction to Elixir
 
First Class Variables as AST Annotations
 First Class Variables as AST Annotations First Class Variables as AST Annotations
First Class Variables as AST Annotations
 
First Class Variables as AST Annotations
First Class Variables as AST AnnotationsFirst Class Variables as AST Annotations
First Class Variables as AST Annotations
 
C# for beginners
C# for beginnersC# for beginners
C# for beginners
 
Angular2
Angular2Angular2
Angular2
 
Lecture 2: ES6 / ES2015 Slide
Lecture 2: ES6 / ES2015 SlideLecture 2: ES6 / ES2015 Slide
Lecture 2: ES6 / ES2015 Slide
 
Elixir in a nutshell - Ecosystem (session 1)
Elixir in a nutshell - Ecosystem (session 1)Elixir in a nutshell - Ecosystem (session 1)
Elixir in a nutshell - Ecosystem (session 1)
 
l2-es6-160830040119.pdf
l2-es6-160830040119.pdfl2-es6-160830040119.pdf
l2-es6-160830040119.pdf
 
sbt, history of JSON libraries, microservices, and schema evolution (Tokyo ver)
sbt, history of JSON libraries, microservices, and schema evolution (Tokyo ver)sbt, history of JSON libraries, microservices, and schema evolution (Tokyo ver)
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...
Building .NET Core tools using the Roslyn API by Arthur Tabatchnic at .Net fo...Building .NET Core tools using the Roslyn API by Arthur Tabatchnic at .Net fo...
Building .NET Core tools using the Roslyn API by Arthur Tabatchnic at .Net fo...
 
Connecting C++ and JavaScript on the Web with Embind
Connecting C++ and JavaScript on the Web with EmbindConnecting C++ and JavaScript on the Web with Embind
Connecting C++ and JavaScript on the Web with Embind
 
C#ppt
C#pptC#ppt
C#ppt
 
Python ppt
Python pptPython ppt
Python ppt
 
JSLounge - TypeScript 소개
JSLounge - TypeScript 소개JSLounge - TypeScript 소개
JSLounge - TypeScript 소개
 
Exploring Elixir Codebases with Archeometer
Exploring Elixir Codebases with ArcheometerExploring Elixir Codebases with Archeometer
Exploring Elixir Codebases with Archeometer
 
Modern C++
Modern C++Modern C++
Modern C++
 

Último

How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.Curtis Poe
 
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptxThe Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptxLoriGlavin3
 
The State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxThe State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxLoriGlavin3
 
(How to Program) Paul Deitel, Harvey Deitel-Java How to Program, Early Object...
(How to Program) Paul Deitel, Harvey Deitel-Java How to Program, Early Object...(How to Program) Paul Deitel, Harvey Deitel-Java How to Program, Early Object...
(How to Program) Paul Deitel, Harvey Deitel-Java How to Program, Early Object...AliaaTarek5
 
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc
 
A Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptxA Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptxLoriGlavin3
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii SoldatenkoFwdays
 
Sample pptx for embedding into website for demo
Sample pptx for embedding into website for demoSample pptx for embedding into website for demo
Sample pptx for embedding into website for demoHarshalMandlekar2
 
Time Series Foundation Models - current state and future directions
Time Series Foundation Models - current state and future directionsTime Series Foundation Models - current state and future directions
Time Series Foundation Models - current state and future directionsNathaniel Shimoni
 
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptxUse of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptxLoriGlavin3
 
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptxPasskey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptxLoriGlavin3
 
Take control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteTake control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteDianaGray10
 
Visualising and forecasting stocks using Dash
Visualising and forecasting stocks using DashVisualising and forecasting stocks using Dash
Visualising and forecasting stocks using Dashnarutouzumaki53779
 
What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024Stephanie Beckett
 
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024BookNet Canada
 
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024BookNet Canada
 
A Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software DevelopersA Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software DevelopersNicole Novielli
 
SALESFORCE EDUCATION CLOUD | FEXLE SERVICES
SALESFORCE EDUCATION CLOUD | FEXLE SERVICESSALESFORCE EDUCATION CLOUD | FEXLE SERVICES
SALESFORCE EDUCATION CLOUD | FEXLE SERVICESmohitsingh558521
 
From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .Alan Dix
 
The Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsThe Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsPixlogix Infotech
 

Último (20)

How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.
 
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptxThe Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
 
The State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxThe State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptx
 
(How to Program) Paul Deitel, Harvey Deitel-Java How to Program, Early Object...
(How to Program) Paul Deitel, Harvey Deitel-Java How to Program, Early Object...(How to Program) Paul Deitel, Harvey Deitel-Java How to Program, Early Object...
(How to Program) Paul Deitel, Harvey Deitel-Java How to Program, Early Object...
 
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
 
A Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptxA Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptx
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko
 
Sample pptx for embedding into website for demo
Sample pptx for embedding into website for demoSample pptx for embedding into website for demo
Sample pptx for embedding into website for demo
 
Time Series Foundation Models - current state and future directions
Time Series Foundation Models - current state and future directionsTime Series Foundation Models - current state and future directions
Time Series Foundation Models - current state and future directions
 
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptxUse of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
 
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptxPasskey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptx
 
Take control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteTake control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test Suite
 
Visualising and forecasting stocks using Dash
Visualising and forecasting stocks using DashVisualising and forecasting stocks using Dash
Visualising and forecasting stocks using Dash
 
What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024
 
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
 
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
 
A Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software DevelopersA Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software Developers
 
SALESFORCE EDUCATION CLOUD | FEXLE SERVICES
SALESFORCE EDUCATION CLOUD | FEXLE SERVICESSALESFORCE EDUCATION CLOUD | FEXLE SERVICES
SALESFORCE EDUCATION CLOUD | FEXLE SERVICES
 
From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .
 
The Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsThe Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and Cons
 

Raleigh Code Camp 2103 - .Net Metaprogramming Essentials

  • 1.
  • 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