1

Hello I have this code that worked to load a dll from a project in my solution. The Dll is now in another solution.

public void LoadCalculationExpert(string pathToExpert)
{
    var assembly = Assembly.LoadFrom(pathToExpert);
    var type = assembly.GetType("Expert.CalculationExpert");
    var calculationExpert = (ICalculationExpert)Activator.CreateInstance(type);
    this._container.RegisterInstance(calculationExpert, new ContainerControlledLifetimeManager());
}

The Dll implement an interface perfectly identical to ICalculationExpert. In fact I copied interface to the other solution. So I know that the cast should work because it is an object that implement the same interface.

The thing is it throws me invalid cast exception.

My guess is the interface being in another namespace screw the cast.

The CalculationExpert and ICalculationExpert is in namespace Expert in another solution. ICalculationExpert in my solution is in namespace GSoft.Sons.Bll.ScoreExpert

Any idea or resource I could read ?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Xavier
  • 329
  • 1
  • 2
  • 15

1 Answers1

0

By OOP principles copying the interface over to your solution is not going to make it identical. If you have access to the dll (namespace Expert with ICalculationExpert interface) why not add dll reference in your solution by which you can implement the same interface from namespace Expert.

Prashanth Thurairatnam
  • 4,353
  • 2
  • 14
  • 17
  • 1
    Alternatively have the interface in a seperate assembly and refer the dll in both the solutions and implement the interface. – Prashanth Thurairatnam Apr 25 '12 at 13:52
  • That's what I usually do. Unfortunately, the code in the dll I load is secret. I give the interface to them and they implement it. Then I must load their dll with the interface. I can't know in advance what namespace their interface or class is in. The only thing I know is their interface is identical to mine. – Xavier Apr 25 '12 at 13:55
  • I hope this would give a solution to you http://stackoverflow.com/questions/3116694/invalidcastexception-of-a-activator-createinstance-object-during-an-installation – Prashanth Thurairatnam Apr 25 '12 at 14:30
  • You are welcome. reference assembly loading across AppDomains by Rick Strahl http://www.west-wind.com/weblog/posts/2009/Jan/19/Assembly-Loading-across-AppDomains – Prashanth Thurairatnam Apr 25 '12 at 14:32