ln.http/rest/FieldDescriptor.cs

48 lines
1.3 KiB
C#

using System;
using System.Collections.Generic;
using System.Reflection;
using System.Text;
namespace ln.http.rest
{
public class FieldDescriptor
{
Action<object, object> setter;
Func<object, object> getter;
public Type OwningType { get; }
public string FieldName { get; }
public Type FieldType { get; }
public bool CanWrite { get; }
public FieldDescriptor(FieldInfo fieldInfo)
{
OwningType = fieldInfo.DeclaringType;
FieldName = fieldInfo.Name;
FieldType = fieldInfo.FieldType;
CanWrite = !fieldInfo.IsInitOnly;
getter = fieldInfo.GetValue;
setter = fieldInfo.SetValue;
}
public FieldDescriptor(PropertyInfo propertyInfo)
{
OwningType = propertyInfo.DeclaringType;
FieldName = propertyInfo.Name;
FieldType = propertyInfo.PropertyType;
CanWrite = propertyInfo.CanWrite;
getter = propertyInfo.GetValue;
setter = propertyInfo.SetValue;
}
public void SetFieldValue(object objectInstance, object fieldValue) => setter(objectInstance, fieldValue);
public object GetFieldValue(object objectInstance) => getter(objectInstance);
}
}