/// <summary> /// 定义属性的 Set 访问方法。 /// </summary> /// <param name="visual">指定方法的可见性。</param> /// <param name="calling">指定方法的调用属性。</param> /// <param name="fieldBuilder">指定一个属性相关的 <see cref="DynamicFieldBuilder"/>。</param> /// <returns>新的 <see cref="DynamicMethodBuilder"/>。</returns> public DynamicMethodBuilder DefineSetMethodByField(VisualDecoration visual = VisualDecoration.Public, CallingDecoration calling = CallingDecoration.Standard, DynamicFieldBuilder fieldBuilder = null) { var isInterface = Context.TypeBuilder is DynamicInterfaceBuilder; var method = new DynamicMethodBuilder(Context, string.Concat("set_", GetMethodName()), null, new[] { PropertyType }, visual, calling, ctx => { if (isInterface) { return; } if (fieldBuilder == null) { fieldBuilder = FieldBuilder; } ctx.Emitter.ldarg_0.ldarg_1.stfld(fieldBuilder.FieldBuilder).ret(); }); PropertyBuilder.SetSetMethod(method.MethodBuilder); return(method); }
/// <summary> /// 定义属性的 Get 访问方法。 /// </summary> /// <param name="visual">指定方法的可见性。</param> /// <param name="calling">指定方法的调用属性。</param> /// <param name="fieldBuilder">指定一个属性相关的 <see cref="DynamicFieldBuilder"/>。</param> /// <returns>新的 <see cref="DynamicMethodBuilder"/>。</returns> public DynamicMethodBuilder DefineGetMethodByField(VisualDecoration visual = VisualDecoration.Public, CallingDecoration calling = CallingDecoration.Standard, DynamicFieldBuilder fieldBuilder = null) { var isInterface = Context.TypeBuilder is DynamicInterfaceBuilder; var method = new DynamicMethodBuilder(Context, "get_" + Name, PropertyType, Type.EmptyTypes, ilCoding: ctx => { if (isInterface) { return; } if (fieldBuilder == null) { fieldBuilder = FieldBuilder; } ctx.Emitter.ldarg_0.ldfld(fieldBuilder.FieldBuilder).ret(); }); PropertyBuilder.SetGetMethod(method.MethodBuilder); return(method); }
/// <summary> /// 使用默认的过程定义属性的 Get 和 Set 方法体。 /// </summary> /// <param name="field">指定一个属性相关的 <see cref="DynamicFieldBuilder"/>。</param> /// <returns>当前的 <see cref="DynamicTypeBuilder"/>。</returns> public DynamicPropertyBuilder DefineGetSetMethods(DynamicFieldBuilder field = null) { DefineGetMethodByField(Visual, Calling, fieldBuilder: field); DefineSetMethodByField(Visual, Calling, fieldBuilder: field); return(this); }