69 lines
2.0 KiB
C#
69 lines
2.0 KiB
C#
using System;
|
|
using Newtonsoft.Json.Linq;
|
|
using System.Collections.Generic;
|
|
using System.Reflection;
|
|
using ln.types.json;
|
|
namespace ln.http.resources.collections
|
|
{
|
|
public class EntityMapper<TENTITY>
|
|
{
|
|
public JSONConvert JSONConvert { get; set; }
|
|
|
|
public Dictionary<string, FieldInfo> publicFields = new Dictionary<string, FieldInfo>();
|
|
public Dictionary<string, PropertyInfo> publicProperties = new Dictionary<string, PropertyInfo>();
|
|
|
|
public EntityMapper()
|
|
{
|
|
foreach (PropertyInfo propertyInfo in typeof(TENTITY).GetProperties())
|
|
{
|
|
publicProperties.Add(propertyInfo.Name, propertyInfo);
|
|
}
|
|
foreach (FieldInfo fieldInfo in typeof(TENTITY).GetFields())
|
|
{
|
|
publicFields.Add(fieldInfo.Name, fieldInfo);
|
|
}
|
|
}
|
|
public EntityMapper(JSONConvert jsonConvert)
|
|
:this()
|
|
{
|
|
JSONConvert = jsonConvert;
|
|
}
|
|
|
|
public JObject MapEntity(TENTITY entity)
|
|
{
|
|
JObject jObject = new JObject();
|
|
|
|
foreach (FieldInfo fieldInfo in publicFields.Values)
|
|
{
|
|
JToken jToken = null;
|
|
if (JSONConvert.Value2JSON(
|
|
fieldInfo.GetValue(entity),
|
|
ref jToken
|
|
))
|
|
{
|
|
jObject.Add(fieldInfo.Name, jToken);
|
|
}
|
|
}
|
|
foreach (PropertyInfo propertyInfo in publicProperties.Values)
|
|
{
|
|
JToken jToken = null;
|
|
if (JSONConvert.Value2JSON(
|
|
propertyInfo.GetValue(entity),
|
|
ref jToken
|
|
))
|
|
{
|
|
jObject.Add(propertyInfo.Name, jToken);
|
|
}
|
|
}
|
|
|
|
return jObject;
|
|
}
|
|
public bool PopulateEntity(JObject json)
|
|
{
|
|
return false;
|
|
}
|
|
|
|
|
|
}
|
|
}
|