Пример #1
0
        public static DynamicBuilder <T> CreateBuilder(IDataRecord dataRecord)
        {
            var dynamicBuilder = new DynamicBuilder <T>();

            var method = new DynamicMethod("DynamicCreate", typeof(T), new[] { typeof(IDataRecord) },
                                           typeof(T), true);
            var generator = method.GetILGenerator();

            var result = generator.DeclareLocal(typeof(T));

            if (IsSingleType(typeof(T)))
            {
                generator.Emit(OpCodes.Ldarg_0);
                generator.Emit(OpCodes.Ldc_I4, 0);
                generator.Emit(OpCodes.Callvirt, getValueMethod);
                generator.Emit(OpCodes.Unbox_Any, dataRecord.GetFieldType(0));

                generator.Emit(OpCodes.Ret);
            }
            else
            {
                generator.Emit(OpCodes.Newobj, typeof(T).GetConstructor(Type.EmptyTypes));
                generator.Emit(OpCodes.Stloc, result);
                for (var i = 0; i < dataRecord.FieldCount; i++)
                {
                    var propertyInfo = typeof(T).GetProperty(dataRecord.GetName(i), BindingFlags.Instance | BindingFlags.Public | BindingFlags.IgnoreCase);
                    var endIfLabel   = generator.DefineLabel();

                    if (propertyInfo == null)
                    {
                        continue;
                    }
                    var proMethod = propertyInfo.GetSetMethod();
                    if (proMethod == null)
                    {
                        continue;
                    }
                    generator.Emit(OpCodes.Ldarg_0);
                    generator.Emit(OpCodes.Ldc_I4, i);
                    generator.Emit(OpCodes.Callvirt, isDBNullMethod);
                    generator.Emit(OpCodes.Brtrue, endIfLabel);

                    generator.Emit(OpCodes.Ldloc, result);
                    generator.Emit(OpCodes.Ldarg_0);
                    generator.Emit(OpCodes.Ldc_I4, i);
                    generator.Emit(OpCodes.Callvirt, getValueMethod);
                    generator.Emit(OpCodes.Unbox_Any, propertyInfo.PropertyType);
                    generator.Emit(OpCodes.Callvirt, proMethod);

                    generator.MarkLabel(endIfLabel);
                }

                generator.Emit(OpCodes.Ldloc, result);
                generator.Emit(OpCodes.Ret);
            }

            dynamicBuilder._handler = (Load)method.CreateDelegate(typeof(Load));
            return(dynamicBuilder);
        }
Пример #2
0
        private static List <T> MappingReaderToList <T>(IDbCommand dbCommand)
        {
            using (var dataReader = dbCommand.ExecuteReader())
            {
                if (dataReader.FieldCount == 0)
                {
                    return(null);
                }
                var recordList = new List <T>();

                var builder = DynamicBuilder <T> .CreateBuilder(dataReader);

                while (dataReader.Read())
                {
                    var record = builder.Build(dataReader);
                    recordList.Add(record);
                }

                return(recordList);
            }
        }
Пример #3
0
        private static T MappingReadToSingle <T>(IDbCommand dbCommand)
        {
            using (var dataReader = dbCommand.ExecuteReader())
            {
                if (dataReader.FieldCount == 0)
                {
                    return(default(T));
                }

                var builder = DynamicBuilder <T> .CreateBuilder(dataReader);

                if (!dataReader.Read())
                {
                    return(default(T));
                }
                var record = builder.Build(dataReader);

                if (dataReader.Read())
                {
                    throw new Exception("The result is not single row.");
                }
                return(record);
            }
        }