动态执行 VB.NET 和 C# 代码
有时候我们需要尝试动态地与一些代码进行交互,而不是只能执行程序内已编死的代码,那该怎么办呢?
我首先推荐各种脚本语言,如Javascript、Lua、Python等等,这些脚本语言有很多优秀的第三方类库,可以很方便的与 .NET 系统集成,让我们的程序中执行动态代码。
但如果你一定想用VB.NET或者C#的代码来运行一段程序,这里就要用到动态编译的功能了。
下面是我写的两个实例,你只需要在窗体 FormMain 中添加一个 button 和一个 textbox 即可,默认名为 Button1、TextBox1。
VB.NET代码
Imports System.CodeDom.Compiler
Imports System.Reflection
Public Class FormMain
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
' 编译参数
Dim cpars As New CompilerParameters
' 编译参数,如 /optimize /removeintchecks 等
cpars.CompilerOptions = "/optimize "
cpars.GenerateInMemory = True '在内存中编译而不输出文件
cpars.GenerateExecutable = False '并不输出执行文件
cpars.IncludeDebugInformation = False '不需要调试信息
' 导入类库(根据自己代码的需要导入)
cpars.ReferencedAssemblies.Add("mscorlib.dll")
cpars.ReferencedAssemblies.Add("System.dll")
cpars.ReferencedAssemblies.Add("System.Data.dll")
cpars.ReferencedAssemblies.Add("System.Deployment.dll")
cpars.ReferencedAssemblies.Add("System.Drawing.dll")
cpars.ReferencedAssemblies.Add("System.Windows.Forms.dll")
cpars.ReferencedAssemblies.Add("System.Xml.dll")
cpars.ReferencedAssemblies.Add("Microsoft.VisualBasic.dll")
' 编译参数,为导入的类库设置全局引用(否则必须使用完整的命名空间名称才能正确调用函数)
cpars.CompilerOptions &= " /imports:" & _
"Microsoft.VisualBasic," & _
"System," & _
"System.Collections," & _
"System.Collections.Generic," & _
"System.Drawing," & _
"System.Windows.Forms"
' 设置编译器
Dim vbc As New VBCodeProvider
'Dim vbc = CodeDomProvider.CreateProvider("VisualBasic") '等效方法
' 一个简单的模板类
Dim codex As String = _
"Public Class CompClass" & vbCrLf & _
" Shared Function RunCode() As Object" & vbCrLf & _
" '$" & vbCrLf & _
" End Function" & vbCrLf & _
"End Class"
' 替换代码到模板类中
Dim code As String = codex.Replace("'$", TextBox1.Text)
' 编译并返回
Dim resut As CompilerResults = vbc.CompileAssemblyFromSource(cpars, code)
' 如果发生了错误
Then
).ToString)
Return
End If
' 尝试调用代码
Dim asm As Assembly = resut.CompiledAssembly '获取程序集
' 获取我们编写的静态方法
Dim mi As MethodInfo = asm.GetType("CompClass").GetMethod("RunCode")
' 执行代码,并获取返回值
Dim ret As Object = mi.Invoke(Nothing, Nothing)
' 对返回值进行处理
If ret IsNot Nothing Then
MsgBox(ret.ToString)
End If
End Sub
End Class
执行程序,在 Textbox1 里写入一些VB代码,按 Button1 即可立即执行里面的代码。
如果拥有返回值,程序还可以获取代码的返回值,但有可能需要你进行拆箱处理。
C#代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Reflection;
using System.CodeDom.Compiler;
namespace WindowsFormsApplication1
{
public partial class FormMain : Form
{
public FormMain()
{
InitializeComponent();
}
private void Button1_Click(object sender, EventArgs e)
{
// 编译参数
var cpars = new CompilerParameters();
cpars.CompilerOptions = "/optimize ";
cpars.GenerateInMemory = true;
cpars.GenerateExecutable = false;
cpars.IncludeDebugInformation = false;
// 导入类库(根据自己代码的需要导入)
cpars.ReferencedAssemblies.Add("mscorlib.dll");
cpars.ReferencedAssemblies.Add("System.dll");
cpars.ReferencedAssemblies.Add("System.Data.dll");
cpars.ReferencedAssemblies.Add("System.Deployment.dll");
cpars.ReferencedAssemblies.Add("System.Drawing.dll");
cpars.ReferencedAssemblies.Add("System.Windows.Forms.dll");
cpars.ReferencedAssemblies.Add("System.Xml.dll");
// 编译器实例
var csc = new Microsoft.CSharp.CSharpCodeProvider();
//var csc = CodeDomProvider.CreateProvider("CSharp");
// 一个简单的模板类
// 因为C#的编译器无法设置全局命名空间,所以需要在代码中导入命名空间
var codex = @"
using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.Windows.Forms;
class CompClass{
static public object RunCode(){
//$
return null;
}
}
";
// 替换代码到模板类中
var code = codex.Replace("//$", TextBox1.Text);
// 编译并返回
var resut = csc.CompileAssemblyFromSource(cpars, code);
// 错误警告
) {
MessageBox.Show(resut.Errors[].ToString());
return;
}
// 调用代码
var asm = resut.CompiledAssembly;
var mi = asm.GetType("CompClass").GetMethod("RunCode");
object ret = mi.Invoke(null, null);
if (ret != null) {
MessageBox.Show(ret.ToString());
}
}
}
}
C#的代码流程与VB的基本相同,区别在于C#的编译器没有导入全局命名空间的参数,因此需要在模板类里写入你需要导入的命名空间。
其他的用法基本都一样。
PS: 我有空再写一点与第三方脚本库进行交互的代码示例。
动态执行 VB.NET 和 C# 代码的更多相关文章
- C#动态执行代码
在开始之前,先熟悉几个类及部分属性.方法:CSharpCodeProvider.ICodeCompiler.CompilerParameters.CompilerResults.Assem ...
- (转+整理)C#中动态执行代码
通过微软提供的CSharpCodeProvider,CompilerParameters,CompilerResults等类,可以在运行时,动态执行自己写的代码文件.原理就是把你的代码文件动态编译成e ...
- 动态执行文本vba代码
动态执行文本vba代码 Public Sub StringExecute(s As String) Dim vbComp As Object Set vbComp = ThisWorkbook.VBP ...
- Javascript动态执行JS(new Function与eval比较)
new Function与eval可以动态执行JS,只要把拼接好的JS方法,然后以字符串的形式传入到这两个函数,可以执行,其中new Function用在模板引擎比较多. 用 Function 类直接 ...
- PHP 动态执行
PHP 动态执行 在页面上直接输入代码,点击执行,返回执行结果 方法很简单,主要使用了 $newfunc = create_function('', $code); 函数来实现. 代码如下: < ...
- JDK1.8中如何用ScriptEngine动态执行JS
JDK1.8中如何用ScriptEngine动态执行JS jdk1.6开始就提供了动态脚本语言诸如JavaScript动态的支持.这无疑是一个很好的功能,毕竟Java的语法不是适合成为动态语言.而JD ...
- 【05】Firebug动态执行JavaScript
Firebug动态执行JavaScript 您可以使用Firebug来编写并实时执行一个JavaScript. 这是为了测试,并确保该脚本工作正常,这是将JavaScript代码部署在生产环境前的好方 ...
- 根据数据库记录动态生成C#类及其公共属性并动态执行的解决方案
原文:根据数据库记录动态生成C#类及其公共属性并动态执行的解决方案 问题: C#中,想动态产生这么一个类: public class StatisticsData { public ...
- 第6.6节 Python动态执行小结
一. Python动态执行支持通过输入数据流或文件传入Python源代码串,进行编译后执行,可以通过这种方式扩展Python程序的功能: 二. 动态执行方法可能导致恶意攻击,因此使用时需要 ...
随机推荐
- Spring IOC(一)体系结构
Spring IOC(一)体系结构 Spring 系列目录(https://www.cnblogs.com/binarylei/p/10198698.html) BeanFactory 是Spring ...
- python 函数调用顺序
def foo(): print ('in the foo') bar() def bar(): print ('in the bar') foo() 1.foo函数进入内存 2.bar函数进入内存 ...
- The Django Book(自定义ModelAdmi类)
默认的,管理界面下显示的东西只是 python2:def __unicode__(self): 和 python3:def __str__(self): 中返回的字段内容 想要让它更加的多元化的话 c ...
- 【Linux】ODBC安装
ODBC介绍 ODBC是Open Database Connect 即开发数据库互连的简称,它是一个用于访问数据库的统一界面标准.ODBC引入一个公共接口以解决不同数据库潜在的不一致性,从而很好的保证 ...
- libmysqlclient version
You probably know that the version number of the libmysqlclient.so library has changed from .16 to . ...
- 谈互联网开放平台:“去中心化”大势所趋 zz
文/磐石之心 几天前与好友聊到众筹咖啡馆的事情,他向我讲述了一个非常具有特色的众筹咖啡馆案例.而这个案例也引发我对当前互联网开放.去中心和集权的一些思考,今天就简单写出来与大家分享. 一个无赚钱目的的 ...
- nginx 502 Bad Gateway 错误解决办法
nginx出现502有很多原因,但大部分原因可以归结为资源数量不够用,也就是说后端PHP-fpm处理有问题,nginx将正确的客户端请求发给了后端的php-fpm进程,但是因为php-fpm进程的问题 ...
- top k问题
1.top k问题 在海量数据处理中,经常会遇到的一类问题:在海量数据中找出出现频率最高的前k个数,或者从海量数据中找出最大的前k个数,这类问题通常被称为top K问题.例如,在搜索引擎中,统计搜索最 ...
- warning: rpmts_HdrFromFdno: Header V3 RSA/SHA256 Signature, key ID fd431d51: NOKEY
问题: yum安装软件时候报如下错误: warning: rpmts_HdrFromFdno: Header V3 RSA/SHA256 Signature, key ID fd431d51: NOK ...
- 构造函数的prototype和constructor属性
Car.prototype = { name:'BMW', height:1400, long:4900 } function Car(color,owner){ this.color = color ...