ExpressionExtensions.cs 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. // Copyright © 2023 Steffen Cole Blake
  2. //
  3. // Permission is hereby granted, free of charge, to any person obtaining
  4. // a copy of this software and associated documentation files (the “Software”),
  5. // to deal in the Software without restriction, including without limitation
  6. // the rights to use, copy, modify, merge, publish, distribute, sublicense,
  7. // and/or sell copies of the Software, and to permit persons to whom the Software
  8. // is furnished to do so, subject to the following conditions:
  9. //
  10. // The above copyright notice and this permission notice shall be included in all
  11. // copies or substantial portions of the Software.
  12. //
  13. // THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
  14. // INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  15. // PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  16. // HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  17. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
  18. // SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  19. //
  20. using System.Linq.Expressions;
  21. using System;
  22. public static class ExpressionExtensions
  23. {
  24. public static string GetFullMemberName(this LambdaExpression memberSelector)
  25. {
  26. var currentExpression = memberSelector.Body;
  27. if (currentExpression is not MemberExpression memberExpression)
  28. {
  29. throw new Exception("Member Expressions only");
  30. }
  31. var name = memberExpression.Member.Name;
  32. while (memberExpression?.Expression is not null and MemberExpression next)
  33. {
  34. memberExpression = next;
  35. name = memberExpression.Member.Name + "." + name;
  36. }
  37. return name;
  38. }
  39. }