1: private void GenerateFieldMapping(ILGenerator ILOut, FieldMapped mapping, Label EndGlobalIfLabel)
2: { 3: MethodInfo readerGetItem = typeof(SqlDataReader).GetMethod("get_Item", new Type[] { typeof(int) }); 4: MethodInfo stringEquals = typeof(string).GetMethod("Equals", new Type[] { typeof(string), typeof(string), typeof(StringComparison) }); 5: MethodInfo convertTo = typeof(MappingHelper).GetMethod("ConvertTo"); 6: MethodInfo getType = typeof(Type).GetMethod("GetTypeFromHandle"); 7: ConstructorInfo exConst = typeof(ApplicationException).GetConstructor(new Type[] { typeof(string) }); 8:
9: Label EndLocalIfLabel = ILOut.DefineLabel();
10: Label CallToConvert = ILOut.DefineLabel();
11: Label MakeAffectation = ILOut.DefineLabel();
12:
13: Type destType = mapping.Field != null ? mapping.Field.FieldType : mapping.Property.PropertyType;
14:
15: // Check if the current mapping field column name is equal to the current reader's column name
16: ILOut.Emit(OpCodes.Ldloc_2);
17: ILOut.Emit(OpCodes.Ldstr, mapping.MappingAttribute.ColumnName);
18: ILOut.Emit(OpCodes.Ldc_I4_3);
19: ILOut.Emit(OpCodes.Call, stringEquals);
20: ILOut.Emit(OpCodes.Brfalse, EndLocalIfLabel);
21:
22: // Get the item from the data reader for the current column
23: ILOut.Emit(OpCodes.Ldarg_1);
24: ILOut.Emit(OpCodes.Ldloc_1);
25: ILOut.Emit(OpCodes.Callvirt, readerGetItem);
26: ILOut.Emit(OpCodes.Stloc, 4);
27:
28: // Check if the current value is null (DBNull)
29: ILOut.Emit(OpCodes.Ldloc, 4);
30: ILOut.Emit(OpCodes.Isinst, typeof(DBNull));
31: ILOut.Emit(OpCodes.Ldnull);
32: ILOut.Emit(OpCodes.Cgt_Un);
33: ILOut.Emit(OpCodes.Ldc_I4_0);
34: ILOut.Emit(OpCodes.Ceq);
35: ILOut.Emit(OpCodes.Brtrue, CallToConvert);
36:
37: // Init to null the converted value if possible
38: if (destType.IsValueType && !destType.IsGenericType)
39: { 40: ILOut.Emit(OpCodes.Ldstr, "Field mapped on '" + mapping.MappingAttribute.ColumnName + "' cannot be null");
41: ILOut.Emit(OpCodes.Newobj, exConst);
42: ILOut.Emit(OpCodes.Throw);
43: }
44: else
45: { 46: ILOut.Emit(OpCodes.Ldnull);
47: ILOut.Emit(OpCodes.Stloc, 5);
48: }
49:
50: ILOut.Emit(OpCodes.Br, MakeAffectation);
51:
52: // Call the ConvertTo Method
53: ILOut.MarkLabel(CallToConvert);
54: if (destType.IsValueType && destType.IsGenericType)
55: { 56: ILOut.Emit(OpCodes.Ldtoken, destType.GetGenericArguments()[0]);
57: }
58: else
59: { 60: ILOut.Emit(OpCodes.Ldtoken, destType);
61: }
62: ILOut.Emit(OpCodes.Call, getType);
63: ILOut.Emit(OpCodes.Ldloc, 4);
64: ILOut.Emit(OpCodes.Call, convertTo);
65: ILOut.Emit(OpCodes.Stloc, 5);
66: ILOut.Emit(OpCodes.Br, MakeAffectation);
67:
68:
69: // Do the affectation of the computed value
70: ILOut.MarkLabel(MakeAffectation);
71:
72: ILOut.Emit(OpCodes.Ldarg_0);
73: ILOut.Emit(OpCodes.Ldloc, 5);
74:
75: if (destType.IsValueType)
76: { 77: ILOut.Emit(OpCodes.Unbox_Any, destType);
78: }
79: else
80: { 81: ILOut.Emit(OpCodes.Castclass, destType);
82: }
83:
84: if (mapping.Field != null)
85: { 86: ILOut.Emit(OpCodes.Stfld, mapping.Field);
87: }
88: else
89: { 90: ILOut.Emit(OpCodes.Callvirt, mapping.Property.GetSetMethod());
91: }
92:
93: ILOut.Emit(OpCodes.Ldloc_0);
94: ILOut.Emit(OpCodes.Ldc_I8, mapping.MappingAttribute.BitFieldIndex);
95: ILOut.Emit(OpCodes.Or);
96: ILOut.Emit(OpCodes.Stloc_0);
97:
98:
99: // Close the If-Else code
100: ILOut.Emit(OpCodes.Br, EndGlobalIfLabel);
101: ILOut.MarkLabel(EndLocalIfLabel);
102: }