this.value = value;
}
+#if !FULL_AOT_RUNTIME
internal override void Emit (EmitContext ec)
{
- ILGenerator ig = ec.ig;
+ if (Type.IsNullable ()) {
+ EmitNullableConstant (ec, Type, value);
+ return;
+ }
+
+ EmitConstant (ec, Type, value);
+ }
- switch (Type.GetTypeCode (Type)){
+ void EmitNullableConstant (EmitContext ec, Type type, object value)
+ {
+ if (value == null) {
+ var local = ec.ig.DeclareLocal (type);
+ ec.EmitNullableInitialize (local);
+ } else {
+ EmitConstant (ec, type.GetFirstGenericArgument (), value);
+ ec.EmitNullableNew (type);
+ }
+ }
+
+ void EmitConstant (EmitContext ec, Type type, object value)
+ {
+ var ig = ec.ig;
+
+ switch (Type.GetTypeCode (type)){
case TypeCode.Byte:
ig.Emit (OpCodes.Ldc_I4, (int) ((byte)value));
return;
return;
}
+ case TypeCode.DBNull:
+ ig.Emit (OpCodes.Ldsfld, typeof (DBNull).GetField ("Value", BindingFlags.Public | BindingFlags.Static));
+ return;
+
case TypeCode.String:
EmitIfNotNull (ec, c => c.ig.Emit (OpCodes.Ldstr, (string) value));
return;
void EmitIfNotNull (EmitContext ec, Action<EmitContext> emit)
{
if (value == null) {
- var ig = ec.ig;
-
- if (Type.IsValueType) { // happens for nullable types
- var local = ig.DeclareLocal (Type);
- ig.Emit (OpCodes.Ldloca, local);
- ig.Emit (OpCodes.Initobj, Type);
- ig.Emit (OpCodes.Ldloc, local);
- } else {
- ec.ig.Emit (OpCodes.Ldnull);
- }
-
+ ec.ig.Emit (OpCodes.Ldnull);
return;
}
emit (ec);
}
+#endif
}
}