private void EmitNullableToNullableConversion(Type typeFrom, Type typeTo, bool isChecked) { using (_ilg.UseTempLocal(typeFrom, out var local)) using (_ilg.UseTempLocal(typeTo, out var local2)) { _ilg.Stloc(local); _ilg.Ldloca(local); EmitHasValue(typeFrom); var brfs = Instruction.Create(OpCodes.Brfalse_S); _ilg.Emit(brfs); _ilg.Ldloca(local); EmitGetValueOrDefault(typeFrom); var nonNullableType = typeFrom.GetNonNullableType(); var nonNullableType2 = typeTo.GetNonNullableType(); EmitConvertToType(nonNullableType, nonNullableType2, isChecked); var constructor = typeTo.GetConstructor(new[] { nonNullableType2 }); _ilg.Newobj(constructor); _ilg.Stloc(local2); var brs = Instruction.Create(OpCodes.Br_S); _ilg.Emit(brs); var lt = Instruction.Create(OpCodes.Ldloca, local2); brfs.Operand = lt; _ilg.Emit(lt); _ilg.Initobj(typeTo); var lt2 = Instruction.Create(OpCodes.Ldloc, local2); brs.Operand = lt2; _ilg.Emit(lt2); } }
public static void RandomConst(IILEmitter il, TypeSig t, IRng rng) { switch (t.ElementType) { case ElementType.I1: il.Const(rng.NextInt8()); break; case ElementType.I2: il.Const(rng.NextInt16()); break; case ElementType.I4: il.Const(rng.NextInt32()); break; case ElementType.I8: il.Const(rng.NextInt64()); break; case ElementType.U1: il.Const(rng.NextUInt8()); break; case ElementType.U2: il.Const(rng.NextUInt16()); break; case ElementType.U4: il.Const(rng.NextUInt32()); break; case ElementType.U8: il.Const(rng.NextUInt64()); break; case ElementType.R4: il.Const(rng.NextFloat()); break; case ElementType.R8: il.Const(rng.NextDouble()); break; case ElementType.Char: il.Const((char)rng.NextUInt16()); break; case ElementType.Boolean: il.Const(rng.NextBoolean()); break; default: if (t.IsValueType) { var it = il.Importer.Import(t); using (il.UseTempLocal(it, out var l)) { il.Ldloca(l); il.Initobj(new TypeSpecUser(it)); il.Ldloc(l); } } else { il.Emit(OpCodes.Ldnull); } break; } }