Exemplo n.º 1
0
        private void ProcessPropertySetNormalCompareGeneric(IlBuilder processor)
        {
            processor.LoadInt(1);
            processor.LoadInt(0);
            var l1 = processor.Instructions[0];
            var l2 = processor.Instructions[1];

            processor.Instructions.Clear();

            var getValueOrDefault = _handler.Import(_pi.PropertyDefinition.PropertyType.GetMethod("GetValueOrDefault"));
            var getHasValue       = _handler.Import(_pi.PropertyDefinition.PropertyType.GetMethod("get_HasValue"));
            var type     = (GenericInstanceType)_pi.PropertyDefinition.PropertyType;
            var realType = type.GenericArguments[0].FullName;

            var v0 = processor.DeclareLocal(_pi.PropertyDefinition.PropertyType);
            var v1 = processor.DeclareLocal(_pi.PropertyDefinition.PropertyType);

            processor.DeclareLocal(_handler.BoolType);
            processor.LoadArg(1).SetLoc(0).LoadArg(0).LoadField(_pi.FieldDefinition).SetLoc(1).LoadLocala_S(v0);

            if (realType == KnownTypesHandler.Decimal)
            {
                processor.Call(getValueOrDefault).LoadLocala_S(v1).Call(getValueOrDefault)
                .Call(_handler.TypeDict[realType]);
                processor.BrTrue_S(l1).LoadLocala_S(v0).Call(getHasValue).LoadLocala_S(v1).Call(getHasValue);
                processor.Ceq().LoadInt(0).Ceq().Br_S(l2);
            }
            else if (_handler.TypeDict.ContainsKey(realType))
            {
                processor.LoadInt(0).Br_S(l2);
                var l4 = processor.Instructions[processor.Instructions.Count - 2];
                var l3 = processor.Instructions[processor.Instructions.Count - 1];
                processor.Instructions.Remove(l3);
                processor.Instructions.Remove(l4);
                processor.Call(getHasValue).LoadLocala_S(v1).Call(getHasValue).Bne_Un_S(l1);
                processor.LoadLocala_S(v0).Call(getHasValue).BrFalse_S(l4).LoadLocala_S(v0);
                processor.Call(getValueOrDefault).LoadLocala_S(v1).Call(getValueOrDefault)
                .Call(_handler.TypeDict[realType]).Br_S(l3);
                processor.Instructions.Add(l4);
                processor.Instructions.Add(l3);
            }
            else
            {
                processor.Call(getValueOrDefault);
                processor.ConvFloaty(realType);
                processor.LoadLocala_S(v1).Call(getValueOrDefault);
                processor.ConvFloaty(realType);
                processor.Bne_Un_S(l1).LoadLocala_S(v0).Call(getHasValue).LoadLocala_S(v1).Call(getHasValue);
                processor.Ceq().LoadInt(0).Ceq().Br_S(l2);
            }

            processor.Instructions.Add(l1);
            processor.Instructions.Add(l2);
            processor.Ceq().SetLoc(2).LoadLoc(2);
            processor.BrTrue_S(_pi.PropertyDefinition.SetMethod.Body.Instructions.LastItem());
        }
Exemplo n.º 2
0
        private PropertyDefinition GenerateProperty(PropertyDefinition property)
        {
            const MethodAttributes attr = MethodAttributes.Public | MethodAttributes.HideBySig | MethodAttributes.SpecialName |
                                          MethodAttributes.NewSlot | MethodAttributes.Virtual | MethodAttributes.Final;

            var p = TypeFactory.CreateProperty(property.Name, attr, property.PropertyType, _handler);

            var getter = new IlBuilder(p.GetMethod.Body);

            getter.DeclareLocal(property.PropertyType);
            getter.LoadArg(0);
            getter.LoadField(_result.Fields[0]);
            getter.Call(_model.Properties[_startIndex].GetMethod);
            getter.SetLoc(0);
            var i = getter.Processor.Create(OpCodes.Ldloc_0);

            getter.Br_S(i);
            getter.Instructions.Add(i);
            getter.Return();
            getter.Append();

            var setter = new IlBuilder(p.SetMethod.Body);

            setter.LoadArg(0);
            setter.LoadField(_result.Fields[0]);
            setter.LoadArg(1);
            setter.Call(_model.Properties[_startIndex].SetMethod);
            setter.Return();
            setter.Append();

            _startIndex++;
            return(p);
        }
