谢谢大家了,请大家继续帮忙

解决方案 »

  1.   

    还要说对不起,暂时不能解决问题。
      

  2.   

    解决了关键问题在于你使用的类型,没有引入。其实就像我们自己写的一个单元一样。
    载源代码中加入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!