Posted by: Zeeshan Amjad | August 8, 2009

Using .Net Reflection in WPF


Using reflection in WPF is exactly the same as using in any other .net application. What does it mean to use Reflection in WPF application? It simply means to show the results returned by the reflection in WPF.

First take a look at reflection. To use reflection first we have to use one namespace named System.Reflection.

Theoretically we can do lots of thing using reflection, but in our discussion we focus only one portion of it. We are interested to see the classes, interfaces, enumerated and other important types in any .Net assembly weather it is DLL or EXE. Hers is a simple code to do this.

  1:                  Assembly asm = Assembly.LoadFrom("C:\MyManagedAssembly.exe");
  2: 
  3:                 Type[] types = asm.GetTypes();
  4: 
  5:                 foreach (Type type in types)
  6:                 {
  7:                     TypeInfo typeInfo = new TypeInfo();
  8: 
  9:                     // do your stuff
 10:                 }
 11: 

Now lets make a simple program. In this program we gives a facility to the users to select the file using windows common dialog box and then get some basic information about that assembly using reflection and display in list control. To use the common dialog box, we have to include one more namespace named “Microsoft.Win32”. Here is a simple XAML file of this program.

  1: <Window x:Class="WpfReflection.Main"
  2:     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  3:     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  4:     Title="Main" Height="400" Width="600" ResizeMode="NoResize" WindowStartupLocation="CenterScreen">    
  5:     <Grid>        
  6:         <Grid.Background>
  7:             <LinearGradientBrush>
  8:                 <LinearGradientBrush.GradientStops>
  9:                     <GradientStop Offset="0" Color="Wheat"/>
 10:                     <GradientStop Offset="1" Color="LightCoral"/>
 11:                 </LinearGradientBrush.GradientStops>                
 12:             </LinearGradientBrush>            
 13:         </Grid.Background>
 14:         
 15:         <Grid.RowDefinitions>
 16:             <RowDefinition/>
 17:             <RowDefinition Height="5*" />
 18:             <RowDefinition/>
 19:         </Grid.RowDefinitions>
 20:         
 21:         <Grid.ColumnDefinitions>
 22:             <ColumnDefinition/>
 23:             <ColumnDefinition/>
 24:             <ColumnDefinition/>
 25:             <ColumnDefinition/>
 26:             <ColumnDefinition/>
 27:         </Grid.ColumnDefinitions>
 28:         
 29:         <TextBlock Grid.Row="0" Grid.Column="0" Margin="5" VerticalAlignment="Center">Name</TextBlock>
 30:         <TextBox Grid.Row="0" Grid.Column="1" Grid.ColumnSpan="3" VerticalAlignment="Center" Margin="5" Name="txtPath"></TextBox>
 31:         <Button Grid.Row="0" Grid.Column="4" Margin="5" Height="25" Width="50" Name="btnPath" Click="btnPath_Click">Browse</Button>
 32:         
 33:         <ListView Grid.Row="1" Grid.Column="0" Grid.ColumnSpan="5" Margin="5" Name="lstResult">
 34:             <ListView.View>
 35:                 <GridView>
 36:                     <GridViewColumn Width="100" Header="Type" DisplayMemberBinding="{Binding Path = Type}"/>
 37:                     <GridViewColumn Width="450" Header="Name" DisplayMemberBinding="{Binding Path = TypeName}"/>
 38:                 </GridView>
 39:             </ListView.View>
 40:         </ListView>
 41:         <Button Grid.Row="2" Grid.Column="2" Margin="5" Height="25" Width="50" Name="btnExit" Click="btnExit_Click">Exit</Button>
 42:     </Grid>
 43: </Window>
 44: 

And here is a simple C# code of this program.

  1: using System;
  2: using System.Collections.Generic;
  3: using System.Windows;
  4: using System.Windows.Controls;
  5: using Microsoft.Win32;
  6: using System.Reflection;
  7: 
  8: namespace WpfReflection
  9: {
 10:     public class TypeInfo
 11:     {
 12:         public String Type
 13:         { get; set; }
 14: 
 15:         public String TypeName
 16:         { get; set; }
 17:     }
 18: 
 19:     /// <summary>
 20:     /// Interaction logic for Main.xaml
 21:     /// </summary>
 22:     public partial class Main : Window
 23:     {
 24:         public Main()
 25:         {
 26:             InitializeComponent();
 27:         }
 28: 
 29:         private void btnExit_Click(object sender, RoutedEventArgs e)
 30:         {
 31:             Close();
 32:         }
 33: 
 34:         private void btnPath_Click(object sender, RoutedEventArgs e)
 35:         {
 36:             OpenFileDialog dlg = new OpenFileDialog();
 37: 
 38:             Nullable<bool> result = dlg.ShowDialog();
 39: 
 40:             if (result == true)
 41:             {
 42:                 txtPath.Text = dlg.FileName;
 43:                 Title = dlg.FileName;
 44: 
 45:                 Assembly asm = Assembly.LoadFrom(txtPath.Text);
 46: 
 47:                 Type[] types = asm.GetTypes();
 48: 
 49:                 foreach (Type type in types)
 50:                 {
 51:                     TypeInfo typeInfo = new TypeInfo();
 52: 
 53:                     if (type.IsClass)
 54:                     {
 55:                         typeInfo.Type = "Class";
 56:                         typeInfo.TypeName = type.FullName;
 57:                     }
 58:                     else if (type.IsInterface)
 59:                     {
 60:                         typeInfo.Type = "Interface";
 61:                         typeInfo.TypeName = type.FullName;
 62:                     }
 63:                     else if (type.IsEnum)
 64:                     {
 65:                         typeInfo.Type = "Enum";
 66:                         typeInfo.TypeName = type.FullName;
 67:                     }
 68:                     else if (type.IsAbstract)
 69:                     {
 70:                         typeInfo.Type = "Abstract";
 71:                         typeInfo.TypeName = type.FullName;
 72:                     }
 73:                     else if (type.IsGenericType)
 74:                     {
 75:                         typeInfo.Type = "Generic";
 76:                         typeInfo.TypeName = type.FullName;
 77:                     }                    
 78: 
 79:                     lstResult.Items.Add(typeInfo);
 80:                 }
 81:             }
 82:         }
 83:     }
 84: }
 85: 

Here is the output of this program when run on one of the WPF dll named “WindowsBase.dll”.

WpfReflection

Advertisements

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

Categories

%d bloggers like this: