I tried this code to copy values from another object a deep clone but it doesnt seem to like nullable properties and I cannot figure out why
public static TConvert ConvertTo<TConvert>(this object entity) where TConvert : new()
{
var convertProperties = TypeDescriptor.GetProperties(typeof(TConvert)).Cast<PropertyDescriptor>();
var entityProperties = TypeDescriptor.GetProperties(entity).Cast<PropertyDescriptor>();
var convert = new TConvert();
foreach (var entityProperty in entityProperties)
{
var property = entityProperty;
var convertProperty = convertProperties.FirstOrDefault(prop => prop.Name == property.Name);
if (convertProperty != null)
{
convertProperty.SetValue(convert,
Convert.ChangeType(entityProperty.GetValue(entity),
convertProperty.PropertyType)); }
}
return convert;
}
I please a try catch around it and it brought me to this property which does exist in both models.
public int? PullUpHolds { get; set; }
How would I modify the above to take that into account I tried removing the if statment but that still caused a exception on the clone.
My Usuage is
private async void btnEndSession_Clicked(object sender, EventArgs e)
{
var item = dgWeightLifting.SelectedItem as WeightLifting;
if(item != null)
{
var removePlayer= await DisplayAlert(Constants.AppName,
$"This will remove the player {item.Players.FullName}
from the weight lifting screen. We will make a final
record for this session in work out history proceed",
"OK", "Cancel");
if (removePlayer)
{
var test =item.ConvertTo<Workout>();
}
}
}