QueryResultSlimDtoTransformer
using Newtonsoft.Json;
using Relativity.Transfer.Dto;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
namespace Relativity.Transfer
{
internal static class QueryResultSlimDtoTransformer
{
public static T Transform<T>(RelativityObjectSlimDto source, List<FieldDto> fields)
{
if (source == null || !source.Values.Any())
throw new ArgumentException("Source list cannot be empty");
if (fields == null || !fields.Any())
throw new ArgumentException("Fields list cannot be empty");
if (source.Values.Count == fields.Count) {
Type type = Assembly.Load("Newtonsoft.Json, Version=6.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed").GetType("Newtonsoft.Json.JsonPropertyAttribute");
IEnumerable<Attribute> source2 = from property in typeof(T).GetProperties()
select property.GetCustomAttribute(type, false);
IEnumerable<string> enumerable = Enumerable.Empty<string>();
if (source2.Any())
enumerable = from x in source2
select ((JsonPropertyAttribute)x).PropertyName;
IEnumerable<object> second = from name in enumerable
select fields.FindIndex((Predicate<FieldDto>)((FieldDto dto) => dto.Name.Equals(name))) into index
where index != -1
select index into idx
select source.Values[idx];
string value2 = JsonConvert.SerializeObject(enumerable.Zip(second, (string name, object value) => new KeyValuePair<string, object>(name, value)).ToDictionary((KeyValuePair<string, object> pair) => pair.Key, (KeyValuePair<string, object> pair) => pair.Value));
try {
return JsonConvert.DeserializeObject<T>(value2);
} catch (Exception innerException) {
throw new InvalidOperationException("Could not transform source into object of type: " + typeof(T).Name, innerException);
}
}
throw new ArgumentException("Number of fields has to be equal to number of values");
}
}
}