using System; using System.Collections.Generic; using System.Reflection; using System.Text; namespace ln.http.rest { public class FieldDescriptor { Action setter; Func 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); } }