Using LINQ and reflection together

I am in the middle of writing a new article for codeproject which uses Amazon Web Services (AWS) to obtain a array of Details[] objects back from a call to the web service proxy. I want to then use some of this data retrieved for an individual Details object, to display on a WPF Flow Document. I had a think about this and thought man, I really dont want to write some nasty loop. LINQ immediately came to mind, unfortunatley the Amazon web service Details object is not something that can be queried using LINQ. As its a single class with lots of properties, and doesnt implement IEnumerable which LINQ needs. This is the case as Amazon Web Services (ASMX) has to cater for all languages, so a simply array is returned. Not cool for LINQability. Mmmm Grrr.

So I had another think about this, and thought, why not just get all the properties that I would like the values of using reflection, and then use LINQ over the resulting array of PropertyInfo (PropertyInfo[]) which is of course LINQ query ready, as an array of PropertyInfo implements IEnumerable.

The resultanat code is very simple, but allows me to query the single Detail (Amazon Web Service implementation)

Lets see that now shall we


And that is all there was to it


One Comment

  1. Hi Sacha,

    I’m considering using LINQ and Reflection for finding plugin modules (and also matching modules that are part of the executing assembly). LINQ seems like a great approach (if I decide to require .NET framework greater than 2.0). My question is, how fast is it compared to looping through interfaces and types to find matches?


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s