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

日韩久久久精品,亚洲精品久久久久久久久久久,亚洲欧美一区二区三区国产精品 ,一区二区福利

ILGenerator.Emit動態 MSIL編程(二)之基礎

系統 1871 0
      
            public
      
      
        sealed
      
      
        class
      
      
         ColorToArgb

    {

        
      
      
        ///
      
      
        <summary>
      
      
        ///
      
      
         將十六進制轉化為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);
        
          //
        
        
          創建程序集
        
        
          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
        
        ));
        
          //
        
        
          注冊變量 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是加載方法參數的意思。這里arg_0事實上是對當前對象的引用即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
        
        
          "
        
        );
        
          //
        
        
          方便反編譯 看代碼寫的對不對
        
        
          #endregion
        
        
          

        }

    }
        
      
    

?

      
        ?
      
    

ILGenerator.Emit動態 MSIL編程(二)之基礎


更多文章、技術交流、商務合作、聯系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯系: 360901061

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

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦!!!

發表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 邵阳县| 苍梧县| 乌审旗| 聂拉木县| 宁都县| 兰考县| 九龙县| 彭山县| 当涂县| 商都县| 南澳县| 井陉县| 阜康市| 余干县| 乌兰察布市| 静安区| 类乌齐县| 嵊泗县| 峡江县| 民勤县| 奈曼旗| 清河县| 保靖县| 山丹县| 大同市| 昌图县| 嘉荫县| 南汇区| 牙克石市| 漳平市| 白城市| 石景山区| 乌审旗| 日土县| 获嘉县| 平顺县| 岳池县| 北安市| 广宁县| 定州市| 黑水县|