Browse Source

添加动态执行代码功能

章政 5 years ago
parent
commit
d98bf2b4e9

+ 69 - 2
UAS-出货标签管理(贸易版)/PublicMethod/BaseUtil.cs

@@ -9,6 +9,9 @@ using UAS_LabelMachine.CustomControl.GroupBoxWithBorder;
 using static System.Windows.Forms.Control;
 using System.Text.RegularExpressions;
 using Seagull.BarTender.Print;
+using Microsoft.CSharp;
+using System.CodeDom.Compiler;
+using System.Reflection;
 
 namespace UAS_LabelMachine
 {
@@ -879,13 +882,77 @@ namespace UAS_LabelMachine
             {
                 GroupByCondition += "pd_ordercode,";
             }
-            if (iCustProdCode || iCustPo || iDC || iLotNo|| iOrderCode)
+            if (iCustProdCode || iCustPo || iDC || iLotNo || iOrderCode)
             {
                 GroupByCondition = " group by " + (GroupByCondition.Substring(0, GroupByCondition.Length - 1));
             }
             sql.Append("max(pib_id),");
-            Console.WriteLine("select " + sql.ToString().Substring(0, sql.Length - 1) + " from prodiobarcode where pib_inoutno = '" + pi_inoutno + "' and pib_outboxcode2 = " + (pib_outboxcode2 == "" ? "0" : pib_outboxcode2) + GroupByCondition);
             return "select " + sql.ToString().Substring(0, sql.Length - 1) + " from prodiobarcode where pib_inoutno = '" + pi_inoutno + "' and pib_outboxcode2 = " + (pib_outboxcode2 == "" ? "0" : pib_outboxcode2) + GroupByCondition;
         }
+
+        public static void CustomerInit()
+        {
+            //创建编译环境实例
+            CSharpCodeProvider objCSharpCodePrivoder = new CSharpCodeProvider();
+            //UAS_LabelMachine
+            CompilerParameters objCompilerParameters = new CompilerParameters();
+            objCompilerParameters.ReferencedAssemblies.Add("System.dll");
+            objCompilerParameters.ReferencedAssemblies.Add("System.Windows.Forms.dll");
+            objCompilerParameters.GenerateExecutable = false;
+            objCompilerParameters.GenerateInMemory = true;
+
+            // 4.CompilerResults
+            CompilerResults cr = objCSharpCodePrivoder.CompileAssemblyFromSource(objCompilerParameters, GenerateCode());
+
+            if (cr.Errors.HasErrors)
+            {
+                Console.WriteLine("编译错误:");
+                foreach (CompilerError err in cr.Errors)
+                {
+                    Console.WriteLine(err.ErrorText);
+                }
+            }
+            else
+            {
+                // 通过反射,调用HelloWorld的实例
+                Assembly objAssembly = cr.CompiledAssembly;
+                object objHelloWorld = objAssembly.CreateInstance("Test.TestClass");
+                MethodInfo objMI = objHelloWorld.GetType().GetMethod("Test");
+                Console.WriteLine(objMI.Invoke(objHelloWorld, null));
+            }
+        }
+
+        static string GenerateCode()
+        {
+            StringBuilder sb = new StringBuilder();
+            sb.Append(Environment.NewLine);
+            sb.Append("using System; ");
+            sb.Append(Environment.NewLine);
+            sb.Append("using System.Windows.Forms; ");
+            sb.Append(Environment.NewLine);
+            sb.Append("namespace Test ");
+            sb.Append(Environment.NewLine);
+            sb.Append("{ ");
+            sb.Append("   public class TestClass");
+            sb.Append(Environment.NewLine);
+            sb.Append("  {");
+            sb.Append(Environment.NewLine);
+            sb.Append("      public void Test()  ");
+            sb.Append(Environment.NewLine);
+            sb.Append("     {");
+            sb.Append(Environment.NewLine);
+            sb.Append("          Form f=Form.ActiveForm; if(f.Controls[\"cu_code\"].Text==\"C000027\"){");
+            sb.Append(Environment.NewLine);
+            sb.Append("          (f.Controls[\"OutBoxLabel\"].Controls[\"DiffCustOutBoxCode\"] as CheckBox).Checked = false;}else {(f.Controls[\"OutBoxLabel\"].Controls[\"DiffCustOutBoxCode\"] as CheckBox).Checked = true;}");
+            sb.Append(Environment.NewLine);
+            sb.Append("     }");
+            sb.Append(Environment.NewLine);
+            sb.Append("  }");
+            sb.Append(Environment.NewLine);
+            sb.Append("}");
+            string code = sb.ToString();
+            Console.WriteLine(code);
+            return sb.ToString();
+        }
     }
 }

+ 5 - 0
UAS-出货标签管理(贸易版)/UAS-出货标签管理(贸易版).csproj

@@ -100,6 +100,10 @@
     <Reference Include="Analysis">
       <HintPath>tool\Analysis.dll</HintPath>
     </Reference>
+    <Reference Include="AssembDll, Version=0.0.0.0, Culture=neutral, processorArchitecture=MSIL">
+      <SpecificVersion>False</SpecificVersion>
+      <HintPath>tool\AssembDll.dll</HintPath>
+    </Reference>
     <Reference Include="ICSharpCode.SharpZipLib">
       <HintPath>tool\ICSharpCode.SharpZipLib.dll</HintPath>
     </Reference>
@@ -810,6 +814,7 @@
     </None>
     <Content Include="SQL\SQL.txt" />
     <Content Include="tool\Analysis.dll" />
+    <Content Include="tool\AssembDll.dll" />
     <Content Include="tool\DataHelper.dll" />
     <Content Include="tool\NPOI.dll" />
     <Content Include="tool\NPOI.OOXML.dll" />

+ 1 - 0
UAS-出货标签管理(贸易版)/UAS_出货标签管理.cs

@@ -1223,6 +1223,7 @@ namespace UAS_LabelMachine
                     stw = new SetLoadingWindow(thread, "正在获取打印标签");
                     BaseUtil.SetFormCenter(stw);
                     stw.ShowDialog();
+                    //BaseUtil.CustomerInit();
                     //重置采集项次
                     LogManager.DoLog("输入单号【" + pi_inoutno.Text + "】");
                 }