Fix error when member is not found with string key during PDT interpretation.

This commit is contained in:
2023-04-08 13:34:37 +08:00
parent 916c55b4b2
commit 2221658e7a
2 changed files with 17 additions and 1 deletions

View File

@@ -266,7 +266,7 @@ namespace Cryville.Common.Pdt {
else {
MemberInfo prop = null;
bool flag = false;
if (pkey is string) prop = ReflectionHelper.GetMember(type, (string)pkey);
if (pkey is string) prop = ReflectionHelper.TryGetMember(type, (string)pkey);
if (prop == null) flag = ReflectionHelper.TryFindMemberWithAttribute<T>(type, out prop);
if (prop == null) throw new MissingMemberException(string.Format("The property \"{0}\" is not found", pkey));
Type ptype = ReflectionHelper.GetMemberType(prop);

View File

@@ -90,6 +90,22 @@ namespace Cryville.Common {
return mil[0];
}
/// <summary>
/// Tries to get the member from a type with the specified name.
/// </summary>
/// <param name="type">The type.</param>
/// <param name="name">The name of the member.</param>
/// <returns>The member. <see langword="null" /> when not found.</returns>
public static MemberInfo TryGetMember(Type type, string name) {
var mil = type.GetMember(
name,
MemberTypes.Field | MemberTypes.Property,
BindingFlags.Public | BindingFlags.Instance
);
if (mil.Length != 1) return null;
return mil[0];
}
/// <summary>
/// Gets the type of a member.
/// </summary>