假设我的界面上有100个文本框, 其Name属性分别是"tb1,tb2,tb3....tb98,tb99,tb100",我现在有一个方法如下:
private void changeValue(int i)
{
string s = string.Fromat("tb{0}.Text = \"changed\"",i);
// 此处把 s 当成代码来执行.
}
例如,我有一个字符串,
"tbCustomer.Text = \"AAA\";";我现在想把它变成一行代码
tbCustomer.Text = "AAA";
该如何实现?
private void changeValue(int i)
{
string s = string.Fromat("tb{0}.Text = \"changed\"",i);
// 此处把 s 当成代码来执行.
}
例如,我有一个字符串,
"tbCustomer.Text = \"AAA\";";我现在想把它变成一行代码
tbCustomer.Text = "AAA";
该如何实现?
using System.CodeDom.Compiler;
using Microsoft.CSharp;来做.
貌似没什么好办法.
STRING到textbox对象...不知怎么处理,期待楼下的....
http://hi.baidu.com/%BE%FD%D0%D0%BD%A1/blog/item/acf943b3acce92a6d8335af0.htmlMicrosoft.CSharp.CSharpCodeProvider provider = new Microsoft.CSharp.CSharpCodeProvider(); System.CodeDom.Compiler.ICodeCompiler comp = provider.CreateCompiler(); System.CodeDom.Compiler.CompilerParameters cp = new System.CodeDom.Compiler.CompilerParameters(); cp.ReferencedAssemblies.Add("system.dll") ;
cp.ReferencedAssemblies.Add("system.data.dll") ;
cp.ReferencedAssemblies.Add("system.xml.dll") ;
cp.GenerateExecutable = false ;
cp.GenerateInMemory = true ; string code = @"using System;
using System.Data;
using System.Xml;
public class Judgement
{
public object GetJude()
{
return (" + expression + @");
}
}" ;
System.CodeDom.Compiler.CompilerResults cr = comp.CompileAssemblyFromSource(cp,code);
System.Diagnostics.Debug.Write(code); if(cr.Errors.HasErrors)
{
System.Text.StringBuilder errorMsg = new System.Text.StringBuilder();
foreach(System.CodeDom.Compiler.CompilerError err in cr.Errors)
{
errorMsg.Append(err.ErrorText );
}
System.Diagnostics.Debug.WriteLine(errorMsg.ToString());
throw new System.Exception
//str相当于"tbCustomer.Text = \"AAA\";";
public void ss(string str)
{
string[] sr = str.Split('=');
string[] dControl = sr[0].Split('.');
TextBox t1 = (TextBox)Page.Form.FindControl(dControl[0].ToString());
if (t1 != null)
{
t1.Text = sr[1];
}
}
楼主自己在修改下 应该可以