示例#1
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);
        }
示例#2
0
 private static void ProcessPropertySetExclude(PropertyInformation pi, IlBuilder processor)
 {
     processor.LoadArg(0);
     processor.LoadArg(1);
     processor.SetField(pi.FieldDefinition);
     processor.Return();
     processor.Append();
 }
示例#3
0
 private void ProcessComposedOfInit(IlBuilder processor)
 {
     foreach (var kv in _coTypes)
     {
         processor.LoadArg(0);
         processor.LoadArg(0);
         processor.NewObj(kv.Key.GetFirstConstructor());
         processor.SetField(kv.Value);
     }
 }
示例#4
0
        private void ProcessPropertySetHasOneBelongsToLazyLoad(IlBuilder processor)
        {
            processor.LoadArg(0);
            processor.LoadField(_pi.FieldDefinition);
            processor.LoadArg(1);
            var sv       = _pi.FieldDefinition.FieldType.GetMethod("set_Value");
            var setValue = _handler.Import(sv);

            processor.CallVirtual(setValue);
            processor.Return();
            processor.Append();
        }
示例#5
0
 private void ProcessPropertySetNormal(IlBuilder processor)
 {
     processor.Return();
     processor.Append();
     if (ProcessorSettings.AddCompareToSetProperty)
     {
         ProcessPropertySetNormalCompare(processor);
     }
     processor.LoadArg(0);
     processor.LoadArg(1);
     processor.SetField(_pi.FieldDefinition);
     processor.InsertBefore(_pi.PropertyDefinition.SetMethod.Body.Instructions.LastItem());
 }
示例#6
0
        private void ProcessPropertySetNormalCompareString(IlBuilder processor)
        {
            processor.DeclareLocal(_handler.BoolType);
            processor.LoadArg(1);
            processor.LoadArg(0);
            processor.LoadField(_pi.FieldDefinition);
            var mi = _handler.TypeDict[_pi.PropertyDefinition.PropertyType.FullName];

            processor.Call(mi);
            processor.LoadInt(0);
            processor.Ceq();
            processor.SetLoc(0);
            processor.LoadLoc(0);
            processor.BrTrue_S(_pi.PropertyDefinition.SetMethod.Body.Instructions.LastItem());
        }
示例#7
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);
        }
示例#8
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());
 }
示例#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);
        }
示例#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++;
                }
            }
        }
示例#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++;
                }
            }
        }
示例#12
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());
        }
示例#13
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);
        }
示例#14
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);
        }
示例#15
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);
        }
示例#16
0
        private void GenerateSetValue(TypeDefinition type, MemberHandler member, TypeReference memberType)
        {
            var method = new MethodDefinition("SetValue", CtMethodAttr, _handler.VoidType);

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

            processor.LoadArg(1).Cast(_model).LoadArg(2).CastOrUnbox(memberType, _handler);
            processor.SetMember(member, _handler);
            processor.Return();
            processor.Append();
            type.Methods.Add(method);
        }
示例#17
0
        private void GenerateGetKeyValuesDirect()
        {
            var method = new MethodDefinition("GetKeyValuesDirect", MethodAttr, _handler.VoidType);

            method.Overrides.Add(_handler.GetKeyValuesDirect);
            method.Parameters.Add(new ParameterDefinition("dic", ParameterAttributes.None, _handler.DictionaryStringObjectType));
            method.Parameters.Add(new ParameterDefinition("o", ParameterAttributes.None, _handler.ObjectType));
            var processor = new IlBuilder(method.Body);

            // User u = (User)o;
            processor.DeclareLocal(_model);
            processor.LoadArg(2).Cast(_model).SetLoc(0);
            // set values
            foreach (var f in _info.KeyMembers)
            {
                processor.LoadArg(1).LoadString(f.Name).LoadLoc(0);
                processor.GetMember(f, _handler);
                processor.Box(_handler.Import(f.MemberType)).CallVirtual(_handler.DictionaryStringObjectAdd);
            }

            processor.Return();
            processor.Append();
            _result.Methods.Add(method);
        }
示例#18
0
 private void ProcessSerializable()
 {
     if (_model.IsSerializable && !_model.IsAbstract)
     {
         _model.Interfaces.Add(_handler.SerializableInterface);
         var method = new MethodDefinition("System.Runtime.Serialization.ISerializable.GetObjectData", FlagGetObjectData, _handler.VoidType);
         method.Overrides.Add(_handler.SerializableGetObjectData);
         method.Parameters.Add(new ParameterDefinition("info", ParameterAttributes.None, _handler.SerializationInfoType));
         method.Parameters.Add(new ParameterDefinition("context", ParameterAttributes.None, _handler.StreamingContextType));
         var processor = new IlBuilder(method.Body);
         processor.LoadArg(0).LoadArg(1).LoadArg(2).Call(_handler.DynamicObjectReferenceSerializeObject);
         processor.Return();
         processor.Append();
         _model.Methods.Add(method);
     }
 }
示例#19
0
        private void ProcessConstructor(MethodDefinition constructor)
        {
            var processor = new IlBuilder(constructor.Body);

            foreach (var pi in _properties)
            {
                if (pi.FieldType != FieldType.Normal)
                {
                    ProcessGenericPropertyInConstructor(pi, processor);
                }
            }
            if (!_model.IsAbstract)
            {
                processor.LoadArg(0).Call(_handler.InitUpdateColumns);
            }
            ProcessComposedOfInit(processor);
            var target = GetCallBaseCtor(constructor);

            processor.InsertAfter(target);
        }
