??????????????????????????

?????????????

?????????????????????dynamic ??????????????????????÷????????????????

??????????

??????????????????

??????????п????????

????????????????

???????????????????????????????????????????

????dynamic ??????У???????

?????????????

???????????????

???????β??????? MyMath.Add ???????????????????????????????????????????????????????????????????????????????????????????????????????СЩ??

???????????????????η????????????????????? 100 ??ε?????μó??????????ε??????????????????? 381 ???????????????λ????????????????????????????????????????????????????

???????????

?????????????????????ο???

using System??
using System.Linq.Expressions??
using System.Reflection.Emit??
public class MyMath {
public int Add(int a?? int b) { return a + b?? }
}
class Program {
static void Main(string[] args) {
int result??
var math = new MyMath()??
var count = 1000000??
Console.WriteLine("????????" + count)??
Console.WriteLine("-----------------------------r ")??
using (Profiler.Step("?????{0} ms")) {
for (int i = 0?? i < count?? i++)
result = 1??
}
using (Profiler.Step("?????? ??{0} ms")) {
for (int i = 0?? i < count?? i++)
result = math.Add(i?? i)??
}
using (Profiler.Step("????????{0} ms")) {
var emitAdd = BuildEmitAddFunc()??
for (int i = 0?? i < count?? i++)
result = emitAdd(math?? i?? i)??
}
using (Profiler.Step("????????{0} ms")) {
var expressionAdd = BuildExpressionAddFunc()??
for (int i = 0?? i < count?? i++)
result = expressionAdd(math?? i?? i)??
}
using (Profiler.Step("dynamic ?????{0} ms")) {
dynamic d = math??
for (int i = 0?? i < count?? i++)
result = d.Add(i?? i)??
}
using (Profiler.Step("????????{0} ms")) {
var add = typeof(MyMath).GetMethod("Add")??
for (int i = 0?? i < count?? i++)
result = (int)add.Invoke(math?? new object[] { i?? i })??
}
Console.WriteLine(" ????????????????...")??
Console.ReadKey()??
}
static Func<MyMath?? int?? int?? int> BuildExpressionAddFunc() {
var add = typeof(MyMath).GetMethod("Add")??
var math = Expression.Parameter(typeof(MyMath))??
var a = Expression.Parameter(typeof(int)?? "a")??
var b = Expression.Parameter(typeof(int)?? "b")??
var body = Expression.Call(math?? add?? a?? b)??
var lambda = Expression.Lambda<Func<MyMath?? int?? int?? int>>(body?? math?? a?? b)??
return lambda.Compile()??
}
static Func<MyMath?? int?? int?? int> BuildEmitAddFunc() {
var add = typeof(MyMath).GetMethod("Add")??
var dynamicMethod = new DynamicMethod(""?? typeof(int)?? new[] { typeof(MyMath)?? typeof(int)?? typeof(int) })??
var il = dynamicMethod.GetILGenerator()??
il.Emit(OpCodes.Ldarg_0)??
il.Emit(OpCodes.Ldarg_1)??
il.Emit(OpCodes.Ldarg_2)??
il.Emit(OpCodes.Callvirt?? add)??
il.Emit(OpCodes.Ret)??
return (Func<MyMath?? int?? int?? int>)dynamicMethod.CreateDelegate(typeof(Func<MyMath?? int?? int?? int>))??
}
}

????Profiler ????д?????????????????

using System??
using System.Diagnostics??
public class Profiler ?? IDisposable {
private Stopwatch watch??
private string message??
private Profiler(string message) {
this.watch = new Stopwatch()??
this.watch.Start()??
this.message = message??
}
public void Dispose() {
watch.Stop()??
Console.WriteLine(message?? watch.ElapsedMilliseconds)??
Console.WriteLine()??
}
public static IDisposable Step(string message) {
return new Profiler(message)??
}
public static T Inline<T>(string message?? Func<T> func) {
using (new Profiler(message))
return func()??
}
}

???????

???????????????????????? .NET 2.0 ?? DynamicMethod??.NET 3.5 ????? Expression Tree??.NET 4 ?????? dynamic ???滻??????????????????????

????????????????????????????????????????????????????????????? .NET Framework ???Щ“??”?????