Permalink
Browse files

Adding ExpressionUtility.

  • Loading branch information...
1 parent 4e3f421 commit 3ecd2ac9ad3b575bcdbaf430701cdebbd3f8be97 @jacobcarpenter jacobcarpenter committed Feb 5, 2010
@@ -0,0 +1,45 @@
+
+using System;
+using System.Linq.Expressions;
+
+namespace Logos.Utility
+{
+ /// <summary>
+ /// Provides helper methods for expression tree operations.
+ /// </summary>
+ public static class ExpressionUtility
+ {
+ /// <summary>
+ /// Gets the name of the property returned by the specified expression.
+ /// </summary>
+ /// <typeparam name="TProperty">The type of the property.</typeparam>
+ /// <param name="expr">The member access expression.</param>
+ /// <returns>The name of the property.</returns>
+ public static string GetPropertyName<TProperty>(Expression<Func<TProperty>> expr)
+ {
+ var exprMember = expr.Body as MemberExpression;
+ if (exprMember != null)
+ return exprMember.Member.Name;
+
+ throw new InvalidOperationException("ExpressionUtility.GetPropertyName failed on " + expr);
+ }
+
+ /// <summary>
+ /// Gets the name of the property returned by the specified expression.
+ /// </summary>
+ /// <typeparam name="T">The type of the property owner.</typeparam>
+ /// <typeparam name="TProperty">The type of the property.</typeparam>
+ /// <param name="expr">The member access expression.</param>
+ /// <returns>The name of the property.</returns>
+ /// <remarks>Type-annotate the argument to the lambda to get type inference for this method. Example:
+ /// <code>(FileInfo info) => info.FullName</code> rather than <code>info => info.FullName</code>.</remarks>
+ public static string GetPropertyName<T, TProperty>(Expression<Func<T, TProperty>> expr)
+ {
+ var exprMember = expr.Body as MemberExpression;
+ if (exprMember != null)
+ return exprMember.Member.Name;
+
+ throw new InvalidOperationException("ExpressionUtility.GetPropertyName failed on " + expr);
+ }
+ }
+}
@@ -52,6 +52,7 @@
<Compile Include="DisposableService.cs" />
<Compile Include="DisposableUtility.cs" />
<Compile Include="EnvironmentUtility.cs" />
+ <Compile Include="ExpressionUtility.cs" />
<Compile Include="HashCodeUtility.cs" />
<Compile Include="IO\WrappingStream.cs" />
<Compile Include="NativeMethods.cs" />
@@ -0,0 +1,50 @@
+
+using System;
+using NUnit.Framework;
+
+namespace Logos.Utility.Tests
+{
+ [TestFixture]
+ public class ExpressionUtilityTests
+ {
+ [Test]
+ public void GetPropertyName()
+ {
+ Assert.AreEqual("TestReadOnly", ExpressionUtility.GetPropertyName((Tester t) => t.TestReadOnly));
+ Assert.AreEqual("TestReadWrite", ExpressionUtility.GetPropertyName((Tester t) => t.TestReadWrite));
+
+ Tester tester = new Tester();
+ Assert.AreEqual("TestReadOnly", ExpressionUtility.GetPropertyName(() => tester.TestReadOnly));
+ Assert.AreEqual("TestReadWrite", ExpressionUtility.GetPropertyName(() => tester.TestReadWrite));
+
+ Assert.AreEqual("TestStaticReadOnly", ExpressionUtility.GetPropertyName(() => Tester.TestStaticReadOnly));
+ Assert.AreEqual("TestStaticReadWrite", ExpressionUtility.GetPropertyName(() => Tester.TestStaticReadWrite));
+ }
+
+ private class Tester
+ {
+ public int TestReadOnly
+ {
+ get { throw new InvalidOperationException(); }
+ }
+
+ public int TestReadWrite
+ {
+ get { throw new InvalidOperationException(); }
+ set { throw new InvalidOperationException(); }
+ }
+
+ public static int TestStaticReadOnly
+ {
+ get { throw new InvalidOperationException(); }
+ }
+
+ public static int TestStaticReadWrite
+ {
+ get { throw new InvalidOperationException(); }
+ set { throw new InvalidOperationException(); }
+ }
+ }
+
+ }
+}
@@ -56,6 +56,7 @@
<Compile Include="EquatableClassTests.cs" />
<Compile Include="EquatableStruct.cs" />
<Compile Include="EquatableStructTests.cs" />
+ <Compile Include="ExpressionUtilityTests.cs" />
<Compile Include="ObjectUtilityTests.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Salsa20Tests.cs" />

0 comments on commit 3ecd2ac

Please sign in to comment.