Metaprogramming by brandon

Preview:

DESCRIPTION

 

Citation preview

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 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.ReflectionDatabinding

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/

maslow

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