在类库“对象引用未设置为对象的实例”中使用MEF导入时接收到空异常?

问题描述

在c#类库项目中尝试使用导入MEF范例时遇到一个奇怪的问题。基本上,我有一个DLL,它具有IDevicePreferences的实现。调用devicePreferences.Callsign时,出现以下异常:

System.NullReferenceException: 'Object reference not set to an instance of an object.'

devicePreferences was null.

我将此类库项目嵌入到另一个项目中使用的DLL中。换句话说,我试图通过实例化具有DLL的标准C#.Net项目中的WeatherClient对象,在类库中进行MEF。 (我的项目->我的类库-> IDevicePreferences DLL)

有趣的是,我能够在我的项目中实现IDevicePreferences接口(那里也有DLL),没有问题。但是我无法从我的类Library / WeatherClient类的 inside 中使用它。

这是WeatherClient类:

public class WeatherClient
{
    [Import(typeof(IDevicePreferences))]
    public IDevicePreferences devicePreferences;

    public WeatherClient(String serverIp,int port)
    {
        this.serverIp = serverIp;
        this.port = port;
        System.Diagnostics.Debug.WriteLine("callsign: " + devicePreferences.Callsign);
    }
}

关于我在做什么错的任何想法?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)