【发布时间】:2012-07-04 20:19:12
【问题描述】:
在我的解决方案资源管理器中,我有一个用于 Windows 服务 BridgeWS 的项目,另一个项目 Vytru.Platform.Bridge.Configuration 有一个静态类 SharedData.cs
我的问题:我想使用这个静态属性 SharedData.DeviceList 来获取我在 BridgeWS 服务中的设备对象列表,但它总是等于 null 吗?
这是我的解决方案
我的静态类中的一些代码
public static class SharedData
{
public const string CONFIGURATION_XML_PATH = @"\Configuration.xml";
private static List<Device> _deviceList;
public static void Initialize()
{
APPLICATION_LOCAL_PATH = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);
if (!string.IsNullOrEmpty(APPLICATION_LOCAL_PATH)) CONFIGURATION_FULL_PATH = APPLICATION_LOCAL_PATH + CONFIGURATION_XML_PATH;
_deviceList = new List<Device>();
_tempDeviceList = new List<Device>();
_deviceAgent = new DeviceManager();
_deviceAgent.Initialize();
}
public static bool AddToTempDeviceList(Device device)
{
if (_tempDeviceList != null)
{
if (!_deviceAgent.IsExist(device, true))
{
_tempDeviceList.Add(device);
return true;
}
}
return false;
}
public static bool UpdateFile()
{
_deviceList = _tempDeviceList;
return _deviceAgent.Save();
}
public static List<Device> DeviceList
{
get { return _deviceList; }
set { _deviceList = value; }
}
public static List<Device> TempDeviceList
{
get { return _tempDeviceList; }
set { _tempDeviceList = value; }
}
public static DeviceManager DeviceAgent
{
get { return _deviceAgent; }
set { _deviceAgent = value; }
}
}
感谢和抱歉我的英语不好。
【问题讨论】:
-
什么时候设置静态属性?仅仅将某些东西设为静态并不足以将实例实际设置到静态引用中。
-
你能提供一些关于
SharedData.DeviceList的代码
标签: c# windows-services static-classes