You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Zero/ZeroLevel/Services/ObjectMapping/MapFieldInfo.cs

91 lines
2.5 KiB

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

Powered by TurnKey Linux.