1:
2: /// <summary>
3: /// Gets a lambda expression that calls a specific property's setter function
4: /// </summary>
5: /// <typeparam name="ClassType">Class type</typeparam>
6: /// <typeparam name="DataType">Data type expecting</typeparam>
7: /// <param name="PropertyName">Property name</param>
8: /// <returns>A lambda expression that calls a specific property's setter function</returns>
9: public static Expression<Action<ClassType, DataType>> GetPropertySetter<ClassType,DataType>(string PropertyName)
10: {
11: if (string.IsNullOrEmpty(PropertyName))
12: throw new ArgumentNullException("PropertyName");
13: string[] SplitName = PropertyName.Split(new string[] { "." }, StringSplitOptions.RemoveEmptyEntries);
14: PropertyInfo Property = Utilities.Reflection.Reflection.GetProperty<ClassType>(SplitName[0]);
15: ParameterExpression ObjectInstance = Expression.Parameter(Property.DeclaringType, "x");
16: ParameterExpression PropertySet = Expression.Parameter(typeof(DataType), "y");
17: MethodCallExpression SetterCall = null;
18: MemberExpression PropertyGet = null;
19: if (SplitName.Length > 1)
20: {
21: PropertyGet = Expression.Property(ObjectInstance, Property);
22: for (int x = 1; x < SplitName.Length - 1; ++x)
23: {
24: Property = Utilities.Reflection.Reflection.GetProperty(Property.PropertyType, SplitName[x]);
25: PropertyGet = Expression.Property(PropertyGet, Property);
26: }
27: Property = Utilities.Reflection.Reflection.GetProperty(Property.PropertyType, SplitName[SplitName.Length - 1]);
28: }
29: if (Property.PropertyType != typeof(DataType))
30: {
31: UnaryExpression Convert = Expression.Convert(PropertySet, Property.PropertyType);
32: if (PropertyGet == null)
33: SetterCall = Expression.Call(ObjectInstance, Property.GetSetMethod(), Convert);
34: else
35: SetterCall = Expression.Call(PropertyGet, Property.GetSetMethod(), Convert);
36: return Expression.Lambda<Action<ClassType, DataType>>(SetterCall, ObjectInstance, PropertySet);
37: }
38: if (PropertyGet == null)
39: SetterCall = Expression.Call(ObjectInstance, Property.GetSetMethod(), PropertySet);
40: else
41: SetterCall = Expression.Call(PropertyGet, Property.GetSetMethod(), PropertySet);
42: return Expression.Lambda<Action<ClassType, DataType>>(SetterCall, ObjectInstance, PropertySet);
43: }
44:
45: /// <summary>
46: /// Gets a lambda expression that calls a specific property's setter function
47: /// </summary>
48: /// <typeparam name="ClassType">Class type</typeparam>
49: /// <typeparam name="DataType">Data type expecting</typeparam>
50: /// <param name="PropertyName">Property name</param>
51: /// <returns>A lambda expression that calls a specific property's setter function</returns>
52: public static Expression<Action<ClassType, DataType>> GetPropertySetter<ClassType, DataType>(PropertyInfo Property)
53: {
54: if (!IsOfType(Property.PropertyType, typeof(DataType)))
55: throw new ArgumentException("Property is not of the type specified");
56: if (!IsOfType(Property.DeclaringType, typeof(ClassType)))
57: throw new ArgumentException("Property is not from the declaring class type specified");
58:
59: ParameterExpression ObjectInstance = Expression.Parameter(Property.DeclaringType, "x");
60: ParameterExpression PropertySet = Expression.Parameter(typeof(DataType), "y");
61: MethodCallExpression SetterCall = null;
62: if(Property.PropertyType!=typeof(DataType))
63: {
64:
65: UnaryExpression Convert=Expression.Convert(PropertySet,Property.PropertyType);
66: SetterCall = Expression.Call(ObjectInstance, Property.GetSetMethod(), Convert);
67: return Expression.Lambda<Action<ClassType, DataType>>(SetterCall, ObjectInstance, PropertySet);
68: }
69: SetterCall = Expression.Call(ObjectInstance, Property.GetSetMethod(), PropertySet);
70: return Expression.Lambda<Action<ClassType, DataType>>(SetterCall, ObjectInstance, PropertySet);
71: }
72:
73: /// <summary>
74: /// Gets a lambda expression that calls a specific property's setter function
75: /// </summary>
76: /// <typeparam name="ClassType">Class type</typeparam>
77: /// <param name="PropertyName">Property name</param>
78: /// <returns>A lambda expression that calls a specific property's setter function</returns>
79: public static Expression<Action<ClassType,object>> GetPropertySetter<ClassType>(string PropertyName)
80: {
81: return GetPropertySetter<ClassType, object>(PropertyName);
82: }
83:
84: /// <summary>
85: /// Gets a lambda expression that calls a specific property's setter function
86: /// </summary>
87: /// <typeparam name="ClassType">Class type</typeparam>
88: /// <param name="PropertyName">Property name</param>
89: /// <returns>A lambda expression that calls a specific property's setter function</returns>
90: public static Expression<Action<ClassType, object>> GetPropertySetter<ClassType>(PropertyInfo Property)
91: {
92: return GetPropertySetter<ClassType, object>(Property);
93: }