Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
865 views
in Technique[技术] by (71.8m points)

c# - Using GetProperties() with BindingFlags.DeclaredOnly in .NET Reflection

If I use

sometype.GetProperties();

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);

I get 0 properties.

Anyone know what I am doing wrong?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...