1.生成DLL
打开VS2008 - >新建->项目->类库->ClassLibrary1,在ClassLibrary1中会自动创建一个Class1类
class1中加入代码如下:
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 namespace ClassLibrary1 6 { 7 public class Class1 8 { 9 private int a = 0; 10 private int b = 0; 11 public int SetA 12 { 13 get { return a; } 14 set { a = value; } 15 } 16 17 public int SetB 18 { 19 get { return b; } 20 set { b = value; } 21 } 22 public int getResult() 23 { 24 return a - b; 25 } 26 } 27 }
创建两个子类:
1 //子类ClassAdd 2 using System; 3 using System.Collections.Generic; 4 using System.Linq; 5 using System.Text; 6 namespace ClassLibrary1 7 { 8 public class ClassAdd : Class1 9 { 10 public int getResult() 11 { 12 return SetA + SetB; 13 } 14 } 15 }
1 //子类ClassMult 2 using System; 3 using System.Collections.Generic; 4 using System.Linq; 5 using System.Text; 6 namespace ClassLibrary1 7 { 8 public class ClassMult : Class1 9 { 10 public int getResult() 11 { 12 return SetA * SetB; 13 } 14 } 15 }
生成解决方案,在ClassLibrary1\ClassLibrary1\bin\Debug就可获得DLL文件
2.调用DLL
VS2008 - >新建->网站 创建一个websit1(asp.net网站)
在该项目下生成一个文件夹,命名bin:右键->新建文件夹->bin
然后在该文件件中引入之前生成的dll:bin右键->添加引用->浏览->找到ClassLibrary1.dll
在Default.aspx.cs添加如下代码:
1 using System; 2 using System.Configuration; 3 using System.Data; 4 using System.Linq; 5 using System.Web; 6 using System.Web.Security; 7 using System.Web.UI; 8 using System.Web.UI.HtmlControls; 9 using System.Web.UI.WebControls; 10 using System.Web.UI.WebControls.WebParts; 11 using System.Xml.Linq; 12 using ClassLibrary1; 13 public partial class _Default : System.Web.UI.Page 14 { 15 protected void Page_Load(object sender, EventArgs e) 16 { 17 ClassAdd add = new ClassAdd(); 18 ClassMult mult = new ClassMult(); 19 add.SetA = 10; 20 add.SetB = 11; 21 Response.Write(add.getResult().ToString()); 22 mult.SetA = 10; 23 mult.SetB = 11; 24 Response.Write("<br />" + mult.getResult().ToString()); 25 } 26 27 }
3.运行website1
网页显示结果:
21
110