WinForm
using System.CodeDom;
using System.CodeDom.Compiler;
using Microsoft.CSharp;
using System.Reflection;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public class Class1
{
public static void GetValue(TextBox textBox1)
{
string codeSnippet = "using System; " + "/r/n" +
"using System.Windows.Forms; " + "/r/n" +
"using System.Drawing; " + "/r/n" +
"namespace DC{" + "/r/n" +
"public class Eval" + "/r/n" +
"{" + "/r/n" +
"public Eval(){} " + "/r/n" +
"public void GetValue(TextBox textBox1)" + "/r/n" +
"{" + "/r/n" +
"textBox1.BackColor=Color.Red;" + "/r/n" +
"}" + "/r/n" +
"}" + "/r/n" +
"}";
CodeSnippetCompileUnit unit = new CodeSnippetCompileUnit(codeSnippet);
ICodeCompiler compiler = new CSharpCodeProvider().CreateCompiler();
CompilerParameters para = new CompilerParameters();
para.ReferencedAssemblies.Add("System.dll");
para.ReferencedAssemblies.Add("System.Drawing.dll");
para.ReferencedAssemblies.Add("System.Windows.Forms.dll");
para.GenerateInMemory = true;
para.GenerateExecutable = false;
para.OutputAssembly = "Eval.dll";
CompilerResults cr = compiler.CompileAssemblyFromDom(para, unit);
if (cr.Errors.HasErrors)
{
StringBuilder error = new StringBuilder();
error.Append("Error Compiling Expression: ");
foreach (CompilerError err in cr.Errors)
{
error.AppendFormat("{0}/n", err.ErrorText);
}
throw new Exception("Error Compiling Expression: " + error.ToString());
}
Assembly asm=cr.CompiledAssembly;
Type type = asm.GetType("DC.Eval");
MethodInfo mi = type.GetMethod("GetValue", BindingFlags.Public | BindingFlags.Instance);
object obj = asm.CreateInstance("DC.Eval");
mi.Invoke(obj, new object[] { textBox1 });
}
}
}
WebForm
using System.CodeDom;
using System.CodeDom.Compiler;
using Microsoft.CSharp;
using System.Reflection;
using System.Text;
public class Class1
{
public static void GetValue(TextBox textBox1)
{
string codeSnippet = "using System; " + "/r/n" +
"using System.Web.UI.WebControls; " + "/r/n" +
"using System.Drawing; " + "/r/n" +
"namespace DC{" + "/r/n" +
"public class Eval" + "/r/n" +
"{" + "/r/n" +
"public Eval(){} " + "/r/n" +
"public void GetValue(TextBox textBox1)" + "/r/n" +
"{" + "/r/n" +
"textBox1.BackColor=Color.Red;" + "/r/n" +
"}" + "/r/n" +
"}" + "/r/n" +
"}";
CodeSnippetCompileUnit unit = new CodeSnippetCompileUnit(codeSnippet);
ICodeCompiler compiler = new CSharpCodeProvider().CreateCompiler();
CompilerParameters para = new CompilerParameters();
para.ReferencedAssemblies.Add("System.dll");
para.ReferencedAssemblies.Add("System.Drawing.dll");
para.ReferencedAssemblies.Add("System.Web.dll");
para.GenerateInMemory = true;
para.GenerateExecutable = false;
para.OutputAssembly = "Eval.dll";
CompilerResults cr = compiler.CompileAssemblyFromDom(para, unit);
if (cr.Errors.HasErrors)
{
StringBuilder error = new StringBuilder();
error.Append("Error Compiling Expression: ");
foreach (CompilerError err in cr.Errors)
{
error.AppendFormat("{0}/n", err.ErrorText);
}
throw new Exception("Error Compiling Expression: " + error.ToString());
}
Assembly asm = cr.CompiledAssembly;
Type type = asm.GetType("DC.Eval");
MethodInfo mi = type.GetMethod("GetValue", BindingFlags.Public | BindingFlags.Instance);
object obj = asm.CreateInstance("DC.Eval");
mi.Invoke(obj, new object[] { textBox1 });
}
}
客户端这样,就能改变输入框的背景色: Class1.GetValue(TextBox1); 只是用来练习,实际上那么做可蠢透了。
这样简单一点:
Type type = this.textBox1.GetType();
type.GetProperty("BackColor").SetValue(textBox1, Color.Red, null);
MessageBox.Show(type.GetProperty("BackColor").GetValue(textBox1,null).ToString());