![](/img/trans.png)
[英]How do I use reflection to get properties explicitly implementing an interface?
[英]How do I get the properties of an object using reflection?
我知道我可以做到這一點
foreach (PropertyInfo property in myobject.GetType().GetProperties())
{
if (property.DeclaringType.ToString() == myobject.GetType().ToString())
{
// only have my object properties here
// and not parent of my object properties
}
}
但如何才能使我剛剛得到的MyObject的性質,而不是那些父母的呢? 即不必做額外的 if 語句。
編輯答案,(感謝@Greg Beech)這有效:-
foreach (PropertyInfo property in
myobject.GetType().GetProperties
(BindingFlags.Public |
BindingFlags.DeclaredOnly |
BindingFlags.Instance))
{
// only properties of my object not parent of myobject
}
我還找到了這個鏈接http://msdn.microsoft.com/en-us/library/4ek9c21e.aspx
查看BindingFlags.DeclaredOnly
並將其傳遞給GetProperties
(您可能希望至少將它與BindingFlags.Public
和BindingFlags.Instance
結合起來)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.