views:

45

answers:

3

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 question.

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?

+4  A: 

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"));
Jon Skeet
Sadly this seems like "please write my code for me" rather than a real question. As with all programming questions, "show me the explicit business rules and I'll show you the high level code that you need to turn into machine understandable code"
drachenstern
@drachenstern I updated the question to be more clear, no need to be a dick.
Mike
@Mike ~ a) You've been around long enough to know how to ask questions. b) Also, I didn't feel I was being a dick. Really I wanted to emphasize @Jon Skeet's comment about "turning predicate into code".
drachenstern
A: 

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

Itay
+1  A: 

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

ck