• linkedu视频
  • 平面设计
  • 电脑入门
  • 操作系统
  • 办公应用
  • 电脑硬件
  • 动画设计
  • 3D设计
  • 网页设计
  • CAD设计
  • 影音处理
  • 数据库
  • 程序设计
  • 认证考试
  • 信息管理
  • 信息安全
菜单
linkedu.com
  • 网页制作
  • 数据库
  • 程序设计
  • 操作系统
  • CMS教程
  • 游戏攻略
  • 脚本语言
  • 平面设计
  • 软件教程
  • 网络安全
  • 电脑知识
  • 服务器
  • 视频教程
  • JavaScript
  • ASP.NET
  • PHP
  • 正则表达式
  • AJAX
  • JSP
  • ASP
  • Flex
  • XML
  • 编程技巧
  • Android
  • swift
  • C#教程
  • vb
  • vb.net
  • C语言
  • Java
  • Delphi
  • 易语言
  • vc/mfc
  • 嵌入式开发
  • 游戏开发
  • ios
  • 编程问答
  • 汇编语言
  • 微信小程序
  • 数据结构
  • OpenGL
  • 架构设计
  • qt
  • 微信公众号
您的位置:首页 > 程序设计 >C#教程 > C#在运行时动态创建类型的实现方法

C#在运行时动态创建类型的实现方法

作者: 字体:[增加 减小] 来源:互联网 时间:2017-05-28

通过本文主要向大家介绍了c#年月日时分秒,c#获取当前时分秒,c#年月日时分秒格式,c#写属时快捷键,c#获取视频时长等相关知识,希望对您有所帮助,也希望大家支持linkedu.com www.linkedu.com

本文实例讲述了C#在运行时动态创建类型的实现方法。是C#项目开发中很实用的技巧。分享给大家供大家参考。具体分析如下:

具体来说,C# 在运行时动态的创建类型是通过动态生成C#源代码,然后通过编译器编译成程序集的方式实现动态创建类型的。

主要功能代码如下:

public static Assembly NewAssembly()
{
  //创建编译器实例。  
  provider = new CSharpCodeProvider();
  //设置编译参数。  
  cp = new CompilerParameters();
  cp.GenerateExecutable = false;
  cp.GenerateInMemory = true;

  // Generate an executable instead of 
  // a class library.
  //cp.GenerateExecutable = true;

  // Set the assembly file name to generate.
  cp.OutputAssembly = "c:\\1.dll";

  // Generate debug information.
  cp.IncludeDebugInformation = true;


  // Save the assembly as a physical file.
  cp.GenerateInMemory = false;

  // Set the level at which the compiler 
  // should start displaying warnings.
  cp.WarningLevel = 3;

  // Set whether to treat all warnings as errors.
  cp.TreatWarningsAsErrors = false;

  // Set compiler argument to optimize output.
  cp.CompilerOptions = "/optimize";

  cp.ReferencedAssemblies.Add("System.dll");
  //cp.ReferencedAssemblies.Add("System.Core.dll");
  cp.ReferencedAssemblies.Add("System.Data.dll");
  //cp.ReferencedAssemblies.Add("System.Data.DataSetExtensions.dll");
  cp.ReferencedAssemblies.Add("System.Deployment.dll");
  cp.ReferencedAssemblies.Add("System.Design.dll");
  cp.ReferencedAssemblies.Add("System.Drawing.dll");
  cp.ReferencedAssemblies.Add("System.Windows.Forms.dll");

  //创建动态代码。  
  
  StringBuilder classSource = new StringBuilder();
  classSource.Append("using System;using System.Windows.Forms;\npublic  class  DynamicClass: UserControl \n");
  classSource.Append("{\n");
  classSource.Append("public DynamicClass()\n{\nInitializeComponent();\nConsole.WriteLine(\"hello\");}\n");
  classSource.Append( "private System.ComponentModel.IContainer components = null;\nprotected override void Dispose(bool disposing)\n{\n");
  classSource.Append( "if (disposing && (components != null)){components.Dispose();}base.Dispose(disposing);\n}\n");
  classSource.Append( "private void InitializeComponent(){\nthis.SuspendLayout();this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);");
  classSource.Append( "this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;this.Name = \"DynamicClass\";this.Size = new System.Drawing.Size(112, 74);this.ResumeLayout(false);\n}");
  //创建属性。  
  /*************************在这里改成需要的属性******************************/
  classSource.Append(propertyString("aaa"));
  classSource.Append(propertyString("bbb"));
  classSource.Append(propertyString("ccc"));

  classSource.Append("}");

  System.Diagnostics.Debug.WriteLine(classSource.ToString());
 
  //编译代码。  
  CompilerResults result = provider.CompileAssemblyFromSource(cp, classSource.ToString());
  if (result.Errors.Count > 0)
  {
 for( int i = 0; i < result.Errors.Count; i ++)
   Console.WriteLine(result.Errors[ i]);
 Console.WriteLine("error");
 return null;
  }
  
  //获取编译后的程序集。  
  Assembly assembly = result.CompiledAssembly;

  return assembly;
}

