Visual Studio之所以强大,就因为它能为开发者尽可能地生成一些代码,免去了我们经常性去做一些无意义的,机械化的操作,从而使开发者不必把时间和精力都花在细枝末节上,能更专心于程序的核心部分,开发效率也大大提高。
说到这代码生成,实话说,在日常的开发任务中,似乎使用的频率不高,而且,动态编译也有一个消耗性能的过程。
那么,我们需要对代码生成有点了解吗?应该说是有这个需要的,说不定哪一天会用得上。有时候,我会发现VS的代码生成功能未必能满足我的需求,这时候,我也会考虑自己写一个代码生成工具。
.NET的类库也提供了支持,虽然不能说所有代码都可以生成,至少常用的大部分代码是没有问题的。
代码生成难学习吗?哈,有点吧,但不能说得上很难,重要的是,语言基础要过得关,你懂得如何写代码,才能知道怎么生成代码。
先不进行任何介绍,我们先来看一个例子,直观地认识一下如何生成代码。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.CodeDom;
using System.CodeDom.Compiler;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
// 声明一个命名空间
CodeNamespace MyNameSpace = new CodeNamespace("MyApp");
// 引用其它命名空间
MyNameSpace.Imports.AddRange(new CodeNamespaceImport[] {
new CodeNamespaceImport("System"),
new CodeNamespaceImport("System.Collections"),
new CodeNamespaceImport("System.Data"),
new CodeNamespaceImport("System.Text")
});
// 声明一个类
CodeTypeDeclaration MyClass = new CodeTypeDeclaration("MyTestClass");
// 定义Main入口点方法
CodeEntryPointMethod MyEntryPoint = new CodeEntryPointMethod();
// Main方法应为public static
MyEntryPoint.Attributes = MemberAttributes.Public | MemberAttributes.Static;
// Main方法返回类型为void
MyEntryPoint.ReturnType = new CodeTypeReference(typeof(void));
// Main方法中的代码
MyEntryPoint.Statements.Add(new CodeMethodInvokeExpression(
new CodeMethodReferenceExpression(new CodeTypeReferenceExpression(typeof(Console)), "WriteLine"),
new CodePrimitiveExpression("Hello,我的应用程序。")));
// 将方法添加到类中
MyClass.Members.Add(MyEntryPoint);
// 将类添加到命名空间
MyNameSpace.Types.Add(MyClass);
// 创建代码单元
CodeCompileUnit MyUnit = new CodeCompileUnit();
// 将命名空间添加到代码单元中
MyUnit.Namespaces.Add(MyNameSpace);
// 创建代码生成类-C#
CodeDomProvider ProviderCS = CodeDomProvider.CreateProvider("C#");
// 生成代码
ProviderCS.GenerateCodeFromCompileUnit(MyUnit, Console.Out, new CodeGeneratorOptions { BracingStyle = "C" });
// 生成VB.NET代码
CodeDomProvider ProviderVB = CodeDomProvider.CreateProvider("vb");
ProviderVB.GenerateCodeFromCompileUnit(MyUnit, Console.Out, new CodeGeneratorOptions());
Console.ReadKey();
}
}
}
把这个程序运行一下,我们看到两段代码,第一段是C#代码,第二段是VB.NET代码。
大家可能看上面的代码,有点云里雾里,不过别急,这个例子只是让各位对代码生成有个整体认识罢了,在后面的文章中,我会尽可能地对每一个知识点进行说明,并且每个要点都会有很简单的示例辅助理解。
分享到:
相关推荐
ASP.NET AJAX深入浅出系列课程(31):构建高性能ASP.NET AJAX应用程序
ASP.NET AJAX深入浅出系列课程(17):关注ASP.NET AJAX的核心:ScriptManager
ASP.NET深入浅出系列,学习ASp.net基础教程
一个讲泛型体系的教程,非常详细。 深入浅出.NET泛型编程
ASP.NET AJAX深入浅出系列课程(19):VS 2008的JavaScript代码提示功能
ASP.NET AJAX深入浅出系列课程(27):AJAX Control Tookit工具包(中)
ASP.NET AJAX深入浅出系列课程(13):ASP.NET AJAX提供的Profile Service
深入浅出ASP.NET程序设计源代码
ASP.NET AJAX深入浅出系列课程(15):使用ASP.NET AJAX提供的Authentication Service
ASP.NET AJAX深入浅出系列课程(29):ASP.NET AJAX的相关扩展(上)
ASP.NET AJAX深入浅出系列课程(30):ASP.NET AJAX的相关扩展(下)
ASP.NET AJAX深入浅出系列课程(32):构建高安全性ASP.NET AJAX应用程序
ASP.NET AJAX深入浅出系列课程(18):ASP.NET AJAX的国际化与本地化支持
[深入浅出AutoCAD.NET二次开发].李冠亿.扫描版 适合AutoCAD二次开发新手入门
ASP.NET AJAX深入浅出系列课程(14):Profile Service的实现方式与扩展
ASP.NET AJAX深入浅出系列课程(10):基于Microsoft AJAX Library扩展客户端组件
深入浅出AutoCAD.NET二次开发 .李冠亿.扫描版, 带目录,带目录,带目录 可复制文本,可复制文本,可复制文本