comparison Chronosv2/source/Extensions/PropertyExtensions.cs @ 10:443821e55f06

Initial cleaned up add from Codeplex files
author stevenh7776 stevenhollidge@hotmail.com
date Tue, 21 Feb 2012 17:25:44 +0700
parents
children 09d18d6e5f40
comparison
equal deleted inserted replaced
9:904a9faadf8b 10:443821e55f06
1 /*
2 The MIT License
3
4 Copyright (c) 2009-2010. Carlos Guzmán Álvarez. http://chronoswpf.codeplex.com/
5
6 Permission is hereby granted, free of charge, to any person obtaining a copy
7 of this software and associated documentation files (the "Software"), to deal
8 in the Software without restriction, including without limitation the rights
9 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 copies of the Software, and to permit persons to whom the Software is
11 furnished to do so, subject to the following conditions:
12
13 The above copyright notice and this permission notice shall be included in
14 all copies or substantial portions of the Software.
15
16 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 THE SOFTWARE.
23 */
24
25 using System;
26 using System.ComponentModel;
27 using System.Linq.Expressions;
28
29 namespace Chronos.Presentation.ViewModel
30 {
31 /// <summary>
32 /// Property Extension Methods
33 /// </summary>
34 /// <remarks>
35 /// http://reyntjes.blogspot.com/2009/04/master-detail-viewmodel_24.html
36 /// http://blogs.ugidotnet.org/bmatte/archive/2008/11/28/pattern-model-view-viewmodel-inotifypropertychanged-static-reflection-e-extension-methods.aspx
37 /// </remarks>
38 public static class PropertyExtensions
39 {
40 #region · Extension Methods ·
41
42 /// <summary>
43 /// Creates a <see cref="PropertyChangedEventArgs" /> instance for a given property.
44 /// </summary>
45 /// <typeparam name="T"></typeparam>
46 /// <param name="property">The property.</param>
47 /// <returns></returns>
48 public static PropertyChangedEventArgs CreateChangeEventArgs<T>(this Expression<Func<T>> property)
49 {
50 var expression = property.Body as MemberExpression;
51 var member = expression.Member;
52
53 return new PropertyChangedEventArgs(member.Name);
54 }
55
56 /// <summary>
57 /// Returns property name from expression
58 /// </summary>
59 /// <typeparam name="T"></typeparam>
60 /// <param name="property"></param>
61 /// <returns></returns>
62 public static string GetPropertyName<T>(this Expression<Func<T>> property)
63 {
64 var expression = property.Body as MemberExpression;
65
66 return expression.Member.Name;
67 }
68
69 /// <summary>
70 /// Return property name from expression.
71 /// </summary>
72 /// <example>
73 /// <![CDATA[
74 /// Expression<Func<Item, object>> expression = i => i.Name;
75 /// var propertyName = expression.GetPropertyName(); // propertyName = "Name"
76 /// ]]>
77 /// </example>
78 /// <typeparam name="T"></typeparam>
79 /// <typeparam name="TValue"></typeparam>
80 /// <param name="expression"></param>
81 /// <returns></returns>
82 public static string GetPropertyName<T, TValue>(this Expression<Func<T, TValue>> expression)
83 {
84 var lambda = expression as LambdaExpression;
85
86 MemberExpression memberExpression;
87
88 if (lambda.Body is UnaryExpression)
89 {
90 var unaryExpression = lambda.Body as UnaryExpression;
91 memberExpression = unaryExpression.Operand as MemberExpression;
92 }
93 else
94 {
95 memberExpression = lambda.Body as MemberExpression;
96 }
97
98 if (memberExpression != null)
99 {
100 return memberExpression.Member.Name;
101 }
102
103 return null;
104 }
105
106 #endregion
107 }
108 }