public static PropertyDefinition BuildPropertyCore(Property property, TypeDefinition type) { var mod = type.Module; //从缓存中查找clr type var ptype = property.PropertyType.GetTypeReference(); var fieldInfo = new FieldDefinition("_" + property.名称, FieldAttributes.Private, ptype); type.Fields.Add(fieldInfo); // type.DefineField("_" + property.名称, ptype, FieldAttributes.Private); var propertyInfo = new PropertyDefinition(property.名称, PropertyAttributes.None, ptype) { HasThis = true }; type.Properties.Add(propertyInfo); #region 基本属性设置 if (property.Size != 100 && property.Size != 0) { propertyInfo.Size(property.Size); } //if (!string.IsNullOrEmpty(property.Expression)) //{ // propertyInfo.PersistentAlias(property.Expression); //} #endregion; #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 setattr = 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.MaxStackSize = 8; getMethod.Body.InitLocals = true; getMethodIl.Emit(OpCodes.Ldarg_0); getMethodIl.Emit(OpCodes.Ldfld, fieldInfo); getMethodIl.Emit(OpCodes.Ret); type.Methods.Add(getMethod); propertyInfo.GetMethod = getMethod; #endregion #region 非计算类型 if (string.IsNullOrEmpty(property.Expression)) { var set = new MethodDefinition("set_" + property.名称, setattr, mod.ImportReference(typeof (void))); var para = new ParameterDefinition("value", ParameterAttributes.None, ptype); set.Parameters.Add(para); var setPropertyValue = mod.ImportReference( typeof (PersistentBase) .GetMethods( SR.BindingFlags.InvokeMethod | SR.BindingFlags.NonPublic | SR.BindingFlags.Instance) .Single(x => x.Name.StartsWith("SetPropertyValue") && x.IsGenericMethod && x.GetParameters().Length == 3) ); var setPropertyValueMethod = new GenericInstanceMethod(setPropertyValue); setPropertyValueMethod.GenericArguments.Add(ptype); //setPropertyValue = setPropertyValue.MakeGenericMethod(ptype); var setIl = set.Body.Instructions; setIl.Add(Instruction.Create(OpCodes.Ldarg_0)); setIl.Add(Instruction.Create(OpCodes.Ldstr, property.名称)); setIl.Add(Instruction.Create(OpCodes.Ldarg_0)); setIl.Add(Instruction.Create(OpCodes.Ldflda, fieldInfo)); setIl.Add(Instruction.Create(OpCodes.Ldarg_1)); setIl.Add(Instruction.Create(OpCodes.Call, setPropertyValueMethod)); setIl.Add(Instruction.Create(OpCodes.Pop)); setIl.Add(Instruction.Create(OpCodes.Ret)); propertyInfo.SetMethod = set; type.Methods.Add(set); } #endregion return propertyInfo; }