private int AddSetValueCall(PropertyDefinition property, FieldReference fieldReference, bool isReadOnly)
{
FodyEnvironment.LogDebug($"\t\t\t{property.Name} - adding SetValue call");
//string fieldName = string.Format("{0}Property", property.Name);
//var declaringType = property.DeclaringType;
//var fieldReference = GetField(declaringType, fieldName);
// Writes SetValue(PropertyData propertyName, object value)
if (property.SetMethod == null)
{
var voidType = _msCoreReferenceFinder.GetCoreTypeReference("Void");
var setMethod = new MethodDefinition($"set_{property.Name}", MethodAttributes.Public, property.DeclaringType.Module.Import(voidType));
setMethod.Parameters.Add(new ParameterDefinition(property.PropertyType.Import()));
property.DeclaringType.Methods.Add(setMethod);
setMethod.MarkAsCompilerGenerated(_msCoreReferenceFinder);
property.SetMethod = setMethod;
}
var finalSetMethod = property.SetMethod;
if (isReadOnly)
{
finalSetMethod.IsPrivate = true;
finalSetMethod.IsPublic = false;
}
var body = property.SetMethod.Body;
body.SimplifyMacros();
var instructions = body.Instructions;
instructions.Clear();
var instructionsToAdd = new List<Instruction>();
instructionsToAdd.AddRange(new[]
{
Instruction.Create(OpCodes.Ldarg_0),
Instruction.Create(OpCodes.Ldsfld, fieldReference),
Instruction.Create(OpCodes.Ldarg_1)
});
if (property.PropertyType.IsBoxingRequired(_catelType.SetValueInvoker.Parameters[1].ParameterType))
{
instructionsToAdd.Add(Instruction.Create(OpCodes.Box, property.PropertyType.Import()));
}
instructionsToAdd.Add(Instruction.Create(OpCodes.Call, _catelType.SetValueInvoker));
instructionsToAdd.Add(Instruction.Create(OpCodes.Ret));
var finalIndex = instructions.Insert(0, instructionsToAdd.ToArray());
body.OptimizeMacros();
return finalIndex;
}