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.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>