public static void Assocication(this PropertyDefinition self, CollectionProperty p1) { var name = p1.DisplayName + "-" + p1.RelationProperty.DisplayName; if (p1.RelationProperty is Property) { var p = p1.RelationProperty as Property; if (p.Owner != null && p.Owner.IsRuntimeDefine) { var pt = p.Owner; if (pt != null) { var relationType = pt.GetTypeDefintion(); if (relationType == null) { throw new Exception("错误,没有找到类型定义!"); } var relationPropertyDefine = relationType.Properties.Single(x => x.Name == p.称); relationPropertyDefine.Assocication(name); } else { throw new Exception("关联属性只能建立在用户定义的类型上面.系统内置的类型是无法更改的!如果需要建立已有类型的关联,可以继承该类型,并增加属性建立关联!"); } } else { throw new Exception("关联属性只能建立在用户定义的类型上面!"); } } self.Assocication(name); }
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); }