Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

add FastPropertyT and FastProperty variants for FastPropertyTP

  • Loading branch information...
commit 987c7075f78856cde275dd1ed78cafc57d6f7d77 1 parent 260a3db
@handcraftsman handcraftsman authored
View
71 src/FastReflection/FastProperty.cs
@@ -0,0 +1,71 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Reflection;
+using System.Linq.Expressions;
+
+namespace FastReflection
+{
+ public class FastProperty
+ {
+ public PropertyInfo Property { get; set; }
+
+ public Func<object, object> GetDelegate;
+ public Action<object, object> 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(object), "instance");
+ var value = Expression.Parameter(typeof(object), "value");
+
+ // value as T is slightly faster than (T)value, so if it's not a value type, use that
+ UnaryExpression instanceCast = (!this.Property.DeclaringType.IsValueType) ? Expression.TypeAs(instance, this.Property.DeclaringType) : Expression.Convert(instance, this.Property.DeclaringType);
+ UnaryExpression valueCast = (!this.Property.PropertyType.IsValueType) ? Expression.TypeAs(value, this.Property.PropertyType) : Expression.Convert(value, this.Property.PropertyType);
+ this.SetDelegate =
+ Expression.Lambda<Action<object, object>>(Expression.Call(instanceCast, setMethod, valueCast),
+ new ParameterExpression[] { instance, value }).Compile();
+ }
+ }
+
+ private void InitializeGet()
+ {
+ var getMethod = this.Property.GetGetMethod();
+ if (getMethod != null)
+ {
+ var instance = Expression.Parameter(typeof(object), "instance");
+ UnaryExpression instanceCast = (!this.Property.DeclaringType.IsValueType)
+ ? Expression.TypeAs(instance, this.Property.DeclaringType)
+ : Expression.Convert(instance, this.Property.DeclaringType);
+ this.GetDelegate =
+ Expression.Lambda<Func<object, object>>(
+ Expression.TypeAs(Expression.Call(instanceCast, getMethod), typeof(object)), instance).Compile();
+ }
+ }
+
+ public bool CanWrite
+ {
+ get { return SetDelegate != null; }
+ }
+
+ public object Get(object instance)
+ {
+ return this.GetDelegate(instance);
+ }
+
+ public void Set(object instance, object value)
+ {
+ this.SetDelegate(instance, value);
+ }
+ }
+}
View
69 src/FastReflection/FastPropertyT.cs
@@ -0,0 +1,69 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Linq.Expressions;
+using System.Reflection;
+
+namespace FastReflection
+{
+ public class FastProperty<T>
+ {
+ public PropertyInfo Property { get; set; }
+
+ public Func<T, object> GetDelegate;
+ public Action<T, object> 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(object), "value");
+ UnaryExpression valueCast = (!this.Property.PropertyType.IsValueType)
+ ? Expression.TypeAs(value, this.Property.PropertyType)
+ : Expression.Convert(value, this.Property.PropertyType);
+ this.SetDelegate =
+ Expression.Lambda<Action<T, object>>(Expression.Call(instance, setMethod, valueCast),
+ new ParameterExpression[] { instance, value }).Compile();
+ }
+ }
+
+ private void InitializeGet()
+ {
+ var getMethod = this.Property.GetGetMethod();
+ if (getMethod != null)
+ {
+ var instance = Expression.Parameter(typeof(T), "instance");
+ this.GetDelegate =
+ Expression.Lambda<Func<T, object>>(Expression.TypeAs(Expression.Call(instance, getMethod), typeof(object)),
+ instance).Compile();
+ }
+ }
+
+ public object Get(T instance)
+ {
+ return this.GetDelegate(instance);
+ }
+
+ public bool CanWrite
+ {
+ get { return SetDelegate != null; }
+ }
+
+ public void Set(T instance, object value)
+ {
+ this.SetDelegate(instance, value);
+ }
+
+
+ }
+}
View
4 src/FastReflection/FastReflection.csproj
@@ -3,7 +3,7 @@
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
- <ProductVersion>9.0.30428</ProductVersion>
+ <ProductVersion>9.0.30729</ProductVersion>
<SchemaVersion>2.0</SchemaVersion>
<ProjectGuid>{31AB1957-5CCA-4BBA-805A-718CB347F349}</ProjectGuid>
<OutputType>Library</OutputType>
@@ -45,6 +45,8 @@
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
+ <Compile Include="FastProperty.cs" />
+ <Compile Include="FastPropertyT.cs" />
<Compile Include="FastPropertyTP.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
Please sign in to comment.
Something went wrong with that request. Please try again.