简体   繁体   English

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

[英]How to access a property from an inner class in an outer class?

here is some code : 这是一些代码:

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

Is there a way to "set" a property from an inner class in an outer class, but not allow it to be set from outside? 有没有办法从外部类的内部类中“设置”属性,但不允许从外部进行设置? Only the outer class must be capable to set the inner class property. 只有外部类必须能够设置内部类属性。

You can create a private inner class with public properties / methods that are accessible to the outer class but not to anything further outside. 您可以创建具有公共属性/方法的private内部类,该外部类可以访问该属性,但外部不可以访问任何其他属性。 If you want part of the inner class to be public, derive the private inner class from some sort of public interface (which can be an interface , class or abstract class depending on your needs). 如果您希望内部类的一部分是公共的,请从某种公共接口(根据需要可以是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; } }
}

As you see, i reduced the example a bit, but all necessary code should be in it. 如您所见,我将示例简化了一些,但是所有必需的代码都应包含在其中。

If you want to access a property from an outside class, use public for the operation you want to use. 如果要从外部类访问属性,请对要使用的操作使用public。 In your case you can only read the property get; 在您的情况下,您只能读取属性get; and not write or change it with set : 而不用set编写或更改它:

public DateTime? Start { get; set; }

The same is applied to your nested class. 嵌套类也是如此。 If you want to access TimerTimes outside of ClockTimer leave it as public , otherwise set it to private to only permit ClockTimer to access it (shortended example): 如果要在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;
    } 
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM