在 .NET 反射中使用 GetProperties() 和 BindingFlags.DeclaredOnly [英] Using GetProperties() with BindingFlags.DeclaredOnly in .NET Reflection

查看:16
本文介绍了在 .NET 反射中使用 GetProperties() 和 BindingFlags.DeclaredOnly的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我使用

sometype.GetProperties();

我从类型和它的父级中获取所有属性.但是,我只想检索在此类型(而不是父项)中明确定义的属性.我认为这就是 BindingFlags.DeclaredOnly 选项的用途.

I get all of the properties from the type and it's parent. However I only want to retrieve the properties defined explicitly in this type (not the parents). I thought that was what the BindingFlags.DeclaredOnly option was for.

但是,当我尝试这样做时:

However, when I try this:

sometype.GetProperties(BindingFlags.DeclaredOnly);

我得到 0 个属性.

有人知道我做错了什么吗?

Anyone know what I am doing wrong?

推荐答案

如果您指定任何 BindingFlags,那么您需要明确指定您想要获得哪些属性.例如:

If you specify any BindingFlags, then you need to specify explicitly what properties you want to get. For example:

sometype.GetProperties (BindingFlags.DeclaredOnly | 
                        BindingFlags.Public | 
                        BindingFlags.Instance);

这篇关于在 .NET 反射中使用 GetProperties() 和 BindingFlags.DeclaredOnly的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