---
title: "CodeSmith 使用教程(11) 引用其它文件或.Net 类库"
author: "mapdigit"
date: "2013-01-17T08:50:00Z"
tags: []
slug: "codesmith-11-net"
draft: false
lastmod: "2013-03-22T14:03:25Z"
id: 1834173
url: "https://mapdigit.iteye.com/blog/1834173"
---

在CodeSmith模板中可以引用.Net 类库，和普通的.Net 项目不同的是，对.Net 库的引用不是通过项目的Add reference 来实现，而是通过在代码模板中指明所要引用的Assembly.

比如引用CodeSmith自带的CodeSmith.CustomProperties.dll ，可以使用如下语句：

```
<%@ Assembly Name="CodeSmith.CustomProperties" %>
```

- Name指明所有需要引用的Assembly的名称，也可以使用Assembly的全名，比如ExampleAssembly, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null
- Src 指明可以动态编译的源码的相对路径名称
- Path 指明应用的Assembly存放的路径

引用合适的Assembly之后，和普遍C#语言类似，对于使用到的.Net类，需要通过Import引入该类所在的命名空间。

比如[CodeSmith 使用教程(9): Progress对象](http://www.imobilebbs.com/wordpress/archives/4219)引入Thread类所在的System.Threading

```
<%@ Import Namespace="System.Threading" %>
```

此外，如果在代码模板中需要引入一些源代码 （比如一些公用的代码）可以通过include ,比如：

```
<!– #include file="CommonScript.cs" –>
```

共享代码的方法除了上面使用的include 方法外，还可以通过设置`CodeTemplate`和`Assembly的Src属性来实现等。`