Tuesday, February 16, 2010

Enumerating ResourceDictionary Keys in Silverlight 2

The Microsoft response to this need is that the feature was not implemented in Silverlight 2. However, enumerating ResourceDictionary Keys is possible through the use of reflection. Aside from the performance cost incurred by reflection, the only other caveat I'm aware of is that the end-user must have the Silverlight 3 runtime installed for this to work.


ResourceDictionary dictionary = new ResourceDictionary()
{
{ "a", "" },
{ "b", "" },
{ "c", "" }
};

PropertyInfo prop = typeof(ResourceDictionary).GetProperty("Keys");
object[] keys = prop.GetValue(dictionary, null) as object[];

foreach (object o in keys)
MessageBox.Show(o.ToString());

No comments:

Post a Comment