views:

248

answers:

1

I have existing code for an ASP .NET application that uses reflection to load dataproviders. I would like to re-use this code in a WPF application but it appears that BuildManager.GetType only looks through top level assemblies if the app isn't ASP .NET. Does anyone know how to get around this limitation?

The following code throws an exception saying DotNetNuke.Data.MySqlDataProvider can't be found in the System.Web assembly. The assembly that contains the DotNetNuke.Data.MySqlDataProvider class definitely exists in the bin folder of the compiled WPF app.

Dim objType Type = BuildManager.GetType("DotNetNuke.Data.MySqlDataProvider", True, True)
A: 

Try using AppDomain to look for the type, like this:

 private Type GlobalGetType(string typeName) 
 {
  Type t = null;

  foreach (Assembly ass in AppDomain.CurrentDomain.GetAssemblies()) 
  {
   Type assType = ass.GetType(typeName);

   if (t != null && assType != null)
    throw new ArgumentException("The specified type was found on different assemblies (\"" + t.Assembly.FullName + "\" and  \"" + ass.FullName + "\")");

   if (assType != null)
    t = assType;
  }

  return t;
 }

It will look in every assembly of the application (including the framework assemblies). I don't know if there's a way to know if the assembly is a top-level assembly, witch would optimize the search, but it's still pretty fast.

Thats just what I was looking for at the time :) I ended up doing it a different way though but i'll still give you the answer
Alex