Paradox Game Engine  v1.0.0 beta06
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros Pages
NameOf.cs
Go to the documentation of this file.
1 // Copyright (c) 2014 Silicon Studio Corp. (http://siliconstudio.co.jp)
2 // This file is distributed under GPL v3. See LICENSE.md for details.
3 using System;
4 using System.Linq.Expressions;
5 
6 namespace SiliconStudio.Presentation.Core
7 {
8  /// <summary>
9  /// A class that provides runtime evaluation of member names using <see cref="Expression"/>. Note that this class should not be used when performances matter.
10  /// </summary>
11  /// <typeparam name="TType">The type that contains the member to retrieve.</typeparam>
12  public static class NameOf<TType>
13  {
14  /// <summary>
15  /// Gets the name of the given member.
16  /// </summary>
17  /// <param name="expression">An expression accessing the member. Must be a <see cref="MemberExpression"/>/</param>
18  /// <returns>The name of the given member.</returns>
19  public static string Member<TMember>(Expression<Func<TType, TMember>> expression)
20  {
21  var body = expression.Body as MemberExpression;
22  if (body == null)
23  throw new ArgumentException("The given expression must be a MemberExpression.");
24  return body.Member.Name;
25  }
26  }
27 }