Пример #1
0
        private void GenerateLoadSimpleValues(bool byIndex)
        {
            var name   = byIndex ? "LoadSimpleValuesByIndex" : "LoadSimpleValuesByName";
            var method = _result.DefineMethod(name, MethodAttr,
                                              null, new Type[] { objectType, iDataReaderType });

            _result.DefineMethodOverride(method, byIndex
                ? handlerBaseLoadSimpleValuesByIndex
                : handlerBaseLoadSimpleValuesByName);
            var processor = new ILBuilder(method.GetILGenerator());

            // User u = (User)o;
            processor.DeclareLocal(_model);
            processor.LoadArg(1).Cast(_model).SetLoc(0);
            // set values
            int n = 0;

            foreach (var f in _info.SimpleMembers)
            {
                processor.LoadLoc(0);
                if (f.Is.AllowNull)
                {
                    processor.LoadArg(0);
                }
                processor.LoadArg(2);
                if (byIndex)
                {
                    processor.LoadInt(n);
                }
                else
                {
                    processor.LoadArg(2).LoadString(f.Name).CallVirtual(dataReaderGetOrdinal);
                }
                var mi1 = DataReaderEmitHelper.GetMethodInfo(f.MemberType);
                if (f.Is.AllowNull || mi1 == null)
                {
                    processor.CallVirtual(dataReaderMethodInt);
                    if (f.Is.AllowNull)
                    {
                        SetSecendArgForGetNullable(f, processor);
                        processor.Call(handlerBaseTypeGetNullable);
                    }
                    // cast or unbox
                    processor.CastOrUnbox(f.MemberType);
                }
                else
                {
                    processor.CallVirtual(mi1);
                }
                processor.SetMember(f);
                n++;
            }

            processor.Return();
        }