Rameez Ahmed Sayad
Rameez Ahmed Sayad

Reputation: 1300

How to load a referenced assembly within another referenced assembly?

I have a scenario where my client is a WPF App let's say W , There are 3 assemblies say A, B,C which are not to be exposed to W , hence the 3 assemblies are referenced in a concrete factory , say F which creates the required instance and provides to W.

I am using reflection in F to generate the 3 objects.

Below is the code

return (IClass)Activator.CreateInstance(Assembly.Load("A").GetType("A.AClass"), 
BindingFlags.NonPublic | BindingFlags.Instance, null, null, null);

The code works if I reference A in W , but doesn't work (System.IO.FileNotFound Exception) when A is referenced in F (I cross checked with Assembly.GetExecutingAssembly().GetReferencedAssemblies() and A is not loaded).

I read that an Assembly may not be loaded if it is not required. But how does my WPF client then load the assembly. If this is the expected behavior , I would go with finding the current Directory path and appending A.dll to it. Any thoughts around ?

Upvotes: 0

Views: 112

Answers (1)

Troy
Troy

Reputation: 567

I suspect the files are not getting copied to the AppDomain.CurrentDomain.BaseDirectory when the solution is built. Without a reference you would need to manually ensure the required DLLs are being copied to the right location.

See BasconSah comment above for a suggestion.

Upvotes: 1

Related Questions