Skip to content

Instantly share code, notes, and snippets.

@yhliaoluan
Last active August 29, 2015 14:01
Show Gist options
  • Save yhliaoluan/c125ef4e31bb4b16aaee to your computer and use it in GitHub Desktop.
Save yhliaoluan/c125ef4e31bb4b16aaee to your computer and use it in GitHub Desktop.
//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);
}
}
@JeffreyZhao
Copy link

微信那里把姓名手机地址给我下吧。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment