public class ClassA
{
public static MethodA() {}
}
private static string NameOf<TRoot>(Expression<Func<TRoot, object>> pathExpression)
{
var members = new Stack<string>();
for (
var memberExpression = pathExpression.Body as MemberExpression;
memberExpression != null;
memberExpression = memberExpression.Expression as MemberExpression
)
{
members.Push(memberExpression.Member.Name);
}
members.Push(typeof(TRoot).Name);
return string.Join(".", members);
}
DeclaringType
у Member
.Console.WriteLine(NameOf(( ) => SomeStatic.Value ));
static string NameOf<T>(Expression<Func<T>> path)
{
var members = new Queue<string>();
Expression? exp = path.Body;
while (exp is not null)
{
if (exp is MemberExpression memberExpression)
{
var prevName = memberExpression.Member.DeclaringType?.Name;
if (prevName is not null)
{
if (members.TryPeek(out var last) || last != prevName)
{
members.Enqueue(prevName);
}
}
members.Enqueue(memberExpression.Member.Name);
exp = memberExpression.Expression;
}
else
{
break;
}
}
return string.Join('.', members);
}
class SomeStatic
{
public static int Value { get; set; }
}