Example #1
0
        private void GenerateNewMemberWithPrivateSetter(MemberHandler pi, ILBuilder processor, ILBuilder ctorBuilder)
        {
            var dt = typeof(Action <,>).MakeGenericType(_model, pi.MemberType);
            var df = _result.DefineField("$" + pi.MemberInfo.Name, dt, fieldFlag);

            ctorBuilder.LoadArg(0);
            ctorBuilder.LoadToken(dt);
            ctorBuilder.Call(getTypeFromHandle);
            ctorBuilder.LoadLoc(0);
            ctorBuilder.LoadString(pi.MemberInfo.Name);
            ctorBuilder.CallVirtual(getProperty);
            ctorBuilder.LoadInt(1);
            ctorBuilder.CallVirtual(getSetMethod);
            ctorBuilder.Call(createDelegate);
            ctorBuilder.Cast(dt);
            ctorBuilder.SetField(df);

            processor.LoadArg(0);
            processor.LoadField(df);
            processor.LoadArg(1);
            GenerateNewMember(pi, processor);
            var invoke = dt.GetMethod("Invoke");

            processor.CallVirtual(invoke);
        }
Example #2
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();
        }
Example #3
0
 private static void SetSecendArgForGetNullable(MemberHandler f, ILBuilder il)
 {
     if (f.MemberType.IsValueType && f.MemberType == typeof(Guid?))
     {
         il.LoadInt(1);
     }
     else if (f.MemberType.IsValueType && f.MemberType == typeof(bool?))
     {
         il.LoadInt(2);
     }
     else if (f.MemberType.IsValueType && f.MemberType == typeof(Date?))
     {
         il.LoadInt(3);
     }
     else if (f.MemberType.IsValueType && f.MemberType == typeof(Time?))
     {
         il.LoadInt(4);
     }
     else
     {
         il.LoadInt(0);
     }
 }
Example #4
0
        private void GenerateSetValuesDirect(ILBuilder processor, Func <MemberHandler, bool> cb1, Func <MemberHandler, bool> cb2)
        {
            // User u = (User)o;
            processor.DeclareLocal(_model);
            processor.LoadArg(2).Cast(_model).SetLoc(0);
            // set values
            int n = 0;

            foreach (var f in _info.Members)
            {
                if (!f.Is.DbGenerate && !f.Is.HasOne && !f.Is.HasMany && !f.Is.HasAndBelongsToMany)
                {
                    if (!cb1(f))
                    {
                        processor.LoadArg(1).LoadArg(0).LoadInt(n);
                        if (cb2(f))
                        {
                            processor.LoadInt(f.Is.Count ? 1 : 2)
                            .Call(handlerBaseTypeNewSpKeyValueDirect);
                        }
                        else
                        {
                            processor.LoadLoc(0);
                            processor.GetMember(f);
                            if (f.Is.BelongsTo)
                            {
                                processor.CallVirtual(f.MemberType.GetMethod("get_ForeignKey"));
                            }
                            else if (f.Is.LazyLoad)
                            {
                                var it = f.MemberType.GetGenericArguments()[0];
                                processor.CallVirtual(f.MemberType.GetMethod("get_Value"));
                                processor.Box(it);
                            }
                            else
                            {
                                processor.Box(f.MemberType);
                            }
                            processor.Call(handlerBaseTypeNewKeyValue);
                        }
                        processor.CallVirtual(keyOpValueListAdd);
                    }
                    n++;
                }
            }
        }
Example #5
0
        private void GenerateLoadRelationValues(bool useIndex, bool noLazy)
        {
            int    index      = _info.SimpleMembers.Length;
            string methodName = useIndex ? "LoadRelationValuesByIndex" : "LoadRelationValuesByName";

            if (noLazy)
            {
                methodName = methodName + "NoLazy";
            }

            var method = _result.DefineMethod(methodName, MethodAttr,
                                              null, new Type[] { objectType, iDataReaderType });

            _result.DefineMethodOverride(method, useIndex
                ? (noLazy ? handlerBaseLoadRelationValuesByIndexNoLazy : handlerBaseLoadRelationValuesByIndex)
                : (noLazy ? handlerBaseLoadRelationValuesByNameNoLazy : handlerBaseLoadRelationValuesByName));
            var processor = new ILBuilder(method.GetILGenerator());

            if (_info.RelationMembers.Length > 0)
            {
                // User u = (User)o;
                processor.DeclareLocal(_model);
                processor.LoadArg(1).Cast(_model).SetLoc(0);
                // set values
                foreach (var f in _info.RelationMembers)
                {
                    if (f.Is.LazyLoad)
                    {
                        if (noLazy)
                        {
                            processor.LoadLoc(0);
                            processor.GetMember(f);
                            processor.LoadArg(2);
                            if (useIndex)
                            {
                                processor.LoadInt(index++).CallVirtual(dataReaderMethodInt);
                            }
                            else
                            {
                                processor.LoadString(f.Name).CallVirtual(dataReaderMethodString);
                            }
                            processor.LoadInt(0);
                            processor.CallVirtual(lazyLoadingInterfaceWrite);
                        }
                    }
                    else if (f.Is.BelongsTo)
                    {
                        processor.LoadLoc(0);
                        processor.GetMember(f);
                        processor.LoadArg(2);
                        if (useIndex)
                        {
                            processor.LoadInt(index++).CallVirtual(dataReaderMethodInt);
                        }
                        else
                        {
                            processor.LoadString(f.Name).CallVirtual(dataReaderMethodString);
                        }
                        processor.CallVirtual(belongsToInterfaceSetForeignKey);
                    }
                }
            }

            processor.Return();
        }