C#获取动态代码的值
生活随笔
收集整理的這篇文章主要介紹了
C#获取动态代码的值
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
通過字符串對控件屬性賦值 例如:"BackColor=Color.FromArgb(100,100,100);BackGroundp_w_picpath=Image.FromFile(\"XXX\")" 其中最關鍵的部分算是獲取動態代碼的值.以下為獲取動態代碼值的類,不足之處還請指正. ------------------------------------------------ 1 usingMicrosoft.CSharp;
2 usingSystem.Reflection;
3 ?using System.CodeDom.Compiler;
4
5 public static class CSharpCodeValueHelper<T>
6 {
7 /// <summary>
8 /// 編譯器實例
9 /// </summary>
10 private static CSharpCodeProvider CSharpCodeProvider { get; set; }
11
12 /// <summary>
13 /// 編譯器參數
14 /// </summary>
15 private static CompilerParameters CompilerParameters { get; set; }
16
17 /// <summary>
18 /// 構造函數
19 /// </summary>
20 static CSharpCodeValueHelper()
21 {
22 CSharpCodeProvider = new CSharpCodeProvider();
23
24 CompilerParameters = new CompilerParameters();
25 CompilerParameters.ReferencedAssemblies.Add("System.dll");
26 CompilerParameters.ReferencedAssemblies.Add("System.Data.dll");
27 CompilerParameters.ReferencedAssemblies.Add("System.Deployment.dll");
28 CompilerParameters.ReferencedAssemblies.Add("System.Drawing.dll");
29 CompilerParameters.ReferencedAssemblies.Add("System.Windows.Forms.dll");
30 CompilerParameters.ReferencedAssemblies.Add("System.Xml.dll");
31 CompilerParameters.GenerateExecutable = false;
32 CompilerParameters.GenerateInMemory = true;
33 }
34
35 /// <summary>
36 /// 獲取代碼的值
37 /// </summary>
38 /// <param name="CSharpCode">CSharpCode代碼</param>
39 /// <returns>動態CSharpCode代碼的值</returns>
40 public static T GetCodeValue(string CSharpCode)
41 {
42 CompilerResults compilerResults = CSharpCodeProvider.CompileAssemblyFromSource(CompilerParameters, CreateCode(CSharpCode));
43
44 if (compilerResults.Errors.HasErrors)
45 {
46 string errorString = string.Empty;
47 errorString += "編譯錯誤:\n";
48 foreach (CompilerError err in compilerResults.Errors)
49 {
50 errorString += err.ErrorText + "\n";
51 }
52 throw new Exception(errorString);
53 }
54 else
55 {
56 // 通過反射,調用動態類DynamicClass實例
57 Assembly assembly = compilerResults.CompiledAssembly;
58 object dynamicClass = assembly.CreateInstance("DynamicNamespace.DynamicClass");
59 MethodInfo methodInfo = dynamicClass.GetType().GetMethod("GetValue");
60
61 return (T)methodInfo.Invoke(dynamicClass, null);
62 }
63 }
64
65 /// <summary>
66 /// 創建代碼
67 /// </summary>
68 /// <param name="CSharpCode">CSharpCode代碼</param>
69 /// <returns>創建完成的代碼</returns>
70 private static string CreateCode(string CSharpCode)
71 {
72 StringBuilder strBuilder = new StringBuilder();
73 strBuilder.AppendLine("using System;");
74 strBuilder.AppendLine("using System.Collections.Generic;");
75 strBuilder.AppendLine("using System.ComponentModel;");
76 strBuilder.AppendLine("using System.Data;");
77 strBuilder.AppendLine("using System.Drawing;");
78 strBuilder.AppendLine("using System.Text;");
79 strBuilder.AppendLine("using System.Windows.Forms;");
80
81 strBuilder.AppendLine("namespace DynamicNamespace");
82 strBuilder.AppendLine("{");
83 strBuilder.AppendLine(" public class DynamicClass");
84 strBuilder.AppendLine(" {");
85 strBuilder.AppendLine(" public object GetValue()");
86 strBuilder.AppendLine(" {");
87 strBuilder.AppendLine(" return " + CSharpCode + ";");
88 strBuilder.AppendLine(" }");
89 strBuilder.AppendLine(" }");
90 strBuilder.AppendLine("}");
91
92 string code = strBuilder.ToString();
93 return code;
94 }
95 }
96
97
98
99
100 下面是調用(注意調用時特殊字符的轉義)
1 Color coor = CSharpCodeValueHelper<Color>.GetCodeValue("Color.FromArgb(100,100,100)");
2
3
4 Image img = CSharpCodeValueHelper<Image>.GetCodeValue("Image.FromFile(\"C:\\\\tu.jpg\")");
2 usingSystem.Reflection;
3 ?using System.CodeDom.Compiler;
4
5 public static class CSharpCodeValueHelper<T>
6 {
7 /// <summary>
8 /// 編譯器實例
9 /// </summary>
10 private static CSharpCodeProvider CSharpCodeProvider { get; set; }
11
12 /// <summary>
13 /// 編譯器參數
14 /// </summary>
15 private static CompilerParameters CompilerParameters { get; set; }
16
17 /// <summary>
18 /// 構造函數
19 /// </summary>
20 static CSharpCodeValueHelper()
21 {
22 CSharpCodeProvider = new CSharpCodeProvider();
23
24 CompilerParameters = new CompilerParameters();
25 CompilerParameters.ReferencedAssemblies.Add("System.dll");
26 CompilerParameters.ReferencedAssemblies.Add("System.Data.dll");
27 CompilerParameters.ReferencedAssemblies.Add("System.Deployment.dll");
28 CompilerParameters.ReferencedAssemblies.Add("System.Drawing.dll");
29 CompilerParameters.ReferencedAssemblies.Add("System.Windows.Forms.dll");
30 CompilerParameters.ReferencedAssemblies.Add("System.Xml.dll");
31 CompilerParameters.GenerateExecutable = false;
32 CompilerParameters.GenerateInMemory = true;
33 }
34
35 /// <summary>
36 /// 獲取代碼的值
37 /// </summary>
38 /// <param name="CSharpCode">CSharpCode代碼</param>
39 /// <returns>動態CSharpCode代碼的值</returns>
40 public static T GetCodeValue(string CSharpCode)
41 {
42 CompilerResults compilerResults = CSharpCodeProvider.CompileAssemblyFromSource(CompilerParameters, CreateCode(CSharpCode));
43
44 if (compilerResults.Errors.HasErrors)
45 {
46 string errorString = string.Empty;
47 errorString += "編譯錯誤:\n";
48 foreach (CompilerError err in compilerResults.Errors)
49 {
50 errorString += err.ErrorText + "\n";
51 }
52 throw new Exception(errorString);
53 }
54 else
55 {
56 // 通過反射,調用動態類DynamicClass實例
57 Assembly assembly = compilerResults.CompiledAssembly;
58 object dynamicClass = assembly.CreateInstance("DynamicNamespace.DynamicClass");
59 MethodInfo methodInfo = dynamicClass.GetType().GetMethod("GetValue");
60
61 return (T)methodInfo.Invoke(dynamicClass, null);
62 }
63 }
64
65 /// <summary>
66 /// 創建代碼
67 /// </summary>
68 /// <param name="CSharpCode">CSharpCode代碼</param>
69 /// <returns>創建完成的代碼</returns>
70 private static string CreateCode(string CSharpCode)
71 {
72 StringBuilder strBuilder = new StringBuilder();
73 strBuilder.AppendLine("using System;");
74 strBuilder.AppendLine("using System.Collections.Generic;");
75 strBuilder.AppendLine("using System.ComponentModel;");
76 strBuilder.AppendLine("using System.Data;");
77 strBuilder.AppendLine("using System.Drawing;");
78 strBuilder.AppendLine("using System.Text;");
79 strBuilder.AppendLine("using System.Windows.Forms;");
80
81 strBuilder.AppendLine("namespace DynamicNamespace");
82 strBuilder.AppendLine("{");
83 strBuilder.AppendLine(" public class DynamicClass");
84 strBuilder.AppendLine(" {");
85 strBuilder.AppendLine(" public object GetValue()");
86 strBuilder.AppendLine(" {");
87 strBuilder.AppendLine(" return " + CSharpCode + ";");
88 strBuilder.AppendLine(" }");
89 strBuilder.AppendLine(" }");
90 strBuilder.AppendLine("}");
91
92 string code = strBuilder.ToString();
93 return code;
94 }
95 }
96
97
98
99
100 下面是調用(注意調用時特殊字符的轉義)
1 Color coor = CSharpCodeValueHelper<Color>.GetCodeValue("Color.FromArgb(100,100,100)");
2
3
4 Image img = CSharpCodeValueHelper<Image>.GetCodeValue("Image.FromFile(\"C:\\\\tu.jpg\")");
轉載于:https://blog.51cto.com/2063557/536823
總結
以上是生活随笔為你收集整理的C#获取动态代码的值的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Windows 7使用wubi硬盘安装U
- 下一篇: 基于地图开发控件GMap.Net 使用