private FieldBuilder getFieldBuilder(PropertyBuilding pBuilding) { //backing field string _name = pBuilding.propertyBuilder.Name; Type _type = pBuilding.propertyBuilder.PropertyType; FieldBuilder _fBuilder = pBuilding.backFieldBuilder; if (_fBuilder == null) { _fBuilder = _tBuilder.DefineField($"m_{_name}", _type, FieldAttributes.Private); pBuilding.backFieldBuilder = _fBuilder; } return(_fBuilder); }
public IGetOrSetAdded AddAutoGet() { PropertyBuilding _pBuild = _propBuilds.Last(); string _name = _pBuild.propertyBuilder.Name; Type _type = _pBuild.propertyBuilder.PropertyType; //backing field FieldBuilder _fBuilder = getFieldBuilder(_pBuild); //get method MethodAttributes _propAttrs = GetGetSetAttrs(_name); MethodBuilder _getBuilder = _tBuilder.DefineMethod($"get_{_name}", _propAttrs, CallingConventions.HasThis, _type, Type.EmptyTypes); ILGenerator _getGen = _getBuilder.GetILGenerator(); _getGen.Emit(OpCodes.Ldarg_0); _getGen.Emit(OpCodes.Ldfld, _fBuilder); _getGen.Emit(OpCodes.Ret); _pBuild.getBuilder = _getBuilder; return(this); }
public IGetOrSetAdded AddAutoSet() { PropertyBuilding _pBuild = _propBuilds.Last(); string _name = _pBuild.propertyBuilder.Name; Type _type = _pBuild.propertyBuilder.PropertyType; //backing field FieldBuilder _fBuilder = getFieldBuilder(_pBuild); //set method MethodAttributes _propAttrs = GetGetSetAttrs(_name); MethodBuilder _setBuilder = _tBuilder.DefineMethod($"set_{_name}", _propAttrs, null, new Type[] { _type }); ILGenerator _setGen = _setBuilder.GetILGenerator(); _setGen.Emit(OpCodes.Ldarg_0); _setGen.Emit(OpCodes.Ldarg_1); _setGen.Emit(OpCodes.Stfld, _fBuilder); _setGen.Emit(OpCodes.Ret); _pBuild.setBuilder = _setBuilder; return(this); }
public IGetOrSetAdded AddGet <T>(Expression <Func <T> > methBody) { //get last prop PropertyBuilding _pBuild = _propBuilds.Last(); string _name = _pBuild.propertyBuilder.Name; Type _type = _pBuild.propertyBuilder.PropertyType; //static method, get body MethodAttributes _statAttrs = MethodAttributes.Public | MethodAttributes.NewSlot | MethodAttributes.Final | MethodAttributes.Static; MethodBuilder _statMethBuilder = _tBuilder.DefineMethod($"stat_get_{_name}", _statAttrs); methBody.CompileToMethod(_statMethBuilder); //get method MethodAttributes _attrs = GetGetSetAttrs(_name); MethodBuilder _getBuilder = _tBuilder.DefineMethod($"get_{_name}", _attrs, CallingConventions.HasThis, methBody.ReturnType, Type.EmptyTypes); ILGenerator _mGen = _getBuilder.GetILGenerator(); _mGen.Emit(OpCodes.Call, _statMethBuilder); _mGen.Emit(OpCodes.Ret); _pBuild.getBuilder = _getBuilder; return(this); }