Your SlideShare is downloading. ×
0
Raleigh Code Camp 2103 - .Net Metaprogramming Essentials
Raleigh Code Camp 2103 - .Net Metaprogramming Essentials
Raleigh Code Camp 2103 - .Net Metaprogramming Essentials
Raleigh Code Camp 2103 - .Net Metaprogramming Essentials
Raleigh Code Camp 2103 - .Net Metaprogramming Essentials
Raleigh Code Camp 2103 - .Net Metaprogramming Essentials
Raleigh Code Camp 2103 - .Net Metaprogramming Essentials
Raleigh Code Camp 2103 - .Net Metaprogramming Essentials
Raleigh Code Camp 2103 - .Net Metaprogramming Essentials
Raleigh Code Camp 2103 - .Net Metaprogramming Essentials
Raleigh Code Camp 2103 - .Net Metaprogramming Essentials
Raleigh Code Camp 2103 - .Net Metaprogramming Essentials
Raleigh Code Camp 2103 - .Net Metaprogramming Essentials
Raleigh Code Camp 2103 - .Net Metaprogramming Essentials
Raleigh Code Camp 2103 - .Net Metaprogramming Essentials
Raleigh Code Camp 2103 - .Net Metaprogramming Essentials
Raleigh Code Camp 2103 - .Net Metaprogramming Essentials
Raleigh Code Camp 2103 - .Net Metaprogramming Essentials
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

Raleigh Code Camp 2103 - .Net Metaprogramming Essentials

111

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
111
On Slideshare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
2
Comments
0
Likes
0
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

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

×