这里是一些代码:如何从外部类的内部类访问属性?
public class TimerEventArgs : EventArgs
{
public Timer Timer { get; }
public ClockTimer.TimerTimes Times { get; }
public DateTime? RunTime { get; }
public TimerEventArgs(Timer Timer, ClockTimer.TimerTimes Times, DateTime? RunTime)
{
this.Timer = Timer;
this.Times = Times;
this.RunTime = RunTime;
}
}
public class ClockTimer
{
public class TimerTimes
{
public DateTime? Start { get; private set; }
public TimeSpan? RunDuration { get; private set; }
public DateTime? Stop { get; private set; }
public TimerTimes()
: this(null, null, null)
{}
public TimerTimes(DateTime? Start, TimeSpan? RunDuration, DateTime? Stop)
{
this.Start = Start;
this.RunDuration = RunDuration;
this.Stop = Stop;
}
}
:
private TimerTimes m_TimerTimes = null;
:
public virtual void Start()
{
// Start timer if not running.
if (!IsRunning)
{
if (m_Timer == null)
m_Timer = new Timer();
m_Timer.Interval = Interval;
m_Timer.Tick += new EventHandler(InnerTimerHandler);
if (m_TimerTimes == null)
m_TimerTimes = new TimerTimes();
m_TimerTimes.Start = DateTime.Now; //Property Start is inaccesssable!!
m_Timer.Start();
TimerEventArgs EventArgs = new TimerEventArgs(m_Timer, m_TimerTimes);
OnTimerStarted(EventArgs);
}
}
:
}
有没有一种方法来“设置”,从一个内部类的属性在外部类中,但不允许它从外部设置?只有外部类必须能够设置内部类的属性。
你能指定你的目的吗?你的意思是哪个属性?请给我一个样品 – rbr94
行:m_TimerTimes.Start = DateTime.Now; – Ron
您的代码非常不完整,无法复制粘贴为工作示例。并且它包含许多对于问题不必要的噪音 – grek40