C# 为什么 GetProperty 找不到它?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/360422/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why does GetProperty fail to find it?
提问by Remi Despres-Smyth
I'm trying to use reflection to get a property from a class. Here is some sample code of what I'm seeing:
我正在尝试使用反射从类中获取属性。这是我所看到的一些示例代码:
using System.Reflection;
namespace ConsoleApplication
{
class Program
{
static void Main(string[] args)
{
PropertyInfo[] tmp2 = typeof(TestClass).GetProperties();
PropertyInfo test = typeof(TestClass).GetProperty(
"TestProp", BindingFlags.Public | BindingFlags.NonPublic);
}
}
public class TestClass
{
public Int32 TestProp
{
get;
set;
}
}
}
When I trace through this, this is what I see:
当我跟踪这个时,这就是我所看到的:
- When I fetch all properties using
GetProperties()
, the resulting array has one entry, for propertyTestProp
. - When I try to fetch
TestProp
usingGetProperty()
, I get null back.
- 当我使用 获取所有属性时
GetProperties()
,结果数组有一个条目,用于 propertyTestProp
。 - 当我尝试获取
TestProp
使用GetProperty()
,我得空回来。
I'm a little stumped; I haven't been able to find anything in the MSDN regarding GetProperty()
to explain this result to me. Any help?
我有点难住;我一直无法在 MSDN 中找到任何关于GetProperty()
向我解释这个结果的内容。有什么帮助吗?
EDIT:
编辑:
If I add BindingFlags.Instance
to the GetProperties()
call, no properties are found, period. This is more consistent, and leads me to believe that TestProp
is not considered an instance property for some reason.
如果我添加BindingFlags.Instance
到GetProperties()
呼叫中,则找不到任何属性,期间。这更加一致,并使我相信TestProp
由于某种原因它不被视为实例属性。
Why would that be? What do I need to do to the class for this property to be considered an instance property?
为什么会这样?我需要对类做什么才能将此属性视为实例属性?
采纳答案by Andrew Rollings
Add BindingFlags.Instance
to the GetProperty
call.
添加BindingFlags.Instance
到GetProperty
通话中。
EDIT: In response to comment...
编辑:回应评论...
The following code returns the property.
以下代码返回该属性。
Note: It's a good idea to actually make your property do something before you try to retrieve it (VS2005) :)
注意:在尝试检索它(VS2005)之前,最好先让你的财产做一些事情:)
using System.Reflection;
namespace ConsoleApplication
{
class Program
{
static void Main(string[] args)
{
PropertyInfo[] tmp2 = typeof(TestClass).GetProperties();
PropertyInfo test = typeof(TestClass).GetProperty(
"TestProp",
BindingFlags.Instance | BindingFlags.Public |
BindingFlags.NonPublic);
Console.WriteLine(test.Name);
}
}
public class TestClass
{
public Int32 TestProp
{
get
{
return 0;
}
set
{
}
}
}
}
回答by leppie
You need to specify whether it is static or an instance (or both) too.
您还需要指定它是静态的还是实例(或两者)。
回答by bruno conde
Try to add the following tag:
尝试添加以下标签:
System.Reflection.BindingFlags.Instance
EDIT:This works (at least to me)
编辑:这有效(至少对我而言)
PropertyInfo test = typeof(TestClass).GetProperty("TestProp", BindingFlags.Public | BindingFlags.Instance);
Console.WriteLine(test.Name);