让我们反射吧

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());

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值