18
Metaprogramming in .NET By Brandon D’Imperio http://imaginarydevelopment.bl ogspot.com

Metaprogramming by brandon

  • Upload
    maslowb

  • View
    430

  • Download
    0

Embed Size (px)

DESCRIPTION

 

Citation preview

Page 1: Metaprogramming by brandon

Metaprogramming in .NET

By Brandon D’Imperiohttp://imaginarydevelopment.blogspot.com

Page 2: Metaprogramming by brandon

What is it?

• Metaprogramming is most easily defined as beside-programming or after-programming– Late-binding– Runtime compilation– Code generation

Page 3: Metaprogramming by brandon

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

Page 4: Metaprogramming by brandon

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

Page 5: Metaprogramming by brandon

Javascript

Page 6: Metaprogramming by brandon

System.ReflectionDatabinding

Page 7: Metaprogramming by brandon

Emit

Page 8: Metaprogramming by brandon

CodeDom

Page 9: Metaprogramming by brandon

CodeDom usage

Page 10: Metaprogramming by brandon

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

Page 11: Metaprogramming by brandon

Expressions

Page 12: Metaprogramming by brandon

Manually built Expression

Page 13: Metaprogramming by brandon

Dynamics

Page 14: Metaprogramming by brandon

Roslyn – Code Issue

Page 15: Metaprogramming by brandon

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”

Page 16: Metaprogramming by brandon

More about me

• http://imaginarydevelopment.blogspot.com• https://www.ohloh.net/accounts/Maslow• http://stackoverflow.com/users/57883/

maslow

Page 17: Metaprogramming by brandon

Emit – vs 2012 dark theme

Page 18: Metaprogramming by brandon

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