2016-11-11 122 views
0

这里是一些代码:如何从外部类的内部类访问属性?

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

有没有一种方法来“设置”,从一个内部类的属性在外部类中,但不允许它从外部设置?只有外部类必须能够设置内部类的属性。

+0

你能指定你的目的吗?你的意思是哪个属性?请给我一个样品 – rbr94

+1

行:m_TimerTimes.Start = DateTime.Now; – Ron

+0

您的代码非常不完整,无法复制粘贴为工作示例。并且它包含许多对于问题不必要的噪音 – grek40

回答

0

如果要从外部类访问属性,请使用public来进行要使用的操作。你的情况,你可以只读取属性get;和不写或set改变:

public DateTime? Start { get; set; } 

这也同样适用于你的嵌套类。如果您要访问TimerTimesClockTimer外面把它作为public,否则将其设置为private只允许ClockTimer访问它(shortended为例):

public class ClockTimer { 

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

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

实际的问题是不同的。再读一遍。 – Thangadurai

+0

@Thangadurai我编辑它。 – rbr94

1

您可以创建一个private内拥有公共属性/方法的类,这些属性可以被外部类访问,但不能被外部访问。如果你想让内部类的一部分公开,可以从某种公共接口(根据你的需要可以是interface,classabstract 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; } } 
} 

正如你所看到的,我减少了一些例子,但所有必要的代码应该在里面。

+0

为什么'抽象'? – rbr94

+0

@ rbr94因为它是'Start',所以如果没有一个实际设置它的值的实现是没有意义的。 “抽象”并不是绝对必要的,它只是显示了预期的用法。 – grek40

+0

@ grek40它也可能是一个接口 – zerkms