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