Last active
August 29, 2015 14:01
-
-
Save yhliaoluan/c125ef4e31bb4b16aaee to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//https://gist.github.com/JeffreyZhao/297760dcff8c066606e8 | |
public class Parser<T, TResult> { | |
public static Func<T, TResult> Func = Emit(); | |
private static Func<T, TResult> Emit() { | |
var method = new DynamicMethod(string.Empty, typeof(TResult), new[] { typeof(T) }); | |
var il = method.GetILGenerator(); | |
il.Emit(OpCodes.Ldarg_0); | |
il.Emit(OpCodes.Ret); | |
return (Func<T, TResult>)method.CreateDelegate(typeof(Func<T, TResult>)); | |
} | |
} | |
public interface ICaller { | |
TResult Call<T, TResult>(T arg); | |
} | |
public class TicksToDateTimeCaller : ICaller { | |
public TResult Call<T, TResult>(T arg) { | |
if (typeof(T) != typeof(long) || typeof(TResult) != typeof(DateTime)) | |
throw new NotSupportedException(); | |
return Parser<DateTime, TResult>.Func(TicksToDateTime(Parser<T, long>.Func(arg))); | |
} | |
public DateTime TicksToDateTime(long ticks) { | |
return new DateTime(ticks); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
微信那里把姓名手机地址给我下吧。