Path: redb-route/redb.Route/Expressions/ExpressionResolver.Members.cs
Snapshot: redb-route, development snapshot 2026-12-06. This is the development state of the public mirrors; it may differ from the published packages.
License: Apache-2.0. Repository: https://github.com/redbase-app/redb-route
Page: https://redbase.app/src/dev-2026-12-06/redb-route/redb.Route/Expressions/ExpressionResolver.Members.cs
----
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
namespace redb.Route.Expressions;
public static partial class ExpressionResolver
{
private const BindingFlags MemberFlags = BindingFlags.Public | BindingFlags.Instance;
///
/// The public instance property a path step names. The exact-case name wins; the case-insensitive match is used
/// only when it is the one name that fits. body.Id on a type with both Id and id (a redb
/// object) reads Id; body.ID there is ambiguous and fails naming both.
///
private static PropertyInfo? FindProperty(Type type, string name)
=> Pick(type, name, type.GetProperties(MemberFlags).Where(p => p.GetIndexParameters().Length == 0), "property");
/// The public instance field a path step names, by the rule of .
private static FieldInfo? FindField(Type type, string name)
=> Pick(type, name, type.GetFields(MemberFlags), "field");
private static T? Pick(Type type, string name, IEnumerable members, string kind) where T : MemberInfo
{
var matches = members.Where(m => string.Equals(m.Name, name, StringComparison.OrdinalIgnoreCase)).ToList();
if (matches.Count == 0)
return null;
var exact = matches.Where(m => m.Name == name).ToList();
if (exact.Count > 0)
return MostDerived(exact);
var names = matches.Select(m => m.Name).Distinct(StringComparer.Ordinal).ToList();
if (names.Count == 1)
return MostDerived(matches);
throw new ExpressionEvaluationException(
$"'{name}' matches more than one {kind} of {type.FullName} when case is ignored: {string.Join(", ", names)}. " +
"Write the member name in its exact case.");
}
/// A member a derived type redeclares (new) is listed once per declaring type: the derived one wins.
private static T MostDerived(List members) where T : MemberInfo
=> members.Count == 1
? members[0]
: members.First(m => members.All(o => o == m || m.DeclaringType!.IsSubclassOf(o.DeclaringType!)));
}