ProjectDDD/Assets/_DDD/_Scripts/GameUi/Utils/Binding/BindingPath.cs

56 lines
2.0 KiB
C#
Raw Normal View History

2025-08-24 20:12:05 +00:00
using System;
using System.Collections.Generic;
using System.Linq.Expressions;
namespace DDD
{
public static class BindingPath
{
public static string For<TViewModel, TProp>(Expression<Func<TViewModel, TProp>> expression)
{
if (expression == null) throw new ArgumentNullException(nameof(expression));
Expression expr = expression.Body;
// 값형식 → object 캐스팅 제거
if (expr is UnaryExpression unary && unary.NodeType == ExpressionType.Convert)
{
expr = unary.Operand;
}
var members = new Stack<string>();
while (expr is MemberExpression me)
{
members.Push(me.Member.Name);
expr = me.Expression;
if (expr is ParameterExpression) break; // 루트 도달
if (expr is UnaryExpression innerUnary && innerUnary.NodeType == ExpressionType.Convert)
expr = innerUnary.Operand;
}
return string.Join(".", members);
}
// 편의용: object 리턴 시그니처도 지원
public static string For<TViewModel>(Expression<Func<TViewModel, object>> expression)
{
if (expression == null) throw new ArgumentNullException(nameof(expression));
Expression expr = expression.Body;
if (expr is UnaryExpression unary && unary.NodeType == ExpressionType.Convert)
expr = unary.Operand;
var members = new Stack<string>();
while (expr is MemberExpression me)
{
members.Push(me.Member.Name);
expr = me.Expression;
if (expr is ParameterExpression) break;
if (expr is UnaryExpression innerUnary && innerUnary.NodeType == ExpressionType.Convert)
expr = innerUnary.Operand;
}
return string.Join(".", members);
}
}
}