// Copyright (C) Stichting Deltares 2018. All rights reserved. // // This file is part of the Dam Engine. // // The Dam Engine is free software: you can redistribute it and/or modify // it under the terms of the GNU Affero General Public License as published by // the Free Software Foundation, either version 3 of the License, or // (at your option) any later version. // // This program is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU Affero General Public License for more details. // // You should have received a copy of the GNU Affero General Public License // along with this program. If not, see . // // All names, logos, and references to "Deltares" are registered trademarks of // Stichting Deltares and remain full property of Stichting Deltares at all times. // All rights reserved. using System; using System.Linq.Expressions; using System.Text; namespace Deltares.DamEngine.Data.Standard { /// /// static class StaticReflection /// public static class StaticReflection { /// /// Gets the name of the member. /// /// /// The instance. /// The expression. /// public static string GetMemberName(this T instance, Expression> expression) { return GetMemberName(expression); } public static string GetMemberName(Expression> expression) { return GetMemberName(expression); } private static string GetMemberName(Expression> expression) { if (expression == null) { throw new ArgumentException("The expression cannot be null."); } return GetMemberName(expression.Body); } private static string GetMemberName(Expression expression) { if (expression == null) { throw new ArgumentException("The expression cannot be null."); } var memberExpression = expression as MemberExpression; if (memberExpression != null) { return GetMemberName(memberExpression); } var methodCallExpression = expression as MethodCallExpression; if (methodCallExpression != null) { // Reference type method return GetMemberName(methodCallExpression); } var unaryExpression = expression as UnaryExpression; if (unaryExpression != null) { // Property, field of method returning value type return GetMemberName(unaryExpression); } throw new ArgumentException("Invalid expression, type is \"" + expression.Type + "\""); } private static string GetFullMemberName(string startingMember, string separator, Expression expression) { var fullMemberNameBuilder = new StringBuilder(startingMember); var memberExpression = expression as MemberExpression; while (null != memberExpression) { if (fullMemberNameBuilder.Length > 0) { fullMemberNameBuilder.Insert(0, separator); } fullMemberNameBuilder.Insert(0, memberExpression.Member.Name); memberExpression = memberExpression.Expression as MemberExpression; } return fullMemberNameBuilder.ToString(); } private static string GetMemberName(MemberExpression memberExpression) { return GetFullMemberName(memberExpression.Member.Name, ".", memberExpression.Expression); } private static string GetMemberName(MethodCallExpression methodCallExpression) { return GetFullMemberName(methodCallExpression.Method.Name, ".", methodCallExpression.Object); } private static string GetMemberName(UnaryExpression unaryExpression) { var methodExpression = unaryExpression.Operand as MethodCallExpression; if (methodExpression != null) { return methodExpression.Method.Name; } return GetFullMemberName("", ".", unaryExpression.Operand); } } }