ln.http.resources/collections/EntityMapper.cs

59 lines
1.6 KiB
C#

using System;
using System.Collections.Generic;
using System.Reflection;
using ln.json.mapping;
using ln.json;
namespace ln.http.resources.collections
{
public class EntityMapper<TENTITY>
{
public JSONMapper JSONMapper { 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(JSONMapper jsonMapper)
:this()
{
}
public JSONObject MapEntity(TENTITY entity)
{
JSONObject jObject = new JSONObject();
foreach (FieldInfo fieldInfo in publicFields.Values)
{
JSONValue jValue = JSONMapper.ToJson(fieldInfo.GetValue(entity));
jObject.Add(fieldInfo.Name, jValue);
}
foreach (PropertyInfo propertyInfo in publicProperties.Values)
{
JSONValue jValue = JSONMapper.ToJson(propertyInfo.GetValue(entity));
jObject.Add(propertyInfo.Name, jValue);
}
return jObject;
}
public bool PopulateEntity(JSONObject json)
{
return false;
}
}
}