using System; using System.Reflection; using ZeroLevel.Services.Reflection; namespace ZeroLevel.Services.ObjectMapping { public class MapMemberInfo : IMemberInfo { #region Properties public bool IsField { get; set; } public string Name { get; set; } public Type ClrType { get; set; } public Action Setter { get; set; } public Func Getter { get; set; } #endregion Properties #region Ctor public MapMemberInfo(Action setter, Func getter) { Getter = getter; Setter = setter; } #endregion Ctor #region Factory public static IMemberInfo FromMember(MemberInfo memberInfo) { switch (memberInfo.MemberType) { case MemberTypes.Field: return FromField(memberInfo as FieldInfo); case MemberTypes.Property: return FromProperty(memberInfo as PropertyInfo); } return null; } #endregion Factory #region Helpers private static bool IsNullable(Type type) { if (!type.IsValueType) return true; // ref-type if (Nullable.GetUnderlyingType(type) != null) return true; // Nullable return false; // value-type } private static MapMemberInfo FromField(FieldInfo fieldInfo) { var field = new MapMemberInfo(fieldInfo.BuildSetter(), fieldInfo.BuildGetter()) { Name = fieldInfo.Name }; field.IsField = true; var type = fieldInfo.FieldType; field.ClrType = type; return field; } private static IMemberInfo FromProperty(PropertyInfo propertyInfo) { var field = new MapMemberInfo(propertyInfo.BuildSetter(), propertyInfo.BuildGetter()) { Name = propertyInfo.Name }; field.IsField = false; var type = propertyInfo.PropertyType; field.ClrType = type; return field; } private static object GetDefault(Type type) { if (type.IsValueType) { return Activator.CreateInstance(type); } return null; } #endregion Helpers } }