///將十六進(jìn)制轉(zhuǎn)化為AGRB/////////publicstaticColorHexadecimalToArgb(stringHexadecimal){stringtext=string.Empty;intnum=1;byteb=255;intnum" />

亚洲免费在线-亚洲免费在线播放-亚洲免费在线观看-亚洲免费在线观看视频-亚洲免费在线看-亚洲免费在线视频

ILGenerator.Emit動(dòng)態(tài) MSIL編程(二)之基礎(chǔ)

系統(tǒng) 1634 0
      
            public
      
      
        sealed
      
      
        class
      
      
         ColorToArgb

    {

        
      
      
        ///
      
      
        <summary>
      
      
        ///
      
      
         將十六進(jìn)制轉(zhuǎn)化為AGRB

        
      
      
        ///
      
      
        </summary>
      
      
        ///
      
      
        <param name="Hexadecimal"></param>
      
      
        ///
      
      
        <returns></returns>
      
      
        public
      
      
        static
      
       Color HexadecimalToArgb(
      
        string
      
      
         Hexadecimal)

        {



            
      
      
        string
      
       text = 
      
        string
      
      
        .Empty;

            
      
      
        int
      
       num = 
      
        1
      
      
        ;

            
      
      
        byte
      
       b = 
      
        255
      
      
        ;

            
      
      
        int
      
      
         num2;

            
      
      
        switch
      
      
         (Hexadecimal.Length)

            {

                
      
      
        case
      
      
        4
      
      
        :

                    num2 
      
      = 
      
        1
      
      
        ;

                    text 
      
      = 
      
        "
      
      
        F
      
      
        "
      
      
        ;

                    
      
      
        goto
      
      
         IL_9D;

                
      
      
        case
      
      
        5
      
      
        :

                    num2 
      
      = 
      
        1
      
      
        ;

                    text 
      
      = 
      
        "
      
      
        F
      
      
        "
      
      
        ;

                    b 
      
      = Convert.ToByte(Hexadecimal.Substring(num, num2) + text, 
      
        16
      
      
        );

                    num 
      
      +=
      
         num2;

                    
      
      
        goto
      
      
         IL_9D;

                
      
      
        case
      
      
        7
      
      
        :

                    num2 
      
      = 
      
        2
      
      
        ;

                    
      
      
        goto
      
      
         IL_9D;

                
      
      
        case
      
      
        9
      
      
        :

                    num2 
      
      = 
      
        2
      
      
        ;

                    b 
      
      = Convert.ToByte(Hexadecimal.Substring(num, num2) + text, 
      
        16
      
      
        );

                    num 
      
      +=
      
         num2;

                    
      
      
        goto
      
      
         IL_9D;

            }

            num2 
      
      = 
      
        2
      
      
        ;

            Hexadecimal 
      
      = 
      
        "
      
      
        #FFFFFF
      
      
        "
      
      
        ;

        IL_9D:

            
      
      
        byte
      
       b2 = Convert.ToByte(Hexadecimal.Substring(num, num2) + text, 
      
        16
      
      
        );

            
      
      
        byte
      
       b3 = Convert.ToByte(Hexadecimal.Substring(num += num2, num2) + text, 
      
        16
      
      
        );

            
      
      
        byte
      
       b4 = Convert.ToByte(Hexadecimal.Substring(num + num2, num2) + text, 
      
        16
      
      
        );

            
      
      
        return
      
      
         Color.FromArgb(b, b2, b3, b4);

        }

    }
      
    
    
        
          class
        
        
           Program

    {

        
        
        
          static
        
        
          void
        
         Main(
        
          string
        
        
          [] args)

        {

            
        
        
          #region
        
        
          var
        
         asmName = 
        
          new
        
         AssemblyName(
        
          "
        
        
          Test
        
        
          "
        
        
          );

            
        
        
          var
        
         asmBuilder = AppDomain.CurrentDomain.DefineDynamicAssembly(asmName, AssemblyBuilderAccess.RunAndSave);
        
          //
        
        
          創(chuàng)建程序集
        
        
          var
        
         mdlBldr = asmBuilder.DefineDynamicModule(
        
          "
        
        
          Test
        
        
          "
        
        , 
        
          "
        
        
          Test.dll
        
        
          "
        
        );
        
          //
        
        
          定義模塊
        
        
          var
        
         typeBldr = mdlBldr.DefineType(
        
          "
        
        
          ColorToArgb
        
        
          "
        
        , TypeAttributes.Public|TypeAttributes.Class|TypeAttributes.Sealed);
        
          //
        
        
          定義類
        
        
          var
        
         methodBldr = typeBldr.DefineMethod(
        
          "
        
        
          HexadecimalToArgb
        
        
          "
        
        
          , MethodAttributes.Public,CallingConventions.Standard, 

                
        
        
          typeof
        
        (Color), 
        
          new
        
         Type[] { 
        
          typeof
        
        (
        
          string
        
        ) });
        
          //
        
        
          定義方法
        
        
          var
        
         MyILGenerator = methodBldr.GetILGenerator();
        
          //
        
        
          獲取il生成器
        
        

            MyILGenerator.DeclareLocal(
        
          typeof
        
        (
        
          string
        
        ));
        
          //
        
        
          注冊(cè)變量 string text
        
        

            MyILGenerator.DeclareLocal(
        
          typeof
        
        (
        
          int
        
        ));
        
          //
        
        
          int num ;
        
        

            MyILGenerator.DeclareLocal(
        
          typeof
        
        (
        
          byte
        
        ));
        
          //
        
        
          byte b;
        
        

            MyILGenerator.DeclareLocal(
        
          typeof
        
        (
        
          int
        
        ));
        
          //
        
        
          int num2;
        
        
          var
        
         b2 = MyILGenerator.DeclareLocal(
        
          typeof
        
        (
        
          byte
        
        
          ));

            
        
        
          var
        
         b3 = MyILGenerator.DeclareLocal(
        
          typeof
        
        (
        
          byte
        
        
          ));

            
        
        
          var
        
         b4 = MyILGenerator.DeclareLocal(
        
          typeof
        
        (
        
          byte
        
        
          ));

            
        
        
          var
        
         color = MyILGenerator.DeclareLocal(
        
          typeof
        
        
          (Color));

            

            
        
        
          #endregion
        
        
          #region
        
        
          //
        
        
          Label defaultCase = MyILGenerator.DefineLabel();
        
        

            Label endOfMethod =
        
           MyILGenerator.DefineLabel();

            Label forLabel 
        
        =
        
           MyILGenerator.DefineLabel();

            Label[] jumpTable 
        
        = 
        
          new
        
        
           Label[] { MyILGenerator.DefineLabel(),MyILGenerator.DefineLabel(),

                      MyILGenerator.DefineLabel(), MyILGenerator.DefineLabel() };

            MyILGenerator.Emit(OpCodes.Ldsfld, 
        
        
          string
        
        .Empty); 
        
          //
        
        
          壓棧賦值
        
        
                      MyILGenerator.Emit(OpCodes.Stloc_0);

            MyILGenerator.Emit(OpCodes.Ldc_I4, 
        
        
          1
        
        
          );

            MyILGenerator.Emit(OpCodes.Stloc_1);

            MyILGenerator.Emit(OpCodes.Ldc_I4, 
        
        
          0XFF
        
        
          );

            MyILGenerator.Emit(OpCodes.Stloc_2);

            MyILGenerator.Emit(OpCodes.Ldarg, 
        
        
          0
        
        );
        
          //
        
        
          Ldarg是加載方法參數(shù)的意思。這里arg_0事實(shí)上是對(duì)當(dāng)前對(duì)象的引用即this
        
        

            MyILGenerator.Emit(OpCodes.Callvirt, 
        
          typeof
        
        (
        
          string
        
        ).GetProperty(
        
          "
        
        
          Length
        
        
          "
        
        
          ).GetGetMethod());

            LocalBuilder length 
        
        = MyILGenerator.DeclareLocal(
        
          typeof
        
        (
        
          int
        
        
          ));

            MyILGenerator.Emit(OpCodes.Stloc_S, length);

            MyILGenerator.Emit(OpCodes.Ldloc_S, length); 

            MyILGenerator.Emit(OpCodes.Ldc_I4_4);

            MyILGenerator.Emit(OpCodes.Sub);

            MyILGenerator.Emit(OpCodes.Switch,jumpTable);

            
        
        
          //
        
        
          MyILGenerator.Emit(OpCodes.Br_S, defaultCase);
        
        

            MyILGenerator.MarkLabel(jumpTable[
        
          0
        
        
          ]);

            MyILGenerator.Emit(OpCodes.Ldc_I4_1);

            MyILGenerator.Emit(OpCodes.Stloc_3);

            

            MyILGenerator.Emit(OpCodes.Ldstr, 
        
        
          "
        
        
          F
        
        
          "
        
        
          );

            MyILGenerator.Emit(OpCodes.Stloc_0);

            MyILGenerator.Emit(OpCodes.Ldarg_0);

            MyILGenerator.Emit(OpCodes.Ldloc_1);

            MyILGenerator.Emit(OpCodes.Ldloc_3);

            MyILGenerator.Emit(OpCodes.Callvirt, 
        
        
          typeof
        
        (
        
          string
        
        ).GetMethod(
        
          "
        
        
          Substring
        
        
          "
        
        , 
        
          new
        
         Type[] { 
        
          typeof
        
        (Int32), 
        
          typeof
        
        
          (Int32) }));

            MyILGenerator.Emit(OpCodes.Ldloc_0);

            MyILGenerator.Emit(OpCodes.Call, 
        
        
          typeof
        
        (
        
          string
        
        ).GetMethod(
        
          "
        
        
          Concat
        
        
          "
        
        , 
        
          new
        
         Type[] { 
        
          typeof
        
        (
        
          string
        
        ), 
        
          typeof
        
        (
        
          string
        
        
          ) }));

            MyILGenerator.Emit(OpCodes.Ldc_I4_S, 
        
        
          16
        
        
          );

            MyILGenerator.Emit(OpCodes.Call, 
        
        
          typeof
        
        (Convert).GetMethod(
        
          "
        
        
          ToByte
        
        
          "
        
        , 
        
          new
        
         Type[] { 
        
          typeof
        
        (
        
          string
        
        ), 
        
          typeof
        
        
          (Int32) }));

            MyILGenerator.Emit(OpCodes.Stloc_2);

            MyILGenerator.Emit(OpCodes.Ldloc_1);

            MyILGenerator.Emit(OpCodes.Ldloc_3);

            MyILGenerator.Emit(OpCodes.Add);

            MyILGenerator.Emit(OpCodes.Stloc_1);

            
        
        
          //
        
        
          MyILGenerator.Emit(OpCodes.Br_S, endOfMethod);
        
        

            MyILGenerator.MarkLabel(jumpTable[
        
          1
        
        
          ]);

            MyILGenerator.Emit(OpCodes.Ldc_I4_2);

            MyILGenerator.Emit(OpCodes.Stloc_3);

            
        
        
          //
        
        
          MyILGenerator.Emit(OpCodes.Br_S, endOfMethod);
        
        

            MyILGenerator.MarkLabel(jumpTable[
        
          2
        
        
          ]);

            MyILGenerator.Emit(OpCodes.Ldc_I4_2);

            MyILGenerator.Emit(OpCodes.Stloc_3);

            MyILGenerator.Emit(OpCodes.Ldarg_0);

            MyILGenerator.Emit(OpCodes.Ldloc_1);

            MyILGenerator.Emit(OpCodes.Ldloc_3);

            MyILGenerator.Emit(OpCodes.Callvirt, 
        
        
          typeof
        
        (
        
          string
        
        ).GetMethod(
        
          "
        
        
          Substring
        
        
          "
        
        , 
        
          new
        
         Type[] { 
        
          typeof
        
        (Int32), 
        
          typeof
        
        
          (Int32) }));

            MyILGenerator.Emit(OpCodes.Ldloc_0);

            MyILGenerator.Emit(OpCodes.Call, 
        
        
          typeof
        
        (
        
          string
        
        ).GetMethod(
        
          "
        
        
          Concat
        
        
          "
        
        , 
        
          new
        
         Type[] { 
        
          typeof
        
        (
        
          string
        
        ), 
        
          typeof
        
        (
        
          string
        
        
          ) }));

            MyILGenerator.Emit(OpCodes.Ldc_I4_S, 
        
        
          16
        
        
          );

            MyILGenerator.Emit(OpCodes.Call, 
        
        
          typeof
        
        (Convert).GetMethod(
        
          "
        
        
          ToByte
        
        
          "
        
        , 
        
          new
        
         Type[] { 
        
          typeof
        
        (
        
          string
        
        ), 
        
          typeof
        
        
          (Int32) }));

            MyILGenerator.Emit(OpCodes.Stloc_2);

            MyILGenerator.Emit(OpCodes.Ldloc_1);

            MyILGenerator.Emit(OpCodes.Ldloc_3);

            MyILGenerator.Emit(OpCodes.Add);

            MyILGenerator.Emit(OpCodes.Stloc_1);

            
        
        
          //
        
        
          MyILGenerator.Emit(OpCodes.Br_S, endOfMethod);
        
        

            MyILGenerator.MarkLabel(jumpTable[
        
          3
        
        
          ]);

            MyILGenerator.Emit(OpCodes.Ldc_I4_2);

            MyILGenerator.Emit(OpCodes.Stloc_3);

            MyILGenerator.Emit(OpCodes.Ldstr, 
        
        
          "
        
        
          #FFFFFF
        
        
          "
        
        
          );

            MyILGenerator.Emit(OpCodes.Starg_S);
        
        
          //
        
        
          , "Hexadecimal");
        
        
                      MyILGenerator.Emit(OpCodes.Ldarg_0);

            MyILGenerator.Emit(OpCodes.Ldloc_1);

            MyILGenerator.Emit(OpCodes.Ldloc_3);

            MyILGenerator.Emit(OpCodes.Callvirt, 
        
        
          typeof
        
        (
        
          string
        
        ).GetMethod(
        
          "
        
        
          Substring
        
        
          "
        
        , 
        
          new
        
         Type[] { 
        
          typeof
        
        (Int32), 
        
          typeof
        
        
          (Int32) }));



            MyILGenerator.Emit(OpCodes.Ldloc_0);

            MyILGenerator.Emit(OpCodes.Call, 
        
        
          typeof
        
        (
        
          string
        
        ).GetMethod(
        
          "
        
        
          Concat
        
        
          "
        
        , 
        
          new
        
         Type[] { 
        
          typeof
        
        (
        
          string
        
        ), 
        
          typeof
        
        (
        
          string
        
        
          ) }));

            MyILGenerator.Emit(OpCodes.Ldc_I4_S, 
        
        
          16
        
        
          );

            MyILGenerator.Emit(OpCodes.Call, 
        
        
          typeof
        
        (Convert).GetMethod(
        
          "
        
        
          ToByte
        
        
          "
        
        , 
        
          new
        
         Type[] { 
        
          typeof
        
        (
        
          string
        
        ), 
        
          typeof
        
        
          (Int32) }));

            MyILGenerator.Emit(OpCodes.Stloc_S, b2);

            MyILGenerator.Emit(OpCodes.Ldarg_0);

            MyILGenerator.Emit(OpCodes.Ldloc_1);

            MyILGenerator.Emit(OpCodes.Ldloc_3);

            MyILGenerator.Emit(OpCodes.Add);

            MyILGenerator.Emit(OpCodes.Dup);

            MyILGenerator.Emit(OpCodes.Stloc_1);

            MyILGenerator.Emit(OpCodes.Ldloc_3);

            MyILGenerator.Emit(OpCodes.Callvirt, 
        
        
          typeof
        
        (
        
          string
        
        ).GetMethod(
        
          "
        
        
          Substring
        
        
          "
        
        , 
        
          new
        
         Type[] { 
        
          typeof
        
        (Int32), 
        
          typeof
        
        
          (Int32) }));

            MyILGenerator.Emit(OpCodes.Ldloc_0);

            MyILGenerator.Emit(OpCodes.Call, 
        
        
          typeof
        
        (
        
          string
        
        ).GetMethod(
        
          "
        
        
          Concat
        
        
          "
        
        , 
        
          new
        
         Type[] { 
        
          typeof
        
        (
        
          string
        
        ), 
        
          typeof
        
        (
        
          string
        
        
          ) }));

            MyILGenerator.Emit(OpCodes.Ldc_I4_S, 
        
        
          16
        
        
          );

            MyILGenerator.Emit(OpCodes.Call, 
        
        
          typeof
        
        (Convert).GetMethod(
        
          "
        
        
          ToByte
        
        
          "
        
        , 
        
          new
        
         Type[] { 
        
          typeof
        
        (
        
          string
        
        ), 
        
          typeof
        
        
          (Int32) }));

            MyILGenerator.Emit(OpCodes.Stloc_S, b3);

            MyILGenerator.Emit(OpCodes.Ldarg_0);

            MyILGenerator.Emit(OpCodes.Ldloc_1);

            MyILGenerator.Emit(OpCodes.Ldloc_3);

            MyILGenerator.Emit(OpCodes.Add);

            MyILGenerator.Emit(OpCodes.Ldloc_3);

            MyILGenerator.Emit(OpCodes.Callvirt, 
        
        
          typeof
        
        (
        
          string
        
        ).GetMethod(
        
          "
        
        
          Substring
        
        
          "
        
        , 
        
          new
        
         Type[] { 
        
          typeof
        
        (Int32), 
        
          typeof
        
        
          (Int32) }));

            MyILGenerator.Emit(OpCodes.Ldloc_0);

            MyILGenerator.Emit(OpCodes.Call, 
        
        
          typeof
        
        (
        
          string
        
        ).GetMethod(
        
          "
        
        
          Concat
        
        
          "
        
        , 
        
          new
        
         Type[] { 
        
          typeof
        
        (
        
          string
        
        ), 
        
          typeof
        
        (
        
          string
        
        
          ) }));

            MyILGenerator.Emit(OpCodes.Ldc_I4_S, 
        
        
          16
        
        
          );

            MyILGenerator.Emit(OpCodes.Call, 
        
        
          typeof
        
        (Convert).GetMethod(
        
          "
        
        
          ToByte
        
        
          "
        
        , 
        
          new
        
         Type[] { 
        
          typeof
        
        (
        
          string
        
        ), 
        
          typeof
        
        
          (Int32) }));

            MyILGenerator.Emit(OpCodes.Stloc_S, b4);

            MyILGenerator.Emit(OpCodes.Ldloc_2);

            MyILGenerator.Emit(OpCodes.Stloc_S, b2);

            MyILGenerator.Emit(OpCodes.Stloc_S, b3);

            MyILGenerator.Emit(OpCodes.Stloc_S, b4);

            MyILGenerator.Emit(OpCodes.Call, 
        
        
          typeof
        
        (Color).GetMethod(
        
          "
        
        
          FromArgb
        
        
          "
        
        , 
        
          new
        
         Type[] { 
        
          typeof
        
        (Int32), 
        
          typeof
        
        (Int32), 
        
          typeof
        
        (Int32), 
        
          typeof
        
        
          (Int32) }));

            MyILGenerator.Emit(OpCodes.Stloc_S, color);

            MyILGenerator.Emit(OpCodes.Br_S, forLabel);

            MyILGenerator.MarkLabel(forLabel);

            MyILGenerator.Emit(OpCodes.Ldloc_S, color);

            MyILGenerator.Emit(OpCodes.Ret);

            typeBldr.CreateType();

            asmBuilder.Save(
        
        
          "
        
        
          Test.dll
        
        
          "
        
        );
        
          //
        
        
          方便反編譯 看代碼寫的對(duì)不對(duì)
        
        
          #endregion
        
        
          

        }

    }
        
      
    

