using System;
using System.Reflection;
using System.Runtime.CompilerServices;
namespace Swan.Reflection
{
///
/// Represents a generic class to store getters and setters.
///
/// The type of the class.
/// The type of the property.
///
public sealed class PropertyProxy : IPropertyProxy
where TClass : class
{
private readonly Func _getter;
private readonly Action _setter;
///
/// Initializes a new instance of the class.
///
/// The property.
public PropertyProxy(PropertyInfo property)
{
if (property == null)
throw new ArgumentNullException(nameof(property));
var getterInfo = property.GetGetMethod(false);
if (getterInfo != null)
_getter = (Func)Delegate.CreateDelegate(typeof(Func), getterInfo);
var setterInfo = property.GetSetMethod(false);
if (setterInfo != null)
_setter = (Action)Delegate.CreateDelegate(typeof(Action), setterInfo);
}
///
[MethodImpl(MethodImplOptions.AggressiveInlining)]
object IPropertyProxy.GetValue(object instance) =>
_getter(instance as TClass);
///
[MethodImpl(MethodImplOptions.AggressiveInlining)]
void IPropertyProxy.SetValue(object instance, object value) =>
_setter(instance as TClass, (TProperty)value);
}
}