internal override void Emit (EmitContext ec)
{
- ILGenerator ig = ec.ig;
+ if (Type.IsNullable ()) {
+ EmitNullableConstant (ec, Type, value);
+ return;
+ }
+
+ EmitConstant (ec, Type, value);
+ }
+
+ 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);
+ }
+ }
- switch (Type.GetTypeCode (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.DateTime: {
+ var date = (DateTime) value;
+ var local = ig.DeclareLocal (typeof (DateTime));
+
+ ig.Emit (OpCodes.Ldloca, local);
+ ig.Emit (OpCodes.Ldc_I8, date.Ticks);
+ ig.Emit (OpCodes.Ldc_I4, (int) date.Kind);
+ ig.Emit (OpCodes.Call, typeof (DateTime).GetConstructor (new [] { typeof (long), typeof (DateTimeKind) }));
+ ig.Emit (OpCodes.Ldloc, local);
+
+ 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;