如何从外部类的内部类访问属性?

时间:2016-11-11 10:25:57

标签: c#

这里有一些代码:

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);
        }
    }
    :
}

有没有办法从外部类的内部类“设置”属性,但不允许从外部设置它?只有外部类必须能够设置内部类属性。

2 个答案:

答案 0 :(得分:1)

您可以使用外部类可访问的公共属性/方法创建private内部类,但不能创建外部任何内容。如果您希望内部类的一部分是公共的,则从某种公共接口派生私有内部类(根据您的需要,可以是interfaceclassabstract class

class Program
{
    static void Main(string[] args)
    {
        var t = new ClockTimer();
        t.Start();
        var date = t.TimerTimesInstance.Start; // getter Ok
        t.TimerTimesInstance.Start = DateTime.Now; // Error! setter denied
    }
}
public class ClockTimer
{
    public interface ITimerTimes
    {
        DateTime? Start { get; }
    }
    private class TimerTimes : ITimerTimes
    {
        public DateTime? Start { get; set; }
    }

    private TimerTimes m_TimerTimes = null;
    public virtual void Start()
    {
        m_TimerTimes = new TimerTimes();
        m_TimerTimes.Start = DateTime.Now;  //Property Start is assessible here
    }

    public ITimerTimes TimerTimesInstance { get { return m_TimerTimes; } }
}

如你所见,我稍微减少了一些例子,但所有必要的代码都应该在其中。

答案 1 :(得分:0)

如果要从外部类访问属性,请使用public进行要使用的操作。在您的情况下,您只能阅读属性get;,而不能使用set进行更改或更改:

public DateTime? Start { get; set; }

同样适用于您的嵌套类。如果您要在 ClockTimer 之外访问 TimerTimes ,请将其保留为public,否则请将其设置为private以仅允许 ClockTimer 访问它(缩短示例):

public class ClockTimer {

    private class TimerTimes {
        public DateTime? Start { get; set; }
    }

    public ClockTimer() {
        var timerTimes = new TimerTimes();
        timerTimes.Start = DateTime.Now;
    } 
}