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));
}
MethodInfo getterInfo = property.GetGetMethod(false);
if(getterInfo != null) {
this._getter = (Func)Delegate.CreateDelegate(typeof(Func), getterInfo);
}
MethodInfo setterInfo = property.GetSetMethod(false);
if(setterInfo != null) {
this._setter = (Action)Delegate.CreateDelegate(typeof(Action), setterInfo);
}
}
///
[MethodImpl(MethodImplOptions.AggressiveInlining)]
Object IPropertyProxy.GetValue(Object instance) => this._getter(instance as TClass);
///
[MethodImpl(MethodImplOptions.AggressiveInlining)]
void IPropertyProxy.SetValue(Object instance, Object value) => this._setter(instance as TClass, (TProperty)value);
}
}