Exemplo n.º 3
0
 private void ProcessPropertySetCallUpdateColumn(IlBuilder processor)
 {
     processor.LoadArg(0);
     processor.LoadString(_pi.ColumnName);
     processor.Call(_handler.ColumnUpdated);
     processor.InsertBefore(_pi.PropertyDefinition.SetMethod.Body.Instructions.LastItem());
 }
Exemplo n.º 4
0
        private void GenerateConstructor()
        {
            var method    = new MethodDefinition(".ctor", CtorAttr, _handler.VoidType);
            var processor = new IlBuilder(method.Body);

            processor.LoadArg(0);
            processor.Call(_handler.ModelHandlerBaseTypeCtor);
            processor.Return();
            processor.Append();
            _result.Methods.Add(method);
        }
Exemplo n.º 5
0
        private void GenerateConstructor(TypeDefinition type)
        {
            var method    = new MethodDefinition(".ctor", CtorAttr, _handler.VoidType);
            var processor = new IlBuilder(method.Body);

            processor.LoadArg(0);
            processor.Call(_handler.ObjectTypeCtor);
            processor.Return();
            processor.Append();
            type.Methods.Add(method);
        }
Exemplo n.º 6
0
 private void ProcessPropertySetNormalCompareValueType(IlBuilder processor)
 {
     processor.DeclareLocal(_handler.BoolType);
     processor.LoadArg(1).LoadArg(0).LoadField(_pi.FieldDefinition);
     if (_handler.TypeDict.ContainsKey(_pi.PropertyDefinition.PropertyType.FullName))
     {
         var mi = _handler.TypeDict[_pi.PropertyDefinition.PropertyType.FullName];
         processor.Call(mi).LoadInt(0);
     }
     processor.Ceq().SetLoc(0).LoadLoc(0);
     processor.BrTrue_S(_pi.PropertyDefinition.SetMethod.Body.Instructions.LastItem());
 }
Exemplo n.º 7
0
        private void GenerateSetKeyValueDirect()
        {
            var method = new MethodDefinition("SetKeyValueDirect", MethodAttr, _handler.VoidType);

            method.Overrides.Add(_handler.SetKeyValueDirect);
            method.Parameters.Add(new ParameterDefinition("obj", ParameterAttributes.None, _handler.ObjectType));
            method.Parameters.Add(new ParameterDefinition("key", ParameterAttributes.None, _handler.ObjectType));
            var processor = new IlBuilder(method.Body);

            if (_info.KeyMembers.Length == 1)
            {
                var h = _info.KeyMembers[0];
                processor.LoadArg(1).Cast(_model);
                processor.LoadArg(2);
                var fh = _info.KeyMembers[0];
                if (fh.MemberType == typeof(long))
                {
                    processor.Call(_handler.ConvertToInt64);
                }
                else if (fh.MemberType == typeof(int))
                {
                    processor.Call(_handler.ConvertToInt32);
                }
                else if (fh.MemberType == typeof(Guid))
                {
                    processor.Unbox(_handler.Import(h.MemberType));
                }
                else
                {
                    processor.Cast(_handler.Import(h.MemberType));
                }
                processor.SetMember(h, _handler);
            }

            processor.Return();
            processor.Append();
            _result.Methods.Add(method);
        }
Exemplo n.º 8
0
        private void GenerateConstructor()
        {
            var method = new MethodDefinition(".ctor", CtorAttr, _handler.VoidType);

            method.Parameters.Add(new ParameterDefinition("owner", ParameterAttributes.None, _model));
            var processor = new IlBuilder(method.Body);

            processor.LoadArg(0);
            processor.Call(_handler.ObjectTypeCtor);
            processor.LoadArg(0);
            processor.LoadArg(1);
            processor.SetField(_result.Fields[0]);
            processor.Return();
            processor.Append();
            _result.Methods.Add(method);
        }
