Exception Handler
2018-04-17 本文已影响0人
Kreiven
Related Assemblies should be referenced
If you get an error like: Exp.Vehicle, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null should be referenced
, I got a solution here, if there's other better and efficient solution, please leave notes;).
For example, there're three assemblies:
- Class library
Exp.Vehicle, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null
with one class:
public abstract class Vehicle {}
- Class library
Exp.Porsche, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null
reference toExp.Vehicle
, with one class:
public class Porsche : Vehicle {}
- Windows Application
Exp.VehicleShop, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null
reference toExp.Porsche
, with one class:
public class VehicleShop {}
Now in the class VehicleShop
, we will need a method to create an instance of Porsche
.
So as usual we write like:
public object CreateCar()
{
return new Porsche(); //Here we get an error: `Exp.Vehicle, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null` should be referenced.
}
In this situation, if we won't able to modify VehicleShop's references
Maybe VehicleShop is a host application that creates vehicles via
System.Reflection
.
My solution is:
- In assembly
Exp.Porsche
, we need an additional class that do the specific work, so thatVehicleShop
can do the creation by calling it:
InPorscheInitializer.cs
:
public class PorscheInitializer
{
public object GetInstance()
{
return new Porsche();
}
}
In VehicleShop.cs
:
public object CreateCar()
{
return new PorscheInitializer().GetInstance();
}