У меня был аналогичный сценарий, когда мне нужно было назначить код ошибки и сообщение нескольким типам объектов, для которых все SHARE специфичные вложенные свойства, поэтому мне не нужно дублировать мои методы ссылки, надеясь, что это поможет кому-то еще:
public T AssignErrorMessage<T>(T response, string errorDescription, int errorCode)
{
PropertyInfo ErrorMessagesProperty = response.GetType().GetProperty("ErrorMessage");
if (ErrorMessagesProperty.GetValue(response, null) == null)
ErrorMessagesProperty.SetValue(response, new ErrorMessage());
PropertyInfo ErrorCodeProperty = ErrorMessagesProperty.GetType().GetProperty("code");
ErrorCodeProperty.SetValue(response, errorCode);
PropertyInfo ErrorMessageDescription = ErrorMessagesProperty.GetType().GetProperty("description");
ErrorMessageDescription.SetValue(response, errorDescription);
return response;
}
public class ErrorMessage
{
public int code { get; set; }
public string description { get; set; }
}