?

      
        ?
      
    

ILGenerator.Emit動(dòng)態(tài) MSIL編程(二)之基礎(chǔ)


更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號(hào)聯(lián)系: 360901061

您的支持是博主寫作最大的動(dòng)力,如果您喜歡我的文章,感覺(jué)我的文章對(duì)您有幫助,請(qǐng)用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長(zhǎng)非常感激您!手機(jī)微信長(zhǎng)按不能支付解決辦法:請(qǐng)將微信支付二維碼保存到相冊(cè),切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對(duì)您有幫助就好】

您的支持是博主寫作最大的動(dòng)力,如果您喜歡我的文章,感覺(jué)我的文章對(duì)您有幫助,請(qǐng)用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長(zhǎng)會(huì)非常 感謝您的哦?。?!

發(fā)表我的評(píng)論
最新評(píng)論 總共0條評(píng)論
主站蜘蛛池模板: 久久频精品99香蕉国产 | 亚洲国产成人麻豆精品 | 久久久久国产一级毛片高清版 | 咪咪爱在线观看 | 97在线观看播放 | 成年午夜性视频免费播放 | 免费人成在线观看网站 | 曰本人一级毛片免费完整视频 | 亚洲韩精品欧美一区二区三区 | 精品一区二区三区在线播放 | 天堂成人在线 | 久久精品国产一区二区三区 | 另类亚洲图片 | 四虎影院国产 | 亚洲在线久久 | 很很操很很日 | 麻豆伦理| 亚洲国产一成人久久精品 | 91精品视频在线播放 | 国产成人一区二区三区在线播放 | 四虎看片 | 日本精品久久久久久久 | 狠狠躁夜夜躁人人爽天天不 | 偷偷狠狠的日日高清完整视频 | 在线亚洲日产一区二区 | 欧美成成人免费 | 波多野给衣一区二区三区 | 成人在线免费网站 | 在线观看av片永久免费 | 天天操天天射天天插 | h视频在线观看网站 | 国产香蕉在线精彩视频 | 操日本妞| 久草香蕉视频在线观看 | 色偷偷777| 在线观看不卡视频 | 久久99中文字幕 | 欧美综合精品一区二区三区 | 五月桃花网婷婷亚洲综合 | 在线播放heyzo北条麻妃 | 97精品久久天干天天蜜 |