Skip to content

Instantly share code, notes, and snippets.

@yhliaoluan
yhliaoluan / auto_clean
Created August 24, 2014 12:39
Auto clean resource using unique_ptr
int auto_clean() {
SOCKET socket = socket(AF_INET, SOCK_STREAM, 0);
unique_ptr<SOCKET, void (*)(SOCKET *)> ac(&socket,
[](SOCKET *s) { closesocket(*s); });
}
//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);