public PropertyDefinition BuildCollectionProperty(CollectionProperty property,TypeDefinition type) { //如果是一对多关系,即关联属性是单值属性时,直接在上面加上关联关系 //如果是多对多,则不需要处理,因为所属类型会执行这个动作 var mod = type.Module; var elementType = property.PropertyType.GetTypeReference(); var ptype = mod.ImportReference(typeof(XPCollection<>)).MakeGenericType(elementType); var propertyInfo = new PropertyDefinition(property.名称, PropertyAttributes.None, ptype) { HasThis = true }; type.Properties.Add(propertyInfo); propertyInfo.Assocication(property); if (property.Aggregated) { propertyInfo.Aggregate(); } #region getter //.method public hidebysig specialname instance string get_创建者() cil managedMono.Cecil.MethodAttributes.Public | Mono.Cecil.MethodAttributes.HideBySig | Mono.Cecil.MethodAttributes.SpecialName var attr = MethodAttributes.Public | MethodAttributes.HideBySig | MethodAttributes.SpecialName | MethodAttributes.FamANDAssem | MethodAttributes.Family; var getMethod = new MethodDefinition("get_" + property.名称, attr, ptype); var getMethodIl = getMethod.Body.GetILProcessor(); getMethod.Body.Variables.Add(new VariableDefinition(ptype)); getMethod.Body.MaxStackSize = 8; getMethod.Body.InitLocals = true; //.maxstack 2 //.locals init ([0] class [DevExpress.Xpo.v15.2]DevExpress.Xpo.XPCollection`1<class IMatrix.ERP.Module.BusinessObjects.员工> xps) //L_0000: nop //L_0001: ldarg.0 //L_0002: ldstr "\u8054\u7cfb\u4eba" //L_0007: call instance class [DevExpress.Xpo.v15.2]DevExpress.Xpo.XPCollection`1<!!0> [DevExpress.Xpo.v15.2]DevExpress.Xpo.XPBaseObject::GetCollection<class IMatrix.ERP.Module.BusinessObjects.员工>(string) //L_000c: stloc.0 //L_000d: br.s L_000f //L_000f: ldloc.0 //L_0010: ret getMethodIl.Emit(OpCodes.Nop); getMethodIl.Emit(OpCodes.Ldarg_0); getMethodIl.Emit(OpCodes.Ldstr, property.名称); var clrGetCollection = typeof(SimpleObject).GetMethods(SR.BindingFlags.NonPublic | SR.BindingFlags.Instance).Single(x => x.Name == "GetCollection" && x.ReturnType.IsGenericType); var getCollection = mod.ImportReference(clrGetCollection); //(tb as TypeDefinition).Methods.Single(x => x.Name == ".ctor" && x.Parameters.First().ParameterType.FullName == typeof(Session).FullName); getCollection = getCollection.MakeGenericMethod(elementType); getMethodIl.Emit(OpCodes.Call, getCollection); getMethodIl.Emit(OpCodes.Stloc_0); var ldloc0 = getMethodIl.Create(OpCodes.Ldloc_0); getMethodIl.Emit(OpCodes.Br_S, ldloc0); getMethodIl.Append(ldloc0); getMethodIl.Emit(OpCodes.Ret); type.Methods.Add(getMethod); propertyInfo.GetMethod = getMethod; #endregion return propertyInfo; }