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 = ; // 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 > )
{
for( int i = ; 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();
}
C# 在运行时动态创建类型的更多相关文章
- logback运行时动态创建日志文件
package com.example.demo.config; import ch.qos.logback.classic.Level; import ch.qos.logback.classic. ...
- [转] Java运行时动态生成class的方法
[From] http://www.liaoxuefeng.com/article/0014617596492474eea2227bf04477e83e6d094683e0536000 廖雪峰 / 编 ...
- Java 运行时动态生成class
转载 http://www.liaoxuefeng.com/article/0014617596492474eea2227bf04477e83e6d094683e0536000 Java是一门静态语言 ...
- LINQ to SQL 运行时动态构建查询条件
在进行数据查询时,经常碰到需要动态构建查询条件.使用LINQ实现这个需求可能会比以前拼接SQL语句更麻烦一些.本文介绍了3种运行时动态构建查询条件的方法.本文中的例子最终实现的都是同一个功能,从Nor ...
- 使用javassist运行时动态重新加载java类及其他替换选择
在不少的情况下,我们需要对生产中的系统进行问题排查,但是又不能重启应用,java应用不同于数据库的存储过程,至少到目前为止,还不能原生的支持随时进行编译替换,从这种角度来说,数据库比java的动态性要 ...
- .NET6运行时动态更新限流阈值
昨天博客园撑不住流量又崩溃了,很巧正在编写这篇文章,于是产生一个假想:如果博客园用上我这个限流组件会怎么样呢? 用户会收到几个429错误,并且多刷新几次就看到了内容,不会出现完全不可用. 还可以降低查 ...
- C++高效安全的运行时动态类型转换
关键字:static_cast,dynamic_cast,fast_dynamic_cast,VS 2015. OS:Window 10. C++类之间类型转换有:static_cast.dynami ...
- 解决 Retrofit 多 BaseUrl 及运行时动态改变 BaseUrl ?
原文地址: juejin.im/post/597856- 解决Retrofit多BaseUrl及运行时动态改变BaseUrl(一) 解决Retrofit多BaseUrl及运行时动态改变BaseUrl( ...
- 运行时动态库:not found 及介绍-linux的-Wl,-rpath命令
---此文章同步自我的CSDN博客--- 一.运行时动态库:not found 今天在使用linux编写c/c++程序时,需要用到第三方的动态库文件.刚开始编译完后,运行提示找不到动态库文件.我就 ...
随机推荐
- Why Python is Slow
Why Python is Slow: Looking Under the Hood https://jakevdp.github.io/blog/2014/05/09/why-python-is-s ...
- Python Generators(生成器)--yield
参考:http://blog.csdn.net/scelong/article/details/6969276 Python生成器 什么是python生成器,意思是带有一个yield语句的函数,既然它 ...
- UNIX标准化及实现之UNIX标准化、UNIX系统实现、标准和实现的关系以及ISO C标准头文件
一.UNIX标准化 1.ISO C (International Organization for Standardization) 2.IEEE POSIX (Institue of Electri ...
- css笔记13:display用法
1.代码演示: element.html如下: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" ...
- Asp.net封装js的类
using System; using System.Collections.Generic; using System.Text; using System.Web; using System.We ...
- CSS skills: 1) Navigate item's animation
<style> .nav { border-right:1px solid #268eb7; } .nav li{overflow:hidden;height:55px;border-le ...
- CF Playing with Paper
Playing with Paper time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- [改善Java代码]break万万不可忘
建议15:break万万不可忘 public class Client { public static void main(String[] args) { System.out.println(&q ...
- Sherlock and The Beast
Sherlock Holmes suspects his archenemy, Professor Moriarty, is once again plotting something diaboli ...
- CSS3 伸缩布局盒模型记
CSS3 伸缩布局盒模型 CSS3引入的布局模式Flexbox布局,主要思想是让容器有能力让其子项目能够改变其宽度,高度,以最佳方式填充可用空间.Flex容器使用Flex项目可以自动放大与收缩,用来填 ...