•
•
•

•
•

http://slidesha.re/1mMiXAz
•
http://1drv.ms/NbF2fF
•

http://bit.ly/1hz6jFT
3
•
•
•

http://atnd.org/events/47189
•
•
•
•

•
•
•
•

•
•
•
•

•
•
•
•

•
•
•
•
•
•
•

•
•

•
•
•
•

•
•
// C のマクロ (危険)
#define SWAP(x, y, type) { type temporary__; temporary__ = x; x = y; y = temporary__; }

int main(void)
{
int a = 1;
int b = 2;
SWAP(a, b, int);
return 0;
}

プリコンパイラー

int main(void)
{
int a = 1;
int b = 2;
{
int temporary__;
temporary__ = a;
a
= b;
b
= temporary__;
}
return 0;
}
int main()

template <int N>

{

struct Factorial

int x = Factorial<3>::value;

{

return 0;

enum { value = N *
Factorial<N 1>::value };

コンパイラー

}
struct Factorial0 { enum { value = 1
}; };
struct Factorial1 { enum { value = 1 * Factorial0::value }; };

};

struct Factorial2 { enum { value = 2 * Factorial1::value }; };

template <>
struct Factorial<0>
{

struct Factorial3 { enum { value = 3 * Factorial2::value }; };

int main()
{

enum { value = 1 };

int x = Factorial3::value;

};

return 0;
}
•
•
•
•
•
•
• T4 テキスト テンプレートを使用したデザイン時コー
ド生成 - MSDN
•
•

•
ソース コード
(C#、VB、JScript)

CodeDOM

CodeDOMProvider

GenerateCodeFromNamespace
CompileAssemblyFromDom

アセンブリ
•
namespace CodeDomHelloWorldDemo
{
using System;
class Program
{
static void Main()
{
Console.WriteLine("Hello world!");
Console.ReadKey();
}
}

}
•
•

•
•
•
•
Expression<Func<Employee, bool>> expression = employee => employee.Name.Contains("山");

employee =>
employee.Name.Con
tains("山")

employee
Parameters
employee
Object

Body

employee.Name

Expression
Name

employee.Name.Co
ntains("山")

Member
Contains
Method
Arguments

“山”
•
•
•
•
•
•
•
Assembly

FieldInfo

Module
Type
・Class
・Interface
・Value Type

PropertyInfo
EventInfo
MethodInfo
ConstructorInfo

ParameterInfo
•

static int Add(int x, int y)
{
return x + y;
}
•

•
1.
2.
•
• ILSpy - SharpDevelop - SourceForge.net
var method

= new DynamicMethod(“add”, …);

method.DefineParameter(1, ParameterAttributes.In, "x");
method.DefineParameter(2, ParameterAttributes.In, “y");

var generator = method.GetILGenerator();
generator.Emit(opcode: OpCodes.Ldarg_0);
generator.Emit(opcode: OpCodes.Ldarg_1);

generator.Emit(opcode: OpCodes.Add
generator.Emit(opcode: OpCodes.Ret

);
);

Func<int, int, int> newDelegate = method.CreateDelegate(…);
•

=>

1.
2.

(x,
y)

+

x

y
• Expression の派生クラス一覧 - 継承階層 - Expression クラス - MSDN ライブラリ
var x

= Expression.Parameter(type: typeof(int));

var y

= Expression.Parameter(type: typeof(int));

var add

= Expression.Add

var lambda = Expression.Lambda

(left: x, right: y);
(add, x, y

Func<int, int, int> newDelegate =
(Func<int, int, int>)lambda.Compile();

);
•
1.

2.
3.
• Microsoft “Roslyn” CTP
•
var engine = new ScriptEngine();
var session = engine.CreateSession();
session.ImportNamespace(…);

Func<int, int, int> newDelegate =
session.Execute ("(Func<int, int, int>)((x, y) => x + y)");
•
•
•
1.
2.
3.

•
1.
2.

3.
4.
•
•
•
•
•
1.
2.
•
1.
2.
1.
2.

3.
1.
2.
3.

4.
1.
2.
3.
•
•
•
•

•
•

•
•
• オブジェクトの文字列変換を静的/動的に行う
• オブジェクトの文字列変換のメタプログラミング
• オブジェクトの文字列変換のメタプログラミング (Reflection.Emit 編)
• オブジェクトの文字列変換のメタプログラミング (式木編)
• オブジェクトの文字列変換のメタプログラミング (Roslyn 編)
• オブジェクトの文字列変換のメタプログラミング (パフォーマンスのテスト)
•
•
•

•

メタプログラミング C#