RaUI/Source/ryControls/Sheng.Winform.Controls/Kernal/FastReflection/PropertyAccessor.cs
鑫Intel c3d4ddf574 ### 2021-07-29更新
------
#### MyDbV4   V3.0.2107.2901
- *.[新增]新增支持计算文件MD5。
- *.[新增]部分DataProvider功能移植到DbExtension里,增加扩展性。
- *.[新增]UnixTimeToDateTime和JSTimeToDateTime新增支持long参数。
- *.[合并]合并RyWeb项目到MyDb里。

#### ryControlsV4    V3.0.2107.2901
  -  *.[改进]优化减少大量IDE警告和消息。
2021-07-29 17:09:32 +08:00

124 lines
3.6 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Linq.Expressions;
using System.Reflection;
namespace Sheng.Winform.Controls.Kernal
{
/// <summary>
///
/// </summary>
public interface IPropertyAccessor
{
/// <summary>
///
/// </summary>
/// <param name="instance"></param>
/// <returns></returns>
object GetValue(object instance);
/// <summary>
///
/// </summary>
/// <param name="instance"></param>
/// <param name="value"></param>
void SetValue(object instance, object value);
}
/// <summary>
///
/// </summary>
public class PropertyAccessor : IPropertyAccessor
{
private Func<object, object> m_getter;
private MethodInvoker m_setMethodInvoker;
/// <summary>
///
/// </summary>
public PropertyInfo PropertyInfo { get; private set; }
/// <summary>
///
/// </summary>
/// <param name="propertyInfo"></param>
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<Func<object, object>>(castPropertyValue, instance);
this.m_getter = lambda.Compile();
}
private void InitializeSet(PropertyInfo propertyInfo)
{
if (!propertyInfo.CanWrite) return;
this.m_setMethodInvoker = new MethodInvoker(propertyInfo.GetSetMethod(true));
}
/// <summary>
///
/// </summary>
/// <param name="o"></param>
/// <returns></returns>
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);
}
/// <summary>
///
/// </summary>
/// <param name="o"></param>
/// <param name="value"></param>
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
}
}