Metaprogramming in .NET

           By Brandon D’Imperio
http://imaginarydevelopment.blogspot.com
What is it?
• Metaprogramming is most easily defined as
  beside-programming or after-programming
  – Late-binding
  – Runtime compilation
  – Code generation
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
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
Javascript
System.Reflection
     Databinding
Emit
CodeDom
CodeDom usage
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
Expressions
Manually built Expression
Dynamics
Roslyn – Code Issue
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 user's 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”
More about me
• http://imaginarydevelopment.blogspot.com
• https://www.ohloh.net/accounts/Maslow
• http://stackoverflow.com/users/57883/maslo
  w
Emit – vs 2012 dark theme
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

Metaprogramming by brandon

  • 1.
    Metaprogramming in .NET By Brandon D’Imperio http://imaginarydevelopment.blogspot.com
  • 2.
    What is it? •Metaprogramming is most easily defined as beside-programming or after-programming – Late-binding – Runtime compilation – Code generation
  • 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.
    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.
  • 6.
    System.Reflection Databinding
  • 7.
  • 8.
  • 9.
  • 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.
  • 12.
  • 13.
  • 14.
  • 15.
    Final words • “metaprogrammingis 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 user's 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.
    More about me •http://imaginarydevelopment.blogspot.com • https://www.ohloh.net/accounts/Maslow • http://stackoverflow.com/users/57883/maslo w
  • 17.
    Emit – vs2012 dark theme
  • 18.
    Appendix • Roslyn Codesource – 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

Editor's Notes

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