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/1go2...
Metaprogramming, huh?
• Writing code that can write code.
• Metaprogramming uses a Metalanguage
• S-Expressions, Attribute...
Metalanguages in .Net
• S-Expressions?
• Not available, unless you are using IronScheme

or clojureclr. Kind of via Expres...
Metaprogramming methods
• Reflection.Emit
• Powerful, can build anything.
• Very manual, hard to use, error prone

• Syste...
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...
Using Expression Trees
Repeatable flow with the Expression builder,
1. Declare parameters
2. Build out body
3. Pull togeth...
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...
3. Pull together in Lambda
var value = Expression.Parameter(typeof(int), "value");
var body = Expression.Multiply(value, E...
4. Compile
var value = Expression.Parameter(typeof(int), "value");
var body = Expression.Multiply(value, Expression.Consta...
5. Use!
var value = Expression.Parameter(typeof(int), "value");
var body = Expression.Multiply(value, Expression.Constant(...
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
•
•
•
•

Getti...
Raleigh Code Camp 2103 - .Net Metaprogramming Essentials
Upcoming SlideShare
Loading in …5
×

Raleigh Code Camp 2103 - .Net Metaprogramming Essentials

235 views
161 views

Published on

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
235
On SlideShare
0
From Embeds
0
Number of Embeds
4
Actions
Shares
0
Downloads
2
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Raleigh Code Camp 2103 - .Net Metaprogramming Essentials

  1. 1. About me... • • • • Technology lead for a team at LexisNexis Blog: http://seanmccarthy.me Twitter: mccartsc G+: scmccart
  2. 2. About this talk... Slides and Code at, https://github. com/scmccart/MetaprogrammingEssentialsDemo Or, http://bit.ly/1go2IsO
  3. 3. 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
  4. 4. 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.
  5. 5. 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
  6. 6. Expression Trees Expression Trees map very closely to an AST An example, x => x.y * 2 As a tree, would be: * . x 2 y
  7. 7. Expression Trees Trees maps to the Expression builder’s use. Expression.Multiply( Expression.Property( x, "y"), Expression.Constant(2)) * . x 2 y
  8. 8. 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!
  9. 9. 1. Declare Parameters var value = Expression.Parameter(typeof(int), "value");
  10. 10. 2. Build out body var value = Expression.Parameter(typeof(int), "value"); var body = Expression.Multiply(value, Expression.Constant(2));
  11. 11. 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);
  12. 12. 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();
  13. 13. 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);
  14. 14. Let’s see that in action.
  15. 15. Speed
  16. 16. Quick Tips • • • • DebugView ToString Cache compiled trees Limits • Async/Await • Yield Return
  17. 17. Object Routing Example • Routing for objects to eliminate nested switch statements. • Uses Expressions for • • • • Getting property values Invoking methods Ternary expressions Constructing Arrays

×