Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

handle properties that don't have getter/setter

  • Loading branch information...
commit 260a3dbd3dda6a4afdb93a1f197d4f7452dc21e3 1 parent a8c4899
@handcraftsman handcraftsman authored
Showing with 56 additions and 44 deletions.
  1. +56 −44 src/FastReflection/FastPropertyTP.cs
View
100 src/FastReflection/FastPropertyTP.cs
@@ -2,52 +2,64 @@
using System.Collections.Generic;
using System.Linq;
using System.Text;
-using System.Linq.Expressions;
using System.Reflection;
+using System.Linq.Expressions;
namespace FastReflection
{
- public class FastProperty<T, P>
- {
- public PropertyInfo Property { get; set; }
-
- public Func<T, P> GetDelegate;
- public Action<T, P> SetDelegate;
-
- public FastProperty(PropertyInfo property)
- {
- this.Property = property;
- InitializeGet();
- InitializeSet();
-
-
- }
-
- private void InitializeSet()
- {
- var instance = Expression.Parameter(typeof(T), "instance");
- var value = Expression.Parameter(typeof(P), "value");
- this.SetDelegate = Expression.Lambda<Action<T, P>>(Expression.Call(instance, this.Property.GetSetMethod(), value), new ParameterExpression[] { instance, value }).Compile();
-
- // roughly looks like Action<T,P> a = new Action<T,P>((instance,value) => instance.set_Property(value));
- }
-
- private void InitializeGet()
- {
- var instance = Expression.Parameter(typeof(T), "instance");
- this.GetDelegate = Expression.Lambda<Func<T, P>>(Expression.Call(instance, this.Property.GetGetMethod()), instance).Compile();
-
- // roughly looks like Func<T,P> getter = instance => return instance.get_Property();
- }
-
- public P Get(T instance)
- {
- return this.GetDelegate(instance);
- }
-
- public void Set(T instance, P value)
- {
- this.SetDelegate(instance, value);
- }
- }
+ public class FastProperty<T, P>
+ {
+ public PropertyInfo Property { get; set; }
+
+ public Func<T, P> GetDelegate;
+ public Action<T, P> SetDelegate;
+
+ public FastProperty(PropertyInfo property)
+ {
+ this.Property = property;
+ InitializeGet();
+ InitializeSet();
+ }
+
+ private void InitializeSet()
+ {
+ var setMethod = this.Property.GetSetMethod();
+ if (setMethod != null)
+ {
+ var instance = Expression.Parameter(typeof(T), "instance");
+ var value = Expression.Parameter(typeof(P), "value");
+ this.SetDelegate =
+ Expression.Lambda<Action<T, P>>(Expression.Call(instance, setMethod, value),
+ new ParameterExpression[] { instance, value }).Compile();
+ }
+ // roughly looks like Action<T,P> a = new Action<T,P>((instance,value) => instance.set_Property(value));
+ }
+
+ private void InitializeGet()
+ {
+ var getMethod = this.Property.GetGetMethod();
+ if (getMethod != null)
+ {
+ var instance = Expression.Parameter(typeof(T), "instance");
+ this.GetDelegate = Expression.Lambda<Func<T, P>>(Expression.Call(instance, getMethod), instance).Compile();
+ }
+ // roughly looks like Func<T,P> getter = instance => return instance.get_Property();
+ }
+
+ public P Get(T instance)
+ {
+ return this.GetDelegate(instance);
+ }
+
+ public bool CanWrite
+ {
+ get { return SetDelegate != null; }
+ }
+
+ public void Set(T instance, P value)
+ {
+ this.SetDelegate(instance, value);
+ }
+ }
+
}
Please sign in to comment.
Something went wrong with that request. Please try again.