private static string propertyString(string propertyName)
{
  StringBuilder sbProperty = new StringBuilder();
  sbProperty.Append(" private  int  _" + propertyName + "  =  0;\n");
  sbProperty.Append(" public  int  " + "" + propertyName + "\n");
  sbProperty.Append(" {\n");
  sbProperty.Append(" get{  return  _" + propertyName + ";}  \n");
  sbProperty.Append(" set{  _" + propertyName + "  =  value;  }\n");
  sbProperty.Append(" }");
  return sbProperty.ToString();
}

</div>

希望本文所述对大家的C#程序设计有所帮助

</div>
分享到:QQ空间新浪微博腾讯微博微信百度贴吧QQ好友复制网址打印

您可能想查找下面的文章:

  • C#根据年月日计算星期几的函数
  • C#集合遍历时删除和增加元素的方法
  • C#在运行时动态创建类型的实现方法
  • C#根据年月日计算星期几的函数小例子

相关文章

  • 2017-05-28c#调用winrar解压缩文件代码分享
  • 2017-05-28C#常用的命名规则汇总
  • 2017-05-28C# listview添加combobox到单元格的实现代码
  • 2017-05-28C# MVC 微信支付教程系列之扫码支付代码实例
  • 2017-05-28C#委托delegate实例解析
  • 2017-05-28C#生成code128条形码的方法
  • 2017-05-28C#实现多线程的Web代理服务器实例
  • 2017-05-28C#中AS和IS关键字的用法
  • 2017-05-28C#影院售票系统毕业设计(4)
  • 2017-05-28c#中的interface abstract与virtual介绍

文章分类

  • JavaScript
  • ASP.NET
  • PHP
  • 正则表达式
  • AJAX
  • JSP
  • ASP
  • Flex
  • XML
  • 编程技巧
  • Android
  • swift
  • C#教程
  • vb
  • vb.net
  • C语言
  • Java
  • Delphi
  • 易语言
  • vc/mfc
  • 嵌入式开发
  • 游戏开发
  • ios
  • 编程问答
  • 汇编语言
  • 微信小程序
  • 数据结构
  • OpenGL
  • 架构设计
  • qt
  • 微信公众号

最近更新的内容

    • C#判断某程序是否运行的方法
    • C#怎么给PDF添加背景图片
    • 终于了解了下.net 和 j2ee的区别
    • 深入讲解C#编程中嵌套类型和匿名类型的定义与使用
    • C#中的递归APS和CPS模式详解
    • C# 程序集和反射详解
    • C#简单的特殊输出实例
    • C#登入实例
    • C#事件处理和委托event delegate实例简述
    • C#编程读取文档Doc、Docx及Pdf内容的方法

关于我们 - 联系我们 - 免责声明 - 网站地图

©2020-2025 All Rights Reserved. linkedu.com 版权所有