C# & WPF

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:

  1. Class library Exp.Vehicle, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null with one class:
public abstract class Vehicle {}
  1. Class library Exp.Porsche, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null reference to Exp.Vehicle, with one class:
public class Porsche : Vehicle {} 
  1. Windows Application Exp.VehicleShop, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null reference to Exp.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:

public class PorscheInitializer
{
    public object GetInstance()
    {
        return new Porsche();
    }
}

In VehicleShop.cs:

public object CreateCar()
{
    return new PorscheInitializer().GetInstance();
} 
上一篇下一篇

猜你喜欢

热点阅读