using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Linq.Expressions; using System.Reflection; namespace Sheng.Winform.Controls.Kernal { /// /// /// public interface IPropertyAccessor { /// /// /// /// /// object GetValue(object instance); /// /// /// /// /// void SetValue(object instance, object value); } /// /// /// public class PropertyAccessor : IPropertyAccessor { private Func m_getter; private MethodInvoker m_setMethodInvoker; /// /// /// public PropertyInfo PropertyInfo { get; private set; } /// /// /// /// public PropertyAccessor(PropertyInfo propertyInfo) { this.PropertyInfo = propertyInfo; this.InitializeGet(propertyInfo); this.InitializeSet(propertyInfo); } private void InitializeGet(PropertyInfo propertyInfo) { if (!propertyInfo.CanRead) return; // Target: (object)(((TInstance)instance).Property) // preparing parameter, object type var instance = Expression.Parameter(typeof(object), "instance"); // non-instance for static method, or ((TInstance)instance) var instanceCast = propertyInfo.GetGetMethod(true).IsStatic ? null : Expression.Convert(instance, propertyInfo.ReflectedType); // ((TInstance)instance).Property var propertyAccess = Expression.Property(instanceCast, propertyInfo); // (object)(((TInstance)instance).Property) var castPropertyValue = Expression.Convert(propertyAccess, typeof(object)); // Lambda expression var lambda = Expression.Lambda>(castPropertyValue, instance); this.m_getter = lambda.Compile(); } private void InitializeSet(PropertyInfo propertyInfo) { if (!propertyInfo.CanWrite) return; this.m_setMethodInvoker = new MethodInvoker(propertyInfo.GetSetMethod(true)); } /// /// /// /// /// public object GetValue(object o) { if (this.m_getter == null) { throw new NotSupportedException("Get method is not defined for this property."); } return this.m_getter(o); } /// /// /// /// /// public void SetValue(object o, object value) { if (this.m_setMethodInvoker == null) { throw new NotSupportedException("Set method is not defined for this property."); } this.m_setMethodInvoker.Invoke(o, new object[] { value }); } #region IPropertyAccessor Members object IPropertyAccessor.GetValue(object instance) { return this.GetValue(instance); } void IPropertyAccessor.SetValue(object instance, object value) { this.SetValue(instance, value); } #endregion } }