Metaprogramming in .NET           By Brandon D’Imperiohttp://imaginarydevelopment.blogspot.com
What is it?• Metaprogramming is most easily defined as  beside-programming or after-programming  – Late-binding  – Runtime...
Where is it?• You’ve already seen and used it, most likely  – Microsoft     • System.Reflection     • CodeDom     • Emit  ...
Why?• DRY – Don’t repeat yourself  – don’t duplicate authority  – Boiler-plate code  – Simplify manual coding hand work   ...
Javascript
System.Reflection     Databinding
Emit
CodeDom
CodeDom usage
T4<#@ template language="C#" #> <#@ output extension=".cs" #><#@ assembly name="System.Core" #><#@ import namespace="Syste...
Expressions
Manually built Expression
Dynamics
Roslyn – Code Issue
Final words• “metaprogramming is all about making software simpler  and reusable. However, rather than depending strictly ...
More about me• http://imaginarydevelopment.blogspot.com• https://www.ohloh.net/accounts/Maslow• http://stackoverflow.com/u...
Emit – vs 2012 dark theme
Appendix• Roslyn Code source  – http://code.msdn.microsoft.com/Implementing-a-Code-    Action-28579fb1• Many of my T4’s av...
Upcoming SlideShare
Loading in …5
×

Metaprogramming by brandon

331 views
290 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
331
On SlideShare
0
From Embeds
0
Number of Embeds
18
Actions
Shares
0
Downloads
8
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide
  • Metaprogramming in .NET MEAP v6 pg 5.
  • .net 1.1
  • .net 1.1
  • Released 2005 out of band from VS2005 Notice the lack of whitespacing?
  • If this were linq to sql(or EF) the expression part would be easier to see/discuss Late binding, questionable practices, and much more!
  • Metaprogramming by brandon

    1. 1. Metaprogramming in .NET By Brandon D’Imperiohttp://imaginarydevelopment.blogspot.com
    2. 2. What is it?• Metaprogramming is most easily defined as beside-programming or after-programming – Late-binding – Runtime compilation – Code generation
    3. 3. Where is it?• You’ve already seen and used it, most likely – Microsoft • System.Reflection • CodeDom • Emit • T4 • Dynamic Language Runtime (DLR, dynamic keyword) Generating Intermediate Language (IL) with Expression Trees • Roslyn (compiler as a service) • Enterprise Library’s Policy Injection block • Generics – Aspect Oriented Programming
    4. 4. Why?• DRY – Don’t repeat yourself – don’t duplicate authority – Boiler-plate code – Simplify manual coding hand work • Update the entire system at a single point rather than search and find by hand• Lack of higher order functions• Make parts of the application update or adjust based on each other at the click of a button
    5. 5. Javascript
    6. 6. System.Reflection Databinding
    7. 7. Emit
    8. 8. CodeDom
    9. 9. CodeDom usage
    10. 10. T4<#@ template language="C#" #> <#@ output extension=".cs" #><#@ assembly name="System.Core" #><#@ import namespace="System.Linq" #><# Type[] types_to_generate = new[] { typeof(object), typeof(bool), typeof(byte),typeof(char),  typeof(decimal), typeof(double),typeof(float),  typeof(int), typeof(long), typeof(sbyte), typeof(short), typeof(string),typeof(uint),  typeof(ulong), typeof(ushort) }; #> using System; public static class greater { <# foreach (var type in types_to_generate) { #> public static <#= type.Name #> of(<#= type.Name #> left, <#= type.Name #> right) { <# Type generic_icomparable =(from intf in type.GetInterfaces()   let args = intf.GetGenericArguments()  where intf.Name == "IComparable`1“ && args != null && args[0].Equals(type)  select intf).FirstOrDefault(); if (generic_icomparable != null || type is IComparable) { #> return left.CompareTo(right) < 0 ? right : left; <# } else { #> throw new ApplicationException( "Type <#= type.Name #> must implement one of the " + "IComparable or IComparable<<#= type.Name #>> interfaces."); <# } #> } <# } #> Many of my T4’s available at http://ideone.com/imaginarydevelopment/t4
    11. 11. Expressions
    12. 12. Manually built Expression
    13. 13. Dynamics
    14. 14. Roslyn – Code Issue
    15. 15. Final words• “metaprogramming is all about making software simpler and reusable. However, rather than depending strictly on language features to reduce code complexity or to increase reusability, metaprogramming achieves those goals through a variety of libraries and coding techniques”• “For the most part however, metaprogramming is a set of language independent skills.”• “As perceived complexity from the end users standpoint goes down, internal complexity of the design often goes up. Complexity reduction when metaprogramming follows the same rules. To achieve simplicity on the outside, the code on the inside of a metaprogramming-enabled component typically takes on some added Responsibilities”
    16. 16. More about me• http://imaginarydevelopment.blogspot.com• https://www.ohloh.net/accounts/Maslow• http://stackoverflow.com/users/57883/maslo w
    17. 17. Emit – vs 2012 dark theme
    18. 18. Appendix• Roslyn Code source – http://code.msdn.microsoft.com/Implementing-a-Code- Action-28579fb1• Many of my T4’s available – http://ideone.com/imaginarydevelopment/t4• Emit Full source – http://breusable.codeplex.com/SourceControl /changeset/view/92288#1863265• AOP – was a little deeper than I thought  we would have time for – http://www.sharpcrafters.com/ – http://www.mono-project.com/Cecil

    ×