Skip to content

Finding all types in a xaml xmlns – code example

February 5, 2007

In the forums, "Jlansing" is digging into finding instances of DPs and also how to map TypeNames to the actual types.

Here is a code example of how to solve the typename to Type mapping as Xaml does it.

I’ll enhance the code over time based on feedback.

string xmlNamespaceToEnumerate = "http://schemas.microsoft.com/winfx/2006/xaml/presentation";
List<Assembly> assemblyList = new List<Assembly>();

assemblyList.Add(typeof(System.Windows.Controls.Button).Assembly);

assemblyList.Add(typeof(System.Windows.Media.Brush).Assembly);

assemblyList.Add(typeof(System.Windows.Markup.XmlnsDefinitionAttribute).Assembly);

Dictionary<string, Type> typeDictionary = new Dictionary<string, Type>();

List<string> clrNamespaces = new List<string>();

foreach (Assembly a in assemblyList)

{

  //Find all ClrNamespaces that match

  object[] attrs = a.GetCustomAttributes(typeof(System.Windows.Markup.XmlnsDefinitionAttribute), false);

  foreach (object attr in attrs)

  {

    System.Windows.Markup.XmlnsDefinitionAttribute xmlnsDef = attr as System.Windows.Markup.XmlnsDefinitionAttribute;

    if (xmlnsDef.XmlNamespace == xmlNamespaceToEnumerate)

    {

      clrNamespaces.Add(xmlnsDef.ClrNamespace);

    }

  }

  foreach (Type t in a.GetTypes())

  {

    if (t.IsPublic && clrNamespaces.Contains(t.Namespace))

    {

      typeDictionary.Add(t.Name, t);

    }

  }

}

From → XAML (non-UI)

4 Comments
  1. Unknown permalink

    This is some great/cool stuff! Thank you very much for all your help with this. I will keep an eye out to see where this goes.
    -John Lansing

  2. Drake permalink

    thank you for this nice piece of code

  3. Unknown permalink

    lotro gold lord of the rings gold lord of the rings online gold lotro gold lord of the rings gold lord of the rings
    online gold Warhammer gold
    Warhammer online gold Warhammer money War gold War money Tabula Rasa Credit lotro gold lord of the rings gold lord of the rings online gold lord of the
    rings online gold lord of the rings
    gold Tabula Rasa Credit World of Warcraft gold PotBS Doubloon Pirates of the
    Burning Sea Doubloon PotBS Gold Pirates of the Burning Sea
    Gold lotro gold lord of the rings gold lord of the rings online gold Tabula Rasa Credit Warhammer gold Warhammer online gold PotBS Doubloon PotBS gold Pirates of the Burning Sea
    gold Pirates of the
    Burning Sea Doubloon

  4. Unknown permalink

    hofg9y 0mexx1 wow gold wow gold wow gold wow gold wow gold wow gold wow power leveling wow power leveling 股票软件 股票软件 大智慧 大智慧 上海水泵 上海水泵 胶体磨 胶体磨 黑马股推荐 黑马股推荐 东方医院 东方医院 wow gold wow gold wow gold wow gold wow gold wow gold wow gold wow gold wow gold wow gold wow gold wow gold wow gold wow gold wow gold wow gold wow gold wow gold wow gold wow gold wow gold wow gold wow gold wow gold 黑马股推荐 上海水泵

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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

%d bloggers like this: