Monday, March 28, 2011

Automatically load .Net assemblies from sub-folder

If you need to put a set of assemblies in a sub folder of output directory, you can set that path as private path of the appdomain. Here is a way to do it and also make it configurable so that a new path can be added easily with recompiling.

Add below entry to App.config
<configuration>
  <configSections>
     <section name="AdditionalProbingPaths" type="System.Configuration.SingleTagSectionHandler"></section>
  </configSections>
</configuration>
<AdditionalProbingPaths Path1="FolderPath" Path2="AnotherPath" Path3="BlahBlah" />


Now in your Main Method write below code.
public static void Main(string[] args_)
{
  var section = (Hashtable)System.Configuration.ConfigurationManager.GetSection("AdditionalProbingPaths");
  foreach (string path in section.Values)
  {
     AppDomain.CurrentDomain.AppendPrivatePath(path);
   }
}

Now when any assembly is to be resolved by run time it will also look at these path. Easy!

Type based templating

            <DataTemplate DataType="{x:Type vm:SampleViewModel}">                 <DataTemplate.Resources>       ...