【问题标题】:Searching with the name parameter of the Type.GetMember method使用 Type.GetMember 方法的名称参数进行搜索
【发布时间】:2013-05-30 13:51:07
【问题描述】:
我目前知道Type.GetMember Method的所有方法重载的第一个参数:
- 姓名
- 类型:System.String
包含公众名称的字符串
会员获得。
区分大小写,允许通过匹配进行搜索:
-
确切成员名称
Type myType = myString.GetType();
// Get the members for myString which are named Compare.
MemberInfo[] myMembers = myType.GetMember("Compare");
-
使用* 通配符以给定值开始的所有成员名称
Type myType = myString.GetType();
// Get the members for myString starting with the letter C.
MemberInfo[] myMembers = myType.GetMember("C*");
// Get the members for myString starting with the string Comp.
myMembers = myType.GetMember("Comp*");
如果你只使用*作为参数值,你甚至可以获得所有可用的成员。
我的问题是:除了上述两种方法之外,是否可以使用其他类型的字符串模式来匹配成员(例如C*e 或Compar??)
【问题讨论】:
标签:
c#
syntax
parameters
wildcard
system.reflection
【解决方案1】:
不,.NET Framework 代码不包含任何其他通配符的处理。
System.RuntimeType.FilterHelper 内部由GetMember 使用的代码:
// System.RuntimeType
private static void FilterHelper(BindingFlags bindingFlags, ref string name, bool allowPrefixLookup, out bool prefixLookup, out bool ignoreCase, out RuntimeType.MemberListType listType)
{
prefixLookup = false;
ignoreCase = false;
if (name != null)
{
if ((bindingFlags & BindingFlags.IgnoreCase) != BindingFlags.Default)
{
name = name.ToLower(CultureInfo.InvariantCulture);
ignoreCase = true;
listType = RuntimeType.MemberListType.CaseInsensitive;
}
else
{
listType = RuntimeType.MemberListType.CaseSensitive;
}
if (allowPrefixLookup && name.EndsWith("*", StringComparison.Ordinal))
{
name = name.Substring(0, name.Length - 1);
prefixLookup = true;
listType = RuntimeType.MemberListType.All;
return;
}
}
else
{
listType = RuntimeType.MemberListType.All;
}
}