我有界面:
public interface IProcess
{
void Step_One();
void Step_Two();
Timer ProcessTimer{get; set;}
}
基类..
public class ProcessBase
{
protected Timer processTimer;
public Timer ProcessTimer{get{ return processTimer;}set{processTimer=value;}}
//sets up all the common objects
public ProcessBase()
{
}
//This constructor will call the default constructor ^
protected ProcessBase(long intervalArg) : this()
{
processTimer = new Timer(intervalArg);
processTimer.Enabled = true;
}
}
具体课程
public class ReportedContentProcess : ProcessBase, IProcess
{
public ReportedContentProcess(): base(5000)
{
}
public void Step_One()
{
}
public void Step_Two()
{
}
}
但是当我尝试在工厂里把它拿出来时......
public static class ProcessFactory
{
public static List<IProcess> GetProcessors()
{
ReportedContentProcess.ReportedContentProcess reportedContentProcess = new ReportedContentProcess.ReportedContentProcess();
List<IProcess> retProcesses = new List<IProcess>();
retProcesses.Add(reportedContentProcess);
return retProcesses;
}
}
然后将处理程序附加到计时器...
processorsForService = ProcessFactory.GetProcessors();
foreach(IProcess p in processorsForService)
{
p.ProcessTimer.Elapsed += new ElapsedEventHandler(IProcess_Timer_Elapsed);
}
我收到运行时错误,说p.ProcessTimer为null。为什么是这样?我在基类中继承并实例化了解为什么它的null。我甚至把它包含在界面中......
答案 0 :(得分:1)
我看到你只是在第二个构造函数中初始化Timer
。如果调用默认值(即没有参数的构造函数)怎么办?