Source

KeeCloud / KeeCloud / Utilities / ReflectionHelpers.cs

Devin Martin 84db277 
























using System;
using System.Linq.Expressions;

namespace KeeCloud.Utilities
{
    class ReflectionHelpers
    {
        /// <summary>
        /// Takes a lambda expression that accesses a member and returns the name of the member
        /// </summary>
        /// <typeparam name="TObject">The type that contains the member</typeparam>
        /// <typeparam name="TMember">The type of the member</typeparam>
        /// <param name="expression">Lambda expression that does nothing more than access (return) the value of the member</param>
        /// <returns>String name of the member that was acessed by the expression</returns>
        public static string MemberNameFromExpression<TObject, TMember>(Expression<Func<TObject, TMember>> expression)
        {
            if (expression.Body is MemberExpression)
            {
                MemberExpression body = (MemberExpression)expression.Body;
                return body.Member.Name;
            }
            else
                throw new Exception("The lambda expression is invalid. Use format () => object.member and nothing more. Ensure that you are accessing a property or a field only");
        }
    }
}
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.