动态生成类中的问题,请 xiammy(傻二哥) 继续帮忙 谢谢大家了,请大家继续帮忙 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 还要说对不起,暂时不能解决问题。 解决了关键问题在于你使用的类型,没有引入。其实就像我们自己写的一个单元一样。载源代码中加入using System;就可以了;Good Luck!附属代码如下:---------------------------------------using System;using System.Drawing;using System.Collections;using System.ComponentModel;using System.Windows.Forms;using System.Data;using System.CodeDom;using System.CodeDom.Compiler;using Microsoft.CSharp;using System.Reflection;using System.Text;namespace WindowsApplication8{ /// <summary> /// Form1 的摘要说明。 /// </summary> public class Form1 : System.Windows.Forms.Form { /// <summary> /// 必需的设计器变量。 /// </summary> private System.ComponentModel.Container components = null; public Form1() { // // Windows 窗体设计器支持所必需的 // InitializeComponent(); // // TODO: 在 InitializeComponent 调用后添加任何构造函数代码 // } /// <summary> /// 清理所有正在使用的资源。 /// </summary> protected override void Dispose( bool disposing ) { if( disposing ) { if (components != null) { components.Dispose(); } } base.Dispose( disposing ); } private static ICodeCompiler compiler = null; private static CompilerParameters paras = null; private static CompilerResults result = null; private System.Windows.Forms.PropertyGrid propertyGrid1; private System.Windows.Forms.Button button1; private static Assembly assembly = null; public static object NewClass(string className) { // 创建编译器实例。 compiler = (new CSharpCodeProvider().CreateCompiler()); // 设置编译参数。 paras = new CompilerParameters(); paras.GenerateExecutable = false; paras.GenerateInMemory = true; paras.OutputAssembly = "Xiammy"; // 创建动态代码。 StringBuilder classSource = new StringBuilder(); classSource.Append("using System;\n"); classSource.Append(" public class "+ className +"\n"); classSource.Append(" {\n"); // 创建一个属性。 classSource.Append(" private System.DateTime propertyName = new System.DateTime(2003, 2, 20);\n"); classSource.Append(" private int i = 0;\n"); classSource.Append(" public DateTime " + "PropertyName\n"); classSource.Append(" {\n"); classSource.Append(" get{ return propertyName;} \n"); classSource.Append(" set{ propertyName = value; }\n"); classSource.Append(" }\n"); classSource.Append(" public int " + "I\n"); classSource.Append(" {\n"); classSource.Append(" get{ return i;} \n"); classSource.Append(" set{ i = value;} \n"); classSource.Append(" }\n"); classSource.Append(" }\n"); System.Diagnostics.Debug.WriteLine(classSource.ToString()); // 编译代码。 result = compiler.CompileAssemblyFromSource(paras, classSource.ToString()); // 获取编译后的程序集。 assembly = result.CompiledAssembly; // 动态调用方法。 return assembly.CreateInstance(className); } #region Windows Form Designer generated code /// <summary> /// 设计器支持所需的方法 - 不要使用代码编辑器修改 /// 此方法的内容。 /// </summary> private void InitializeComponent() { this.propertyGrid1 = new System.Windows.Forms.PropertyGrid(); this.button1 = new System.Windows.Forms.Button(); this.SuspendLayout(); // // propertyGrid1 // this.propertyGrid1.CommandsVisibleIfAvailable = true; this.propertyGrid1.LargeButtons = false; this.propertyGrid1.LineColor = System.Drawing.SystemColors.ScrollBar; this.propertyGrid1.Location = new System.Drawing.Point(8, 8); this.propertyGrid1.Name = "propertyGrid1"; this.propertyGrid1.SelectedObject = this.button1; this.propertyGrid1.Size = new System.Drawing.Size(176, 248); this.propertyGrid1.TabIndex = 0; this.propertyGrid1.Text = "propertyGrid1"; this.propertyGrid1.ViewBackColor = System.Drawing.SystemColors.Window; this.propertyGrid1.ViewForeColor = System.Drawing.SystemColors.WindowText; // // button1 // this.button1.Location = new System.Drawing.Point(208, 40); this.button1.Name = "button1"; this.button1.Size = new System.Drawing.Size(80, 24); this.button1.TabIndex = 1; this.button1.Text = "button1"; this.button1.Click += new System.EventHandler(this.button1_Click); // // Form1 // this.AutoScaleBaseSize = new System.Drawing.Size(6, 14); this.ClientSize = new System.Drawing.Size(292, 273); this.Controls.AddRange(new System.Windows.Forms.Control[] { this.button1, this.propertyGrid1}); this.Name = "Form1"; this.Text = "Form1"; this.ResumeLayout(false); } #endregion /// <summary> /// 应用程序的主入口点。 /// </summary> [STAThread] static void Main() { Application.Run(new Form1()); } private void button1_Click(object sender, System.EventArgs e) { object xiammy = null; xiammy = NewClass("hanxiaoming"); this.propertyGrid1.SelectedObject = xiammy; if (xiammy.Equals(null)) this.Text = "null"; } }}GoodLuck! 想请教下,怎么把十六进制原样存入到byte[]中? 求教lock的问题 关于C#中new关键字的问题 请问如何在C#的代码中控制Expression上的按钮是否显示呢? 跨库查询语句的一点小问题 GridView 排序页面不动问题 c#可以实现连接和断开宽带的拨号程序吗? 数据库新增数据的问题 大家看,这话是怎么理解的? C#里面有没有Server.Math()这个函数? 请问这个是怎么回事?100分 在线等待 C#中关于“用户控件”的两个问题:不能将对象引用设置到对象实例?
载源代码中加入using System;
就可以了;Good Luck!
附属代码如下:
---------------------------------------
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.CodeDom;
using System.CodeDom.Compiler;
using Microsoft.CSharp;
using System.Reflection;
using System.Text;
namespace WindowsApplication8
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null; public Form1()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent(); //
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
} /// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
} private static ICodeCompiler compiler = null;
private static CompilerParameters paras = null;
private static CompilerResults result = null;
private System.Windows.Forms.PropertyGrid propertyGrid1;
private System.Windows.Forms.Button button1;
private static Assembly assembly = null; public static object NewClass(string className)
{
// 创建编译器实例。
compiler = (new CSharpCodeProvider().CreateCompiler());
// 设置编译参数。
paras = new CompilerParameters();
paras.GenerateExecutable = false;
paras.GenerateInMemory = true;
paras.OutputAssembly = "Xiammy";
// 创建动态代码。
StringBuilder classSource = new StringBuilder();
classSource.Append("using System;\n");
classSource.Append(" public class "+ className +"\n");
classSource.Append(" {\n");
// 创建一个属性。
classSource.Append(" private System.DateTime propertyName = new System.DateTime(2003, 2, 20);\n");
classSource.Append(" private int i = 0;\n");
classSource.Append(" public DateTime " + "PropertyName\n");
classSource.Append(" {\n");
classSource.Append(" get{ return propertyName;} \n");
classSource.Append(" set{ propertyName = value; }\n");
classSource.Append(" }\n"); classSource.Append(" public int " + "I\n");
classSource.Append(" {\n");
classSource.Append(" get{ return i;} \n");
classSource.Append(" set{ i = value;} \n");
classSource.Append(" }\n"); classSource.Append(" }\n");
System.Diagnostics.Debug.WriteLine(classSource.ToString()); // 编译代码。
result = compiler.CompileAssemblyFromSource(paras, classSource.ToString());
// 获取编译后的程序集。
assembly = result.CompiledAssembly;
// 动态调用方法。
return assembly.CreateInstance(className);
} #region Windows Form Designer generated code
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.propertyGrid1 = new System.Windows.Forms.PropertyGrid();
this.button1 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// propertyGrid1
//
this.propertyGrid1.CommandsVisibleIfAvailable = true;
this.propertyGrid1.LargeButtons = false;
this.propertyGrid1.LineColor = System.Drawing.SystemColors.ScrollBar;
this.propertyGrid1.Location = new System.Drawing.Point(8, 8);
this.propertyGrid1.Name = "propertyGrid1";
this.propertyGrid1.SelectedObject = this.button1;
this.propertyGrid1.Size = new System.Drawing.Size(176, 248);
this.propertyGrid1.TabIndex = 0;
this.propertyGrid1.Text = "propertyGrid1";
this.propertyGrid1.ViewBackColor = System.Drawing.SystemColors.Window;
this.propertyGrid1.ViewForeColor = System.Drawing.SystemColors.WindowText;
//
// button1
//
this.button1.Location = new System.Drawing.Point(208, 40);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(80, 24);
this.button1.TabIndex = 1;
this.button1.Text = "button1";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(292, 273);
this.Controls.AddRange(new System.Windows.Forms.Control[] {
this.button1,
this.propertyGrid1});
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false); }
#endregion /// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
} private void button1_Click(object sender, System.EventArgs e)
{
object xiammy = null;
xiammy = NewClass("hanxiaoming");
this.propertyGrid1.SelectedObject = xiammy;
if (xiammy.Equals(null))
this.Text = "null";
}
}
}
GoodLuck!