<output id="qn6qe"></output>

    1. <output id="qn6qe"><tt id="qn6qe"></tt></output>
    2. <strike id="qn6qe"></strike>

      亚洲 日本 欧洲 欧美 视频,日韩中文字幕有码av,一本一道av中文字幕无码,国产线播放免费人成视频播放,人妻少妇偷人无码视频,日夜啪啪一区二区三区,国产尤物精品自在拍视频首页,久热这里只有精品12
      happyhippy

      這個世界的問題在于聰明人充滿疑惑,而傻子們堅信不疑。--羅素

      1. 禁止在一個類的靜態構造函數中訪問另一個類中的靜態成員。
         猜下下面這段程序的輸出結果:

      using System;
      using System.Threading;

      namespace TypeConstructorLock
      {
          
      class Class1
          
      {
              [STAThread]
              
      public static void Main(string[] args)
              
      {
                  Thread threadA 
      = new Thread(new ThreadStart(TouchStatic1));
                  threadA.Name 
      = "Thread A";
            
                  Thread threadB 
      = new Thread(new ThreadStart(TouchStatic2));
                  threadB.Name 
      = "Thread B";
       
                  threadA.Start();
                  threadB.Start();
                  threadA.Join();
                  threadB.Join();
              }

       
              
      static void TouchStatic1() string s = Static1.Message; }
              
      static void TouchStatic2() string s = Static2.Message; }
          }


          
      class Static1
          
      {
              
      static Static1()
              
      {
                  Console.WriteLine(
      "Begin Static1 .cctor on thread {0}"
                      Thread.CurrentThread.Name);
                  Thread.Sleep(
      5000);
                  Console.WriteLine(
      "//Static1 has a message from Static2: {0}"
                      Static2.Message);      
                  message 
      = "Hello From Static1";
                  Console.WriteLine(
      "Exit Static1 .cctor on thread {0}"
                      Thread.CurrentThread.Name);
              }

       
              
      static public string Message get return message; } }
              
      static string message = "blank";
          }

       
          
      class Static2
          
      {
              
      static Static2()
              
      {
                  Console.WriteLine(
      "Begin Static2 .cctor on thread {0}"
                      Thread.CurrentThread.Name);
                  Thread.Sleep(
      5000);
                  Console.WriteLine(
      "//Static2 has a message from Static1: {0}"
                      Static1.Message);         
                  message 
      = "Hello From Static2";
                  Console.WriteLine(
      "Exit Static2 .cctor on thread {0}"
                      Thread.CurrentThread.Name);
              }

       
              
      static public string Message get return message; } }
              
      static string message = "blank";    
          }

      }

         執行結果:

      E:CSC>staticlock.exe
      Begin Static1 .cctor on thread Thread A
      Begin Static2 .cctor on thread Thread B
      //Static2 has a message from Static1: blank
      Exit Static2 .cctor on thread Thread B
      //Static1 has a message from Static2: Hello From Static2
      Exit Static1 .cctor on thread Thread A



          留意上面的“//static2 has a message from static1:black”,里面取得的字符串值是“blank”,而不是“Hello From Static1”,我們不能確定兩個靜態構造函數中的兩條message賦值語句誰已經執行或沒有執行! 這里要記住一條規則:禁止在一個類的靜態構造函數中訪問另一個類中的靜態成員(Avoid touching the static members of another type from within a type constructor)。雖然我們很少情況下會寫出上面這種代碼,但一旦寫出來了,這種錯誤將很難調試,我們要牢記這樣準則。

       

      2. 如果靜態構造函數中有異常(用E描述)拋出,則CLR會對E進行包裝,重新拋出一個System.TypeInitializationException異常,而把E作為該異常的InnerException。在進行程序設計時,我們一定要保證在類的靜態構造函數不能有異常拋出!


      3. C#中不支持子類繼承父類的靜態成員,但卻可以通過子類類名來訪問父類的靜態成員!
         反射靜態成員的示例:

      using System;
      using System.Reflection;
      namespace Reflection
      {
          
      class Class1
          
      {
              [STAThread]
              
      static void Main(string[] args)
              
      {           
                  Type type;              
                  
      object result;
                  PropertyInfo[] properties;
                  
                  
      object instance = null;
                  
      object[] index = null;
                  
                  type 
      = typeof(Static1);
                  
                  properties 
      = type.GetProperties(BindingFlags.Static | BindingFlags.Public);            
                  result 
      = properties[0].GetValue(instance, index);//注意:訪問內的靜態成員,這里傳入的是null
                  Console.WriteLine(result.ToString());
                  
                  type 
      = typeof(Static2);
                  
                  properties 
      = type.GetProperties(BindingFlags.Static | BindingFlags.Public |
                       BindingFlags.FlattenHierarchy);
      //如果不運用FlattenHierarchy,則返回空數組。
              /*FlattenHierarchy:指定應返回層次結構上的公共靜態成員和受保護的靜態成員。
               * 不返回繼承類中的私有靜態成員。靜態成員包括字段、方法、事件和屬性。不返回嵌套類型。
               *
               * FlattenHierarchy告訴CLR將基類Static1的靜態屬性也算在內!
               * C#中不支持子類繼承父類的靜態成員,但卻可以通過子類類名來訪問父類的靜態成員!
               * e.g.:我們可以通過訪問Static2.Message來訪問Static1.Message
               *  
      */

                  result 
      = properties[0].GetValue(instance, index);
                  Console.WriteLine(result.ToString());

              Console.WriteLine(Static2.Message);
              }

          }

                  
          
      class Static1
          
      {
              
      static public string Message get return message; } }
              
      static string message = "Hello World 1";
          }

                  
          
      class Static2 : Static1{}
      }

         執行結果:

      E:CSC>refstatic.exe
      Hello World 
      1
      Hello World 
      1 

       

       

      參考:http://msdn.microsoft.com/msdnmag/issues/05/01/StaticsinNET/default.aspx


      posted on 2007-04-05 18:58  Silent Void  閱讀(1060)  評論(0)    收藏  舉報

      主站蜘蛛池模板: 国产成人啪精品午夜网站| 欧美国产日产一区二区| 国产天美传媒性色av高清| 久久精品蜜芽亚洲国产AV| 久久香蕉国产线看观看怡红院妓院 | 中文字幕av无码一区二区三区 | 中文字幕国产精品一区二| 国产精品中文字幕在线看| 国内精品伊人久久久久av| 99久久婷婷国产综合精品青草漫画 | 开心激情站开心激情网六月婷婷| 2020国产欧洲精品网站| 国产卡一卡二卡三免费入口| 免费人成再在线观看视频| 亚洲区欧美区综合区自拍区| 伊吾县| 国偷自产一区二区三区在线视频| 小嫩批日出水无码视频免费| 日韩AV无码精品一二三区| 综合在线 亚洲 成人 欧美| 人妻精品人妻无码一区二区三区| 亚洲综合精品一区二区三区| 乱人伦中文字幕成人网站在线| 国产精成人品日日拍夜夜| 亚洲av综合色区在线观看| 亚洲国产精品综合久久网络| 精品一二三四区在线观看| 热久久99精品这里有精品| 亚洲区综合中文字幕日日| 色欲av亚洲一区无码少妇| 国产老肥熟一区二区三区| 午夜成人无码免费看网站| 一区二区三区国产不卡| 18禁无遮挡啪啪无码网站| 吉川爱美一区二区三区视频| 国产精品免费久久久免费| 看全色黄大色黄大片 视频| 国产一区二区三区导航| 97欧美精品系列一区二区| 亚洲av一本二本三本| 中文字幕在线视频不卡一区二区|