开发者

What is the best way to get only certain properties using reflection?

I am trying to come up with the best way to get only certain properties from a type using reflection. How can I differentiate the properties from each other?

Let me add this to help clarify my que开发者_开发百科stion.

I understand that I can use binding flags or name. But say I want only a certain four properties. Would the best way be to create a custom attribute for the ones I want then loop through all of the properties to see if they have that attribute?


Well, fairly obviously by name, or by type, or by declaring type (e.g. the type or its base class).

Basically if you can describe what you mean by "certain properties" it's just a matter of turning that predicate into code. For example, suppose you only wanted properties beginning with A:

var properties = type.GetProperties().Where(p => p.Name.StartsWith("A"));


System.Reflection.BindingFlags are designed to allow you to filter things like public / private, member / static when reflecting types.


You can use the Type.GetProperty(string) to get certain property.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