背景
上次提到单个.cs文件打包为.dll文件的操作,有点繁琐。这次我们用Visual Studio 2019这个强大的编译器进行辅助打包。
操作步骤
1、新建一个项目,如Test
我这里举例是窗体应用程序,同学们练习的时候这个项目创建成控制台应用程序也可以。
2、给项目添加一个类文件,如Student.cs[并给此类注明为公用类:public],给它添加字段name、age、sex。以及对应属性。给它添加无参构造函数以及带参构造函数。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Test
{
/// <summary>
/// 学生类
/// </summary>
public class Student
{
#region 字段区
/// <summary>
/// 姓名
/// </summary>
private string name;
/// <summary>
/// 年龄
/// </summary>
private int age;
/// <summary>
/// 性别
/// </summary>
private string sex;
#endregion
#region 属性区
/// <summary>
/// 姓名
/// </summary>
public String Name
{
set
{
this.name = value;
}
get
{
return this.name;
}
}
/// <summary>
/// 年龄
/// </summary>
public int Age
{
set
{
this.age = value;
}
get
{
return this.age;
}
}
/// <summary>
/// 性别
/// </summary>
public String Sex
{
set
{
this.sex = value;
}
get
{
return this.sex;
}
}
#endregion
#region 函数区
/// <summary>
/// 无参构造函数
/// </summary>
public Student()
{
//默认约定
this.name = "冰凌";
this.age = 24;
this.sex = "男";
}
/// <summary>
/// 带参构造函数
/// </summary>
/// <param name="name">姓名</param>
/// <param name="age">年龄</param>
/// <param name="sex">性别</param>
public Student(string name,int age,string sex)
{
this.name = name;
this.age = age;
this.sex = sex;
}
#endregion
}
}
3、右键单击项目名称,在下拉菜单中选择属性,在应用程序里选择输出类型为类库
4、生成->重新生产解决方案
5、重新生成完毕后,就可以在我们的项目文件夹的bin/Debug/下看到项目名称.dll文件了