通过字符串对控件属性赋值
例如:"BackColor=Color.FromArgb(100,100,100);BackGroundimage=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\")");
|
请发表评论