0

I am preloading an assembly in the Application_Start() using Assembly.LoadFrom("pathtomyassembly").

Why am I not able to get my assembly using Assembly.Load("MyAssemblyName") just like I can with any other assembly loaded automatically from the bin directory.

Is there a way to do this without calling LoadFrom() with the assembly path every time?

UPDATE:

The weird thing is, when I use

AppDomain.CurrentDomain.GetAssemblies()

I can see that my assembly is loaded in the AppDomain.

Is there a way to get it without looping through the assemblies?

Jason
  • 4,557
  • 5
  • 31
  • 40
  • can you show some code and explain why you load them manually instead of referencing them in the project? – Davide Piras Sep 16 '11 at 14:07
  • @Davide Piras: There is not much code to show. I use Assembly.LoadFrom(assemblyPath) at runtime and I then need to instanciate some custom classes from those assemblies on demand. Basically, I have a common code base running as a child application under each of my custom application. When I call a webpage from this "base" application, I need to be able to access a class from my custom assembly. – Jason Sep 16 '11 at 14:20
  • so you need this: http://stackoverflow.com/questions/1803540/load-assembly-at-runtime-and-create-class-instance ?? – Davide Piras Sep 16 '11 at 14:24
  • @Davide Piras: Activator.CreateInstance with the class name and the assembly does not seem to work either. I works fine when I create an instance of a class from the assemblies in my bin folder, but not with the ones I load dynamically. – Jason Sep 16 '11 at 14:34

3 Answers3

1

you need to supply fully qualified name Assembly.Load [Assembly].Load("Assembly text name, Version, Culture, PublicKeyToken").

E.g.

Assembly.Load("ActivityLibrary, Version=2.0.0.0, Culture=neutral, PublicKeyToken=null");

Ref:

http://geekswithblogs.net/rupreet/archive/2010/02/16/137988.aspx

http://msdn.microsoft.com/en-us/library/x4cw969y.aspx

Priyank
  • 10,503
  • 2
  • 27
  • 25
0

My hunch is you can't use Assembly.Load("MyAssemblyName") because there's no matching DLL in your program's bin directory. Your build script would need to manually copy that DLL from pathtomyassembly to your program's bin directory, or you'd need to add it as a link in your solution and make sure "Copy to Output Directory" is checked.

BishopRook
  • 1,240
  • 6
  • 11
0

I ended up looping through AppDomain.CurrentDomain.GetAssemblies() to get my assembly.

public static Assembly GetAssemblyFromAppDomain(string assemblyName)
{
    foreach (var assembly in AppDomain.CurrentDomain.GetAssemblies())
    {
        if (assembly.FullName.StartsWith(assemblyName + ",")) 
        {
            return assembly;
        }
    }
    return null;   
}

Everything seems to work fine.

Jason
  • 4,557
  • 5
  • 31
  • 40