下面是你的源程序,我现在需要生成类的的属性值为Color类型。
我试了换掉你的程序中的using System  ->using System.Drawing
然后再定义propertyname为Color型。执行时总是报告错误:
未处理的“System.IO.FileNotFoundException”类型的异常出现在 mscorlib.dll 中,找不到它的一个依赖项。请帮忙!
---------------------------------------
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";
}
}
}

解决方案 »

  1.   

    我修改后的程序如下。
    在输出窗口显示的类如果单独定义的话可以成功运行
    谢谢!
    ---------------------------------------
    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.Drawing;\n");
    classSource.Append(" public class "+ className +"\n");
    classSource.Append(" {\n");

    // 创建一个属性。
    classSource.Append(" private System.Drawing.Color propertyName = Color.Black;\n");
    classSource.Append(" private int i = 0;\n");
    classSource.Append(" public Color " + "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";
    }
    }
    }
      

  2.   

    晕。  CodeCompileUnit 、ICodeGenerator.GenerateCodeFromCompileUnit