示例#20
0
        private void GenerateGetValue(TypeDefinition type, MemberHandler member, TypeReference memberType)
        {
            var method = new MethodDefinition("GetValue", CtMethodAttr, _handler.ObjectType);

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

            processor.DeclareLocal(_handler.ObjectType);
            processor.LoadArg(1).Cast(_model);
            processor.GetMember(member, _handler);
            if (member.MemberInfo.MemberType.IsValueType)
            {
                processor.Box(memberType);
            }
            processor.SetLoc(0);
            processor.LoadLoc(0);
            processor.Return();
            processor.Append();
            type.Methods.Add(method);
        }
示例#21
0
        private void ProcessGenericPropertyInConstructor(PropertyInformation pi, IlBuilder processor)
        {
            processor.LoadArg(0).LoadArg(0);
            MethodReference ci1;
            var             ft = (GenericInstanceType)pi.FieldDefinition.FieldType;

            if (pi.IsHasOne || pi.IsHasMany || pi.IsHasAndBelongsToMany)
            {
                ci1 = ft.GetConstructor(typeof(DbObjectSmartUpdate), typeof(string), typeof(string));
                var ob = GetOrderByString(pi);
                if (string.IsNullOrEmpty(ob))
                {
                    processor.LoadNull();
                }
                else
                {
                    processor.LoadString(ob);
                }
            }
            else
            {
                ci1 = ft.GetConstructor(typeof(DbObjectSmartUpdate), typeof(string));
            }
            if (pi.IsLazyLoad)
            {
                processor.LoadString(pi.ColumnName);
            }
            else
            {
                processor.Nop().Nop().Nop().Nop().LoadString(pi.FieldDefinition.Name);
            }
            var ctor = _handler.Import(ci1);

            ctor.DeclaringType = ft; //NOTE: might be a bug of Cecil
            processor.NewObj(ctor);
            processor.SetField(pi.FieldDefinition);
        }
示例#22
0
        private void GenerateGetKeyValueDirect()
        {
            var method = new MethodDefinition("GetKeyValueDirect", MethodAttr, _handler.ObjectType);

            method.Overrides.Add(_handler.GetKeyValueDirect);
            method.Parameters.Add(new ParameterDefinition("o", 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.GetMember(h, _handler);
                processor.Box(_handler.Import(h.MemberType));
            }
            else
            {
                processor.LoadNull();
            }

            processor.Return();
            processor.Append();
            _result.Methods.Add(method);
        }
示例#23
0
        private void GenerateSetValuesForSelectDirectDirect(string methodName, bool noLazy)
        {
            var method = new MethodDefinition(methodName, MethodAttr, _handler.VoidType);

            method.Overrides.Add(noLazy ? _handler.SetValuesForSelectDirectNoLazy : _handler.SetValuesForSelectDirect);
            method.Parameters.Add(new ParameterDefinition("keys", ParameterAttributes.None, _handler.ListKeyValuePairStringStringType));
            var processor = new IlBuilder(method.Body);

            foreach (var f in _info.Members)
            {
                if (!f.Is.HasOne && !f.Is.HasMany && !f.Is.HasAndBelongsToMany)
                {
                    if (noLazy || !f.Is.LazyLoad)
                    {
                        processor.LoadArg(1);

                        processor.LoadString(f.Name);
                        if (f.Name != f.MemberInfo.Name)
                        {
                            processor.LoadString(f.MemberInfo.Name);
                        }
                        else
                        {
                            processor.LoadNull();
                        }
                        processor.NewObj(_handler.KeyValuePairStringStringCtor);

                        processor.CallVirtual(_handler.ListKeyValuePairStringStringAdd);
                    }
                }
            }

            processor.Return();
            processor.Append();
            _result.Methods.Add(method);
        }
示例#24
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 = new MethodDefinition(methodName, MethodAttr, _handler.VoidType);

            method.Overrides.Add(useIndex
                ? (noLazy ? _handler.LoadRelationValuesByIndexNoLazy : _handler.LoadRelationValuesByIndex)
                : (noLazy ? _handler.LoadRelationValuesByNameNoLazy : _handler.LoadRelationValuesByName));
            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);

            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, _handler);
                            processor.LoadArg(2);
                            if (useIndex)
                            {
                                processor.LoadInt(index++).CallVirtual(_handler.GetDataReaderMethodInt());
                            }
                            else
                            {
                                processor.LoadString(f.Name).CallVirtual(_handler.GetDataReaderMethodString());
                            }
                            processor.LoadInt(0);
                            processor.CallVirtual(_handler.LazyLoadingInterfaceWrite);
                        }
                    }
                    else if (f.Is.BelongsTo)
                    {
                        processor.LoadLoc(0);
                        processor.GetMember(f, _handler);
                        processor.LoadArg(2);
                        if (useIndex)
                        {
                            processor.LoadInt(index++).CallVirtual(_handler.GetDataReaderMethodInt());
                        }
                        else
                        {
                            processor.LoadString(f.Name).CallVirtual(_handler.GetDataReaderMethodString());
                        }
                        processor.CallVirtual(_handler.BelongsToInterfaceSetForeignKey);
                    }
                }
            }

            processor.Return();
            processor.Append();
            _result.Methods.Add(method);
        }