AWG Blogs

Friday, April 5, 2013

Get Property by an Attribute Property Value using LINQ

//look up the attribute matching the listid using Reflection and LINQ
                Type dtoType = typeof(AppInitParamsDTO);
                PropertyInfo pi = dtoType.GetProperties(BindingFlags.Instance | BindingFlags.Public)
                            .Where(
                            x => typeof(AppInitParamsSettingsListNameAttribute)
                                .GetProperty("ListName")
                                .GetValue(Attribute.GetCustomAttribute(
x, typeof(AppInitParamsSettingsListNameAttribute)), null).ToString()
                                == listId).First();