Exemplo n.º 9
0
        private void GenerateLoadSimpleValuesByIndex()
        {
            var method = new MethodDefinition("LoadSimpleValuesByIndex", MethodAttr, _handler.VoidType);

            method.Overrides.Add(_handler.LoadSimpleValuesByIndex);
            method.Parameters.Add(new ParameterDefinition("o", ParameterAttributes.None, _handler.ObjectType));
            method.Parameters.Add(new ParameterDefinition("dr", ParameterAttributes.None, _handler.DataReaderInterface));
            var processor = new IlBuilder(method.Body);

            // 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).LoadInt(n);
                var mi1 = Helper.GetMethodInfo(f.MemberType);
                if (f.Is.AllowNull || mi1 == null)
                {
                    processor.CallVirtual(_handler.GetDataReaderMethodInt());
                    if (f.Is.AllowNull)
                    {
                        SetSecendArgForGetNullable(f, processor);
                        processor.Call(_handler.ModelHandlerBaseTypeGetNullable);
                    }
                    // cast or unbox
                    processor.CastOrUnbox(_handler.Import(f.MemberType), _handler);
                }
                else
                {
                    processor.CallVirtual(_handler.Import(mi1));
                }
                processor.SetMember(f, _handler);
                n++;
            }

            processor.Return();
            processor.Append();
            _result.Methods.Add(method);
        }
Exemplo n.º 10
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(_handler.ModelHandlerBaseTypeNewSpKeyValueDirect);
                        }
                        else
                        {
                            processor.LoadLoc(0);
                            processor.GetMember(f, _handler);
                            if (f.Is.BelongsTo)
                            {
                                processor.CallVirtual(_handler.Import(f.MemberType.GetMethod("get_ForeignKey")));
                            }
                            else if (f.Is.LazyLoad)
                            {
                                var it = f.MemberType.GetGenericArguments()[0];
                                processor.CallVirtual(_handler.Import(f.MemberType.GetMethod("get_Value")));
                                processor.Box(_handler.Import(it));
                            }
                            else
                            {
                                processor.Box(_handler.Import(f.MemberType));
                            }
                            processor.Call(_handler.ModelHandlerBaseTypeNewKeyValue);
                        }
                        processor.CallVirtual(_handler.KeyOpValueListAdd);
                    }
                    n++;
                }
            }
        }
Exemplo n.º 11
0
        private void GenerateSetValuesForPartialUpdate(IlBuilder processor)
        {
            // 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 (!f.Is.Key && (f.Is.UpdatedOn || f.Is.SavedOn || !f.Is.CreatedOn || f.Is.Count))
                    {
                        if (f.Is.UpdatedOn || f.Is.SavedOn || f.Is.Count)
                        {
                            processor.LoadArg(1).LoadArg(0).LoadInt(n)
                            .LoadInt(f.Is.Count ? 1 : 2)
                            .Call(_handler.ModelHandlerBaseTypeNewSpKeyValueDirect).CallVirtual(_handler.KeyOpValueListAdd);
                        }
                        else
                        {
                            processor.LoadArg(0).LoadArg(1).LoadLoc(0).LoadString(f.Name).LoadInt(n).LoadLoc(0);
                            processor.GetMember(f, _handler);
                            if (f.Is.BelongsTo)
                            {
                                processor.CallVirtual(_handler.Import(f.MemberType.GetMethod("get_ForeignKey")));
                            }
                            else if (f.Is.LazyLoad)
                            {
                                var it = f.MemberType.GetGenericArguments()[0];
                                processor.CallVirtual(_handler.Import(f.MemberType.GetMethod("get_Value")));
                                processor.Box(_handler.Import(it));
                            }
                            else
                            {
                                processor.Box(_handler.Import(f.MemberType));
                            }
                            processor.Call(_handler.ModelHandlerBaseTypeAddKeyValue);
                        }
                    }
                    n++;
                }
            }
        }