参考:
http://www.codeproject.com/csharp/runtime_eval.asp应该可以实现你要的功能
http://www.codeproject.com/csharp/runtime_eval.asp应该可以实现你要的功能
解决方案 »
- 为什么绑定不了DataGridView的数据
- 请教窗口中实现这种效果各子窗口或控件使用的类型?
- toolStripDropDownButton 动态创建多层菜单后,如何获取用户选择
- 如何在无窗体类下使用timer控件
- 怎么判断form里某个像素是否是红色
- 参数中的对象没有名字,这是什么用法?(偶实在是查不到了,很弱质的问题哈!)
- 在winform中,如何打开一个form而关闭原有的form
- 请问Reporting Servies 中少了using Microsoft.ReportingServices.Interfaces的引用,要加入什么DLL?
- JAVA写的一个加密工具类,求大牛改写成PHP、C#、C++、Python等版本
- Session问题!
- ref 与 out 做为参数调用时的区别
- 窗体和控件在分辨率改变后,随分辨率的变化窗体和控件也随只变化。
返回值是一个object类型的值
{
/// <summary>
/// Class1 的摘要说明。
/// </summary>
class Class1
{
public void print ()
{
Console.WriteLine(" 你好呀!");
}
}
class Cass2
{
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main(string[] args)
{
Type myType1 = Type.GetType("根据类名实例化类.Class1");
if(myType1.IsClass)
{
Class1 c = (Class1)Activator.CreateInstance(myType1);
c.print();
}
}
}
}
Class1 c = (Class1)Activator.CreateInstance(myType1);关键是不知道Class1,也就是说在程序中不能出现Class1,只有一个字符串string strClassName = “Class1”;
string strClassName;
如果strClassName 是“Class1”则实例化一个Class1的对象,如果strClassName 是“Class2” 则实例化一个Class2的对象。而strClassName具体是什么,在程序设计时并不知道。
{
public abc(string name)
{
ICodeCompiler compiler=new CSharpCodeProvider().CreateCompiler();
CompilerParameters cp=new CompilerParameters();
cp.ReferencedAssemblies.Add("system.dll");
\\你可以在这里根据需要添加引用
cp.ReferencedAssemblies.Add("System.Windows.Forms.dll");
cp.GenerateExecutable=false;
cp.GenerateInMemory=true;
StringBuilder str=new StringBuilder();
str.Append("using System;\n");
\\你可以在这里根据需要添加引用
str.Append("using System.Windows.Forms;\n");
str.Append("namespace abc {\n");
str.Append("public class control1:"+name+" {\n");
str.Append("}}");
CompilerResults result=compiler.CompileAssemblyFromSource(cp,str.ToString());
if (result.Errors.HasErrors)
{
string s="";
for (int i=0;i<result.Errors.Count;i++)
{
s+=result.Errors[i].ErrorText+"\n";
}
throw new Exception("Error Compiling Expression: " + s);
}
Assembly assembly=result.CompiledAssembly;
o=assembly.CreateInstance("abc.control1");
}
public static object GetObjectByName(string name)
{
abc abc1= new abc(name);
return abc1.o;
}
public object o=null;
}如果你要求实例化的类都在System和System.Windows.Forms名称空间下的话,你可以直接使用上面的类,需要其他的名称空间,自己添加 :)
使用:
Label o=(Label)abc.GetObjectByName("Label");
o.Parent=this;
o.Left=10;
o.Top=10;
o.Text="abc";
o.Height=20;
o.Width=40;
未处理的"System.NullReferenceException"类型异常出现在InspectionManager.exe中
其他信息:Object reference not set to an instance of an object.但若将程序改为ReportClass oRpt = new RptWorkSheetType1();
则执行正常。苦恼!
为何?