Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var local = il.DeclareLocal(typeof(Nullable<int>));
- il.Emit(OpCodes.Stloc, local);
- il.Emit(OpCodes.Ldloca, local);
- var method = typeof(Nullable<int>).GetMethod("get_HasValue");
- il.EmitCall(OpCodes.Call, method, null);
- il.Emit(/* not sure */);
- var method = typeof(Nullable<int>).GetMethod("get_HasValue");
- il.EmitCall(OpCodes.Call, method, null);
- var m = new DynamicMethod("M", typeof(bool), Type.EmptyTypes);
- var il = m.GetILGenerator();
- var ctor = typeof(Nullable<int>).GetConstructor(new[] { typeof(int) });
- il.Emit(OpCodes.Ldc_I4_6);
- il.Emit(OpCodes.Newobj, ctor);
- //var local = il.DeclareLocal(typeof(Nullable<int>));
- //il.Emit(OpCodes.Stloc, local);
- //il.Emit(OpCodes.Ldloca, local);
- var getValue = typeof(Nullable<int>).GetMethod("get_HasValue");
- il.Emit(OpCodes.Call, getValue);
- il.Emit(OpCodes.Ret);
- Console.WriteLine(m.Invoke(null, null));
- DynamicMethod method = new DynamicMethod("M", typeof(bool), Type.EmptyTypes);
- ILGenerator il = method.GetILGenerator();
- Type nullable = typeof(Nullable<int>);
- ConstructorInfo ctor = nullable.GetConstructor(new Type[] { typeof(int) });
- MethodInfo getValue = nullable.GetProperty("HasValue").GetGetMethod();
- LocalBuilder value = il.DeclareLocal(nullable);
- // load the variable to assign the value from the ctor to
- il.Emit(OpCodes.Ldloca_S, value);
- // load constructor args
- il.Emit(OpCodes.Ldc_I4_6);
- il.Emit(OpCodes.Call, ctor);
- il.Emit(OpCodes.Ldloca_S, value);
- il.Emit(OpCodes.Call, getValue);
- il.Emit(OpCodes.Ret);
- Console.WriteLine(method.Invoke(null, null));
Add Comment
Please, Sign In to add comment