具有嵌套类的C#Singleton函数和结构

时间:2017-03-02 10:03:46

标签: c# function class nested singleton

我尝试做的单身懒惰比我想象的要困难。 但我想我已经做到了50%。我的问题是关于下一个代码,为什么我需要通过Instanciation _I来到达' int函数'而不是通过_I来到达结构? 单身人士是正确实施还是没有理解? 感谢

class Program
{
    static void Main(string[] args)
    {
        Console.WriteLine(Example_Singleton.Singleton.Data.Act_Number + "\n");
       Console.WriteLine(Example_Singleton.Singleton.Time.TLength_Form.Time50ms.ToString()+"\n");
       Example_Singleton.Singleton.Time._IT.waitTimer(3000);  
       Console.WriteLine("done");
       Console.ReadKey();   
    }
}
   class Example_Singleton
{
    public sealed class Singleton
    {
        private Singleton()
        {
        }//ctor singleton
        public static Singleton _I { get { return Nested.instance; } }//instance called for singleton
        private class Nested
        {
            // Explicit static constructor to tell C# compiler
            // not to mark type as beforefieldinit
            static Nested() { }//ctor nested
            internal static readonly Singleton instance = new Singleton();
        }
        public struct Data
        {//base structure for data station
            public static Int16 Act_Number = 352;
        }
        public sealed class Time/* Class Time for PSX*/
        {
            private Time()
            {
            }//ctor
            public static Time _IT { get { return thisTime.instance; } }
            private class thisTime
            {
                static thisTime() { }//ctor
                internal static readonly Time instance = new Time();
            }
            internal void waitTimer(Int32 TimeLength) //Wait timer
            {
                System.Threading.Thread.Sleep(TimeLength);
            }
            internal struct User_Time
            {
                public const int RefreshData = 100;
                public const int WaitSynchro = 10;
            }
            internal struct TLength_System    //time length definitions for system time
            {
                public const double Time10ms = 10;
                public const double Time50ms = 50;
            }
            internal struct TLength_Form      //time length definitions for Form time
            {
                public const int Time10ms = 10;
                public const int Time50ms = 50;
            }
        }
    } 

1 个答案:

答案 0 :(得分:1)

如果您将Time课程更改为以下

    public sealed class Time/* Class Time for PSX*/
    {
        private Time()
        {
        }//ctor
        private static object _oLock = new object();
        private static Time _it;
        public static Time _IT 
        { 
            get 
            { 
                if(_it == null)
                    lock(_oLock)
                        if (_it == null)
                            _it = new Time();

                return _it;
            }
        }
        internal void waitTimer(Int32 TimeLength) //Wait timer
        {
            System.Threading.Thread.Sleep(TimeLength);
        }
        internal struct User_Time
        {
            public const int RefreshData = 100;
            public const int WaitSynchro = 10;
        }
        internal struct TLength_System    //time length definitions for system time
        {
            public const double Time10ms = 10;
            public const double Time50ms = 50;
        }
        internal struct TLength_Form      //time length definitions for Form time
        {
            public const int Time10ms = 10;
            public const int Time50ms = 50;
        }
    }

你只需要像这样使用单身

Time._IT.waitTimer(3000);

注意锁定对象允许你的单例是线程安全的。