问题描述
我有code以下行片断搜索实例通过名称属性格式:
I had the following line snippet of code that searches for a propery of an instance by name:
var prop = Backend.GetType().GetProperty(fieldName);
现在我想忽略字段名的情况下,所以我尝试了以下内容:
Now I want to ignore the case of fieldName, so I tried the following:
var prop = Backend.GetType().GetProperty(fieldName, BindingFlags.IgnoreCase);
...没有骰子。现在支撑不会找到有确切的情况下,字段名。
... No dice. Now prop won't find field names that have the exact case.
因此.....如何使用NET反射的名字忽略大小写搜索的属性?
Hence.....How do I use .Net reflection to search for a property by name ignoring case?
推荐答案
您需要指定 BindingFlags.Public | BindingFlags.Instance
以及
using System;
using System.Reflection;
public class Test
{
private int foo;
public int Foo { get { return foo; } }
static void Main()
{
var prop = typeof(Test).GetProperty("foo",
BindingFlags.Public
| BindingFlags.Instance
| BindingFlags.IgnoreCase);
Console.WriteLine(prop);
}
}
(如果不指定任何标志,公共场所,实例和静态默认情况下提供的。如果你指定它明确地建议你只指定实例或静态之一,如果你知道你需要什么。)
(When you don't specify any flags, public, instance and static are provided by default. If you're specifying it explicitly I suggest you only specify one of instance or static, if you know what you need.)
这篇关于如何使用NET反射来搜索属性的名字忽略大小写?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!