Tag Archives: Reflection

Uses for Silverlight reflection, pt. II

So, as promised, here is the followup to what I was doing with Silverlight reflection that made me need access to Internal members of a class.

Localization in Silverlight is still an interesting story and everybody seems to have their own way of doing it.  The client I’m on uses Excel spreadsheets that load into a database. The localization data then pulls when a usercontrol loads and changes the controls data based on the locale and what was in the database.

This was very tedious for developers to setup.  You would create your UI and then have to go back and pull the default text for your controls and add them all to the spreadsheet.

Using reflection I can create an instance of all the user controls in my assembly and display them to the user in a listbox.  From there, when they select a user control, I can reflect on that type and display all the controls defined in the UserControl to the user.  When you define a control in xaml and give it a name, it becomes an internal member of a partial class generated by Visual Studio.

So, what I do is when you are all done with your UserControl you simply run my tool on the assembly and it reflects through and shows all the UserControls. When you select one, it uses reflection to create an instance with the default constructor, which initializes all your controls properties.

From there I grab all the controls through reflection and use some logic to get which properties of each I want to localize.  For TextBlocks I grab the Text property, for ContentControls it’s the Content property, and so on.

Using that I generate a simple tab-delimited string with all the default localization for all the controls and the user can copy/paste right into Excel and move on to their next task and let someone else do the translating/localizing for them.

Silverlight Reflection and internal members

If you want to use reflection in Silverlight you have to come to grips with the fact that you can only reflect on public members of the class.  This is a very important part of the security features in Silverlight.

However, I have a scenario that would be greatly helped by being able to reflect on internal members.  The documentation I found on MSDN said that as long as the member was visible at compile time you could reflect on it.  This should include internal members in the same assembly or an assembly that’s the target of InternalsVisibleTo.

I was having problems, at first I just tried setting the InternalsVisibleTo attribute in the AssemblyInfo.cs for my project to the project that had my reflection code. 

No such luck, even though I could access the members through normal means.  What I was missing was setting the BindingFlags attribute while calling GetMembers on my type. 

If you pass in BindingFlag.NonPublic | BindingFlags.Public | BindingFlags.Instance it should all work for you now.

Up next will be a post sometime about why I wanted this